|
28 | 28 | function BootstrapActionable() { |
29 | 29 | this.options = $.extend({}, defaults); |
30 | 30 | this.init(); |
| 31 | + this.registerJQueryPlugin(); |
31 | 32 | } |
32 | 33 |
|
33 | 34 | BootstrapActionable.prototype.init = function () { |
|
73 | 74 | } |
74 | 75 | }); |
75 | 76 | }; |
| 77 | + |
| 78 | + BootstrapActionable.prototype.registerJQueryPlugin = function () { |
| 79 | + $.fn.BootstrapActionable = function (options) { |
| 80 | + function BootstrapActionable(element, defaults) { |
| 81 | + this.options = $.extend(true, {}, { |
| 82 | + outputElement: element, |
| 83 | + containerRemoveEventName: "containerRemoveEventName", |
| 84 | + containerReadyEventName: "containerReadyEventName", |
| 85 | + ajaxBeforeSend: null, |
| 86 | + ajaxSuccess: function (response, textStatus, jqXHR) { |
| 87 | + var $that = this; |
| 88 | + $($that.outputElement).html(response).promise().done(function () { |
| 89 | + setTimeout(function () { |
| 90 | + $($that.outputElement).trigger(javatmp.settings.javaTmpAjaxContainerReady); |
| 91 | + }, 0); |
| 92 | + }); |
| 93 | + }, |
| 94 | + ajaxError: function (jqXHR, textStatus, errorThrown) { |
| 95 | + $(this.outputElement).html("Error Loading Request !!!"); |
| 96 | + } |
| 97 | + }, defaults); |
| 98 | + } |
| 99 | + |
| 100 | + var populate = function (localOptions) { |
| 101 | + var $this = localOptions.linkElement; |
| 102 | + var event = localOptions.linkEvent; |
| 103 | + event.preventDefault(); |
| 104 | + var javaTmpRemoveEvent = $.Event(localOptions.containerRemoveEventName, {_newTarget: $this}); |
| 105 | + $(localOptions.outputElement).trigger(javaTmpRemoveEvent).promise().done(function () { |
| 106 | + if (!javaTmpRemoveEvent.isDefaultPrevented()) { |
| 107 | + $(localOptions.outputElement).off(localOptions.containerReadyEventName).promise().done(function () { |
| 108 | + $(localOptions.outputElement).off(localOptions.containerRemoveEventName).promise().done(function () { |
| 109 | + $(localOptions.outputElement).off(javaTmpRemoveEvent).promise().done(function () { |
| 110 | + $.ajax({ |
| 111 | + type: javatmp.settings.httpMethod, |
| 112 | + async: true, |
| 113 | + cache: true, |
| 114 | + dataType: javatmp.settings.dataType, |
| 115 | + url: $this.attr("href"), |
| 116 | + data: javatmp.settings.defaultPassData, |
| 117 | + beforeSend: function (jqXHR, settings) { |
| 118 | + if ($.isFunction(localOptions.ajaxBeforeSend)) { |
| 119 | + return localOptions.ajaxBeforeSend.call(localOptions, jqXHR, settings); |
| 120 | + } |
| 121 | + }, |
| 122 | + success: function (response, textStatus, jqXHR) { |
| 123 | + if ($.isFunction(localOptions.ajaxSuccess)) { |
| 124 | + return localOptions.ajaxSuccess.call(localOptions, response, textStatus, jqXHR); |
| 125 | + } |
| 126 | + }, |
| 127 | + error: function (jqXHR, textStatus, errorThrown) { |
| 128 | + if ($.isFunction(localOptions.ajaxError)) { |
| 129 | + return localOptions.ajaxError.call(localOptions, jqXHR, textStatus, errorThrown); |
| 130 | + } |
| 131 | + } |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | + }); |
| 136 | + } |
| 137 | + }); |
| 138 | + }; |
| 139 | + |
| 140 | + BootstrapActionable.prototype.populateByLinkEvent = function (populateOptions) { |
| 141 | + var localOptions = $.extend(true, {}, this.options, populateOptions); |
| 142 | + populate(localOptions); |
| 143 | + }; |
| 144 | + //expose methods |
| 145 | + if (typeof options === 'string') { |
| 146 | + var args = Array.prototype.slice.call(arguments, 1); |
| 147 | + if ($.isFunction(this.data('BootstrapActionable')[options])) { |
| 148 | + return this.data('BootstrapActionable')[options].apply(this.data('BootstrapActionable'), args); |
| 149 | + } else { |
| 150 | + throw new Error("Function [" + options + "] does not found in plugin BootstrapActionable"); |
| 151 | + } |
| 152 | + } |
| 153 | + return this.each(function (index) { |
| 154 | + var element = $(this); |
| 155 | + |
| 156 | + // Return early if this element already has a plugin instance |
| 157 | + if (element.data('BootstrapActionable')) |
| 158 | + return element.data('BootstrapActionable'); |
| 159 | + // pass options to plugin constructor |
| 160 | + var bootstrapActionableInstance = new BootstrapActionable(element, options); |
| 161 | + // Store plugin object in this element's data |
| 162 | + element.data('BootstrapActionable', bootstrapActionableInstance); |
| 163 | + return bootstrapActionableInstance; |
| 164 | + }); |
| 165 | + } |
| 166 | + ; |
| 167 | + }; |
| 168 | + |
76 | 169 | return new BootstrapActionable(); |
77 | 170 | })); |
0 commit comments