AdminLTE/build/js/BoxRefresh.js

120 lines
3 KiB
JavaScript
Raw Normal View History

2017-02-25 20:38:05 +01:00
/* BoxRefresh()
* =========
* Adds AJAX content control to a box.
*
* @Usage: $('#my-box').boxRefresh(options)
* or add [data-widget="box-refresh"] to the box element
* Pass any option as data-option="value"
*/
+function ($) {
2017-10-26 22:53:11 +02:00
'use strict';
2017-02-25 20:38:05 +01:00
2017-10-26 22:53:11 +02:00
var DataKey = 'lte.boxrefresh';
2017-02-25 20:38:05 +01:00
var Default = {
source : '',
params : {},
trigger : '.refresh-btn',
content : '.box-body',
loadInContent : true,
responseType : '',
overlayTemplate: '<div class="overlay"><div class="fa fa-refresh fa-spin"></div></div>',
onLoadStart : function () {
},
onLoadDone : function (response) {
2017-10-26 22:53:11 +02:00
return response;
2017-02-25 20:38:05 +01:00
}
2017-10-26 22:53:11 +02:00
};
2017-02-25 20:38:05 +01:00
var Selector = {
data: '[data-widget="box-refresh"]'
2017-10-26 22:53:11 +02:00
};
2017-02-25 20:38:05 +01:00
// BoxRefresh Class Definition
// =========================
var BoxRefresh = function (element, options) {
2017-10-26 22:53:11 +02:00
this.element = element;
this.options = options;
this.$overlay = $(options.overlayTemplate);
2017-02-25 20:38:05 +01:00
if (options.source === '') {
2017-10-26 22:53:11 +02:00
throw new Error('Source url was not defined. Please specify a url in your BoxRefresh source option.');
2017-02-25 20:38:05 +01:00
}
2017-10-26 22:53:11 +02:00
this._setUpListeners();
this.load();
};
2017-02-25 20:38:05 +01:00
BoxRefresh.prototype.load = function () {
2017-10-26 22:53:11 +02:00
this._addOverlay();
this.options.onLoadStart.call($(this));
2017-02-25 20:38:05 +01:00
$.get(this.options.source, this.options.params, function (response) {
if (this.options.loadInContent) {
$(this.element).find(this.options.content).html(response);
2017-02-25 20:38:05 +01:00
}
2017-10-26 22:53:11 +02:00
this.options.onLoadDone.call($(this), response);
this._removeOverlay();
}.bind(this), this.options.responseType !== '' && this.options.responseType);
};
2017-02-25 20:38:05 +01:00
// Private
BoxRefresh.prototype._setUpListeners = function () {
$(this.element).on('click', this.options.trigger, function (event) {
2017-10-26 22:53:11 +02:00
if (event) event.preventDefault();
this.load();
}.bind(this));
};
2017-02-25 20:38:05 +01:00
BoxRefresh.prototype._addOverlay = function () {
2017-10-26 22:53:11 +02:00
$(this.element).append(this.$overlay);
};
2017-02-25 20:38:05 +01:00
BoxRefresh.prototype._removeOverlay = function () {
$(this.$overlay).remove();
2017-10-26 22:53:11 +02:00
};
2017-02-25 20:38:05 +01:00
// Plugin Definition
// =================
function Plugin(option) {
return this.each(function () {
2017-10-26 22:53:11 +02:00
var $this = $(this);
var data = $this.data(DataKey);
2017-02-25 20:38:05 +01:00
if (!data) {
2017-10-26 22:53:11 +02:00
var options = $.extend({}, Default, $this.data(), typeof option == 'object' && option);
$this.data(DataKey, (data = new BoxRefresh($this, options)));
2017-02-25 20:38:05 +01:00
}
if (typeof data == 'string') {
if (typeof data[option] == 'undefined') {
2017-10-26 22:53:11 +02:00
throw new Error('No method named ' + option);
2017-02-25 20:38:05 +01:00
}
2017-10-26 22:53:11 +02:00
data[option]();
2017-02-25 20:38:05 +01:00
}
2017-10-26 22:53:11 +02:00
});
2017-02-25 20:38:05 +01:00
}
2017-10-26 22:53:11 +02:00
var old = $.fn.boxRefresh;
2017-02-25 20:38:05 +01:00
2017-10-26 22:53:11 +02:00
$.fn.boxRefresh = Plugin;
$.fn.boxRefresh.Constructor = BoxRefresh;
2017-02-25 20:38:05 +01:00
// No Conflict Mode
// ================
$.fn.boxRefresh.noConflict = function () {
2017-10-26 22:53:11 +02:00
$.fn.boxRefresh = old;
return this;
};
2017-02-25 20:38:05 +01:00
// BoxRefresh Data API
// =================
$(window).on('load', function () {
$(Selector.data).each(function () {
2017-10-26 22:53:11 +02:00
Plugin.call($(this));
});
});
2017-02-25 20:38:05 +01:00
2017-10-26 22:53:11 +02:00
}(jQuery);