Files
galaxy/static/scripts/ui/mode-button.js
T

3 lines
2.2 KiB
JavaScript

"use strict";!function(t){"function"==typeof define&&define.amd?define([],t):t(jQuery)}(function(){function t(t,e){return this.currModeIndex=0,this._init(t,e)}t.prototype.DATA_KEY="mode-button",t.prototype.defaults={switchModesOnClick:!0},t.prototype._init=function(t,e){if(e=e||{},this.$element=$(t),this.options=$.extend(!0,{},this.defaults,e),!e.modes)throw new Error('ModeButton requires a "modes" array');var o=this;return this.$element.click(function(t){o.callModeFn(),o.options.switchModesOnClick&&o._incModeIndex(),$(this).html(o.options.modes[o.currModeIndex].html)}),this.reset()},t.prototype._incModeIndex=function(){return this.currModeIndex+=1,this.currModeIndex>=this.options.modes.length&&(this.currModeIndex=0),this},t.prototype._getModeIndex=function(t){for(var e=0;e<this.options.modes.length;e+=1)if(this.options.modes[e].mode===t)return e;throw new Error("mode not found: "+t)},t.prototype._setModeByIndex=function(t){var e=this.options.modes[t];if(!e)throw new Error("mode index not found: "+t);return this.currModeIndex=t,e.html&&this.$element.html(e.html),this},t.prototype.currentMode=function(){return this.options.modes[this.currModeIndex]},t.prototype.current=function(){return this.currentMode().mode},t.prototype.getMode=function(t){return t?this.options.modes[this._getModeIndex(t)]:this.currentMode()},t.prototype.hasMode=function(t){try{return!!this.getMode(t)}catch(t){}return!1},t.prototype.setMode=function(t){return this._setModeByIndex(this._getModeIndex(t))},t.prototype.reset=function(){return this.currModeIndex=0,this.options.initialMode&&(this.currModeIndex=this._getModeIndex(this.options.initialMode)),this._setModeByIndex(this.currModeIndex)},t.prototype.callModeFn=function(t){var e=this.getMode(t).onclick;if(e&&$.type("function"===e))return e.call(this.$element.get(0))},$.fn.modeButton=function(e){if(!this.length)return this;if("object"===$.type(e))return this.map(function(){var o=$(this);return o.data("mode-button",new t(o,e)),this});var o=$(this[0]).data("mode-button");if(!o)throw new Error("modeButton needs an options object or string name of a function");if(o&&"string"===$.type(e)){var n=e;if(o&&"function"===$.type(o[n]))return o[n].apply(o,$.makeArray(arguments).slice(1))}return o}});
//# sourceMappingURL=../../maps/ui/mode-button.js.map