mirror of https://github.com/tqdm/tqdm.git
26 lines
4.4 KiB
JavaScript
26 lines
4.4 KiB
JavaScript
/*
|
|
Copyright (c) 2007-2014 IOLA and Ole Laursen
|
|
|
|
Permission is hereby granted, free of charge, to any person
|
|
obtaining a copy of this software and associated documentation
|
|
files (the "Software"), to deal in the Software without
|
|
restriction, including without limitation the rights to use,
|
|
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the
|
|
Software is furnished to do so, subject to the following
|
|
conditions:
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
!function(e){function t(t){function o(e){h.active&&(a(e),t.getPlaceholder().trigger("plotselecting",[s()]))}function n(t){1==t.which&&(document.body.focus(),void 0!==document.onselectstart&&null==m.onselectstart&&(m.onselectstart=document.onselectstart,document.onselectstart=function(){return!1}),void 0!==document.ondrag&&null==m.ondrag&&(m.ondrag=document.ondrag,document.ondrag=function(){return!1}),l(h.first,t),h.active=!0,x=function(e){i(e)},e(document).one("mouseup",x))}function i(e){return x=null,void 0!==document.onselectstart&&(document.onselectstart=m.onselectstart),void 0!==document.ondrag&&(document.ondrag=m.ondrag),h.active=!1,a(e),g()?r():(t.getPlaceholder().trigger("plotunselected",[]),t.getPlaceholder().trigger("plotselecting",[null])),!1}function s(){if(!g())return null;if(!h.show)return null;var o={},n=h.first,i=h.second;return e.each(t.getAxes(),function(e,t){if(t.used){var s=t.c2p(n[t.direction]),r=t.c2p(i[t.direction]);o[e]={from:Math.min(s,r),to:Math.max(s,r)}}}),o}function r(){var e=s();t.getPlaceholder().trigger("plotselected",[e]),e.xaxis&&e.yaxis&&t.getPlaceholder().trigger("selected",[{x1:e.xaxis.from,y1:e.yaxis.from,x2:e.xaxis.to,y2:e.yaxis.to}])}function c(e,t,o){return e>t?e:t>o?o:t}function l(e,o){var n=t.getOptions(),i=t.getPlaceholder().offset(),s=t.getPlotOffset();e.x=c(0,o.pageX-i.left-s.left,t.width()),e.y=c(0,o.pageY-i.top-s.top,t.height()),"y"==n.selection.mode&&(e.x=e==h.first?0:t.width()),"x"==n.selection.mode&&(e.y=e==h.first?0:t.height())}function a(e){null!=e.pageX&&(l(h.second,e),g()?(h.show=!0,t.triggerRedrawOverlay()):u(!0))}function u(e){h.show&&(h.show=!1,t.triggerRedrawOverlay(),e||t.getPlaceholder().trigger("plotunselected",[]))}function d(e,o){var n,i,s,r,c=t.getAxes();for(var l in c)if(n=c[l],n.direction==o&&(r=o+n.n+"axis",e[r]||1!=n.n||(r=o+"axis"),e[r])){i=e[r].from,s=e[r].to;break}if(e[r]||(n="x"==o?t.getXAxes()[0]:t.getYAxes()[0],i=e[o+"1"],s=e[o+"2"]),null!=i&&null!=s&&i>s){var a=i;i=s,s=a}return{from:i,to:s,axis:n}}function f(e,o){var n,i=t.getOptions();"y"==i.selection.mode?(h.first.x=0,h.second.x=t.width()):(n=d(e,"x"),h.first.x=n.axis.p2c(n.from),h.second.x=n.axis.p2c(n.to)),"x"==i.selection.mode?(h.first.y=0,h.second.y=t.height()):(n=d(e,"y"),h.first.y=n.axis.p2c(n.from),h.second.y=n.axis.p2c(n.to)),h.show=!0,t.triggerRedrawOverlay(),!o&&g()&&r()}function g(){var e=t.getOptions().selection.minSize;return Math.abs(h.second.x-h.first.x)>=e&&Math.abs(h.second.y-h.first.y)>=e}var h={first:{x:-1,y:-1},second:{x:-1,y:-1},show:!1,active:!1},m={},x=null;t.clearSelection=u,t.setSelection=f,t.getSelection=s,t.hooks.bindEvents.push(function(e,t){var i=e.getOptions();null!=i.selection.mode&&(t.mousemove(o),t.mousedown(n))}),t.hooks.drawOverlay.push(function(t,o){if(h.show&&g()){var n=t.getPlotOffset(),i=t.getOptions();o.save(),o.translate(n.left,n.top);var s=e.color.parse(i.selection.color);o.strokeStyle=s.scale("a",.8).toString(),o.lineWidth=1,o.lineJoin=i.selection.shape,o.fillStyle=s.scale("a",.4).toString();var r=Math.min(h.first.x,h.second.x)+.5,c=Math.min(h.first.y,h.second.y)+.5,l=Math.abs(h.second.x-h.first.x)-1,a=Math.abs(h.second.y-h.first.y)-1;o.fillRect(r,c,l,a),o.strokeRect(r,c,l,a),o.restore()}}),t.hooks.shutdown.push(function(t,i){i.unbind("mousemove",o),i.unbind("mousedown",n),x&&e(document).unbind("mouseup",x)})}e.plot.plugins.push({init:t,options:{selection:{mode:null,color:"#e8cfac",shape:"round",minSize:5}},name:"selection",version:"1.1"})}(jQuery); |