Skip to content
selectable.js 4.2 KiB
Newer Older
杨郁彬's avatar
杨郁彬 committed
/*!
 * jQuery coral Selectable 4.0.1
 *
 * Depends:
 *	jquery.ui.core.js
 *	jquery.ui.mouse.js
 *	jquery.ui.component.js
 */
(function(){$.component("coral.selectable",$.coral.mouse,{version:"4.0.1",options:{appendTo:"body",autoRefresh:true,distance:0,filter:"*",tolerance:"touch",selected:null,selecting:null,start:null,stop:null,unselected:null,unselecting:null},_create:function(){var b,a=this;this.element.addClass("coral-selectable");this.dragged=false;this.refresh=function(){b=$(a.options.filter,a.element[0]);b.addClass("coral-selectee");b.each(function(){var c=$(this),d=c.offset();$.data(this,"selectable-item",{element:this,$element:c,left:d.left,top:d.top,right:d.left+c.outerWidth(),bottom:d.top+c.outerHeight(),startselected:false,selected:c.hasClass("coral-selected"),selecting:c.hasClass("coral-selecting"),unselecting:c.hasClass("coral-unselecting")})})};this.refresh();this.selectees=b.addClass("coral-selectee");this._mouseInit();this.helper=$("<div class='coral-selectable-helper'></div>")},_destroy:function(){this.selectees.removeClass("coral-selectee").removeData("selectable-item");this.element.removeClass("coral-selectable coral-selectable-disabled");this._mouseDestroy()},_mouseStart:function(c){var b=this,a=this.options;this.opos=[c.pageX,c.pageY];if(this.options.disabled){return}this.selectees=$(a.filter,this.element[0]);this._trigger("start",c);$(a.appendTo).append(this.helper);this.helper.css({left:c.pageX,top:c.pageY,width:0,height:0});if(a.autoRefresh){this.refresh()}this.selectees.filter(".coral-selected").each(function(){var d=$.data(this,"selectable-item");d.startselected=true;if(!c.metaKey&&!c.ctrlKey){d.$element.removeClass("coral-selected");d.selected=false;d.$element.addClass("coral-unselecting");d.unselecting=true;b._trigger("unselecting",c,{unselecting:d.element})}});$(c.target).parents().addBack().each(function(){var d,e=$.data(this,"selectable-item");if(e){d=(!c.metaKey&&!c.ctrlKey)||!e.$element.hasClass("coral-selected");e.$element.removeClass(d?"coral-unselecting":"coral-selected").addClass(d?"coral-selecting":"coral-unselecting");
e.unselecting=!d;e.selecting=d;e.selected=d;if(d){b._trigger("selecting",c,{selecting:e.element})}else{b._trigger("unselecting",c,{unselecting:e.element})}return false}})},_mouseDrag:function(h){this.dragged=true;if(this.options.disabled){return}var e,g=this,c=this.options,b=this.opos[0],f=this.opos[1],a=h.pageX,d=h.pageY;if(b>a){e=a;a=b;b=e}if(f>d){e=d;d=f;f=e}this.helper.css({left:b,top:f,width:a-b,height:d-f});this.selectees.each(function(){var i=$.data(this,"selectable-item"),j=false;if(!i||i.element===g.element[0]){return}if(c.tolerance==="touch"){j=(!(i.left>a||i.right<b||i.top>d||i.bottom<f))}else{if(c.tolerance==="fit"){j=(i.left>b&&i.right<a&&i.top>f&&i.bottom<d)}}if(j){if(i.selected){i.$element.removeClass("coral-selected");i.selected=false}if(i.unselecting){i.$element.removeClass("coral-unselecting");i.unselecting=false}if(!i.selecting){i.$element.addClass("coral-selecting");i.selecting=true;g._trigger("selecting",h,{selecting:i.element})}}else{if(i.selecting){if((h.metaKey||h.ctrlKey)&&i.startselected){i.$element.removeClass("coral-selecting");i.selecting=false;i.$element.addClass("coral-selected");i.selected=true}else{i.$element.removeClass("coral-selecting");i.selecting=false;if(i.startselected){i.$element.addClass("coral-unselecting");i.unselecting=true}g._trigger("unselecting",h,{unselecting:i.element})}}if(i.selected){if(!h.metaKey&&!h.ctrlKey&&!i.startselected){i.$element.removeClass("coral-selected");i.selected=false;i.$element.addClass("coral-unselecting");i.unselecting=true;g._trigger("unselecting",h,{unselecting:i.element})}}}});return false},_mouseStop:function(b){var a=this;this.dragged=false;$(".coral-unselecting",this.element[0]).each(function(){var c=$.data(this,"selectable-item");c.$element.removeClass("coral-unselecting");c.unselecting=false;c.startselected=false;a._trigger("unselected",b,{unselected:c.element})});$(".coral-selecting",this.element[0]).each(function(){var c=$.data(this,"selectable-item");c.$element.removeClass("coral-selecting").addClass("coral-selected");
c.selecting=false;c.selected=true;c.startselected=true;a._trigger("selected",b,{selected:c.element})});this._trigger("stop",b);this.helper.remove();return false}})})();