Skip to content

Commit 0a54b23

Browse files
committed
support a bootstrap actionable area with ajax management lifecycle through the expose of jquery plugin BootstrapActionable
1 parent b940166 commit 0a54b23

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

js/bootstrap-actionable.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
function BootstrapActionable() {
2929
this.options = $.extend({}, defaults);
3030
this.init();
31+
this.registerJQueryPlugin();
3132
}
3233

3334
BootstrapActionable.prototype.init = function () {
@@ -73,5 +74,97 @@
7374
}
7475
});
7576
};
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+
76169
return new BootstrapActionable();
77170
}));

0 commit comments

Comments
 (0)