mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-21 13:50:20 +08:00
1 line
2.2 KiB
JavaScript
1 line
2.2 KiB
JavaScript
define("ui/mode-button",["jquery"],function(t){"use strict";function e(t,e){return this.currModeIndex=0,this._init(t,e)}var o=function(t){return t&&t.__esModule?t:{default:t}}(t).default;e.prototype.DATA_KEY="mode-button",e.prototype.defaults={switchModesOnClick:!0},e.prototype._init=function(t,e){if(e=e||{},this.$element=o(t),this.options=o.extend(!0,{},this.defaults,e),!e.modes)throw new Error('ModeButton requires a "modes" array');var n=this;return this.$element.click(function(t){n.callModeFn(),n.options.switchModesOnClick&&n._incModeIndex(),o(this).html(n.options.modes[n.currModeIndex].html)}),this.reset()},e.prototype._incModeIndex=function(){return this.currModeIndex+=1,this.currModeIndex>=this.options.modes.length&&(this.currModeIndex=0),this},e.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)},e.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},e.prototype.currentMode=function(){return this.options.modes[this.currModeIndex]},e.prototype.current=function(){return this.currentMode().mode},e.prototype.getMode=function(t){return t?this.options.modes[this._getModeIndex(t)]:this.currentMode()},e.prototype.hasMode=function(t){try{return!!this.getMode(t)}catch(t){}return!1},e.prototype.setMode=function(t){return this._setModeByIndex(this._getModeIndex(t))},e.prototype.reset=function(){return this.currModeIndex=0,this.options.initialMode&&(this.currModeIndex=this._getModeIndex(this.options.initialMode)),this._setModeByIndex(this.currModeIndex)},e.prototype.callModeFn=function(t){var e=this.getMode(t).onclick;if(e&&o.type("function"===e))return e.call(this.$element.get(0))},o.fn.modeButton=function(t){if(!this.length)return this;if("object"===o.type(t))return this.map(function(){var n=o(this);return n.data("mode-button",new e(n,t)),this});var n=o(this[0]).data("mode-button");if(!n)throw new Error("modeButton needs an options object or string name of a function");if(n&&"string"===o.type(t)){var r=t;if(n&&"function"===o.type(n[r]))return n[r].apply(n,o.makeArray(arguments).slice(1))}return n}}); |