EnVisageOnline/Main/Source/EnVisage/Scripts/Plugins/ButtonLocker.js

129 lines
3.3 KiB
JavaScript

/* ===========================================================
* ButtonLocker.js v0.0.1
* ===========================================================
* Copyright 2017 Prevu
* ========================================================== */
(function ($) {
"use strict"; // jshint ;_;
/* ButtonLocker CLASS DEFINITION
* ====================== */
var ButtonLocker = function (element, options) {
this.init(element, options);
};
ButtonLocker.prototype = {
constructor: ButtonLocker,
init: function (element, options) {
var plugin = this;
this.options = options;
this.$container = $(element);
this.$isLocked = false;
var tagName = this.$container.prop('tagName');
if ((tagName != 'BUTTON') && (tagName != 'A' || !this.$container.hasClass('btn'))) {
throw "Widget must be created for buttons";
}
this.$container.on('click.buttonlocker', function (event) {
var result = plugin.buttonClick(event);
return result;
});
if (this.options.debug) {
console.log('ButtonLocker: initialized');
}
if (this.options.initCallback && (typeof this.options.initCallback === 'function')) {
this.options.initCallback();
}
},
buttonClick: function (event) {
if (this.$isLocked)
return;
this.disable();
var plugin = this;
var clickResult = null;
try {
if (this.options.click && (typeof this.options.click === 'function')) {
event.stopPropagation();
clickResult = this.options.click(this.$container, this.options.params);
if (clickResult != null)
return clickResult;
}
}
finally {
if (plugin.options.timeout) {
setTimeout(function () {
plugin.enable();
}, plugin.options.timeout);
}
}
},
enable: function () {
if (this.$container.hasClass('disabled')) {
this.$container.removeClass('disabled');
}
this.$isLocked = false;
if (this.options.debug) {
console.log('ButtonLocker: button unlocked');
}
},
disable: function () {
this.$isLocked = true;
if (!this.$container.hasClass('disabled')) {
this.$container.addClass('disabled');
}
if (this.options.debug) {
console.log('ButtonLocker: button locked');
}
},
isDisabled: function () {
return this.$isLocked;
},
destroy: function () {
var e = $.Event('destroy');
this.$container.trigger(e);
if (e.isDefaultPrevented())
return;
this.$container.off('click.buttonlocker', this.buttonClick);
if (this.options.debug) {
console.log('ButtonLocker: destroyed');
}
},
};
/* BUTTON LOCKER PLUGIN DEFINITION
* ======================= */
$.fn.buttonLocker = function (option, args) {
return this.each(function () {
var $this = $(this),
data = $this.data('buttonLocker'),
options = $.extend({}, $.fn.buttonLocker.defaults, $this.data(), typeof option == 'object' && option);
if (!data) $this.data('buttonLocker', (data = new ButtonLocker(this, options)));
if (typeof option == 'string')
data[option].apply(data, [].concat(args));
});
};
$.fn.buttonLocker.defaults = {
click: null,
params: {}, // These params passed to click function
timeout: 300, // if null, false or 0 then plugin does not enable itself after timeout
initCallback: null,
debug: false,
};
$.fn.buttonLocker.Constructor = ButtonLocker;
}(jQuery));