Skip to content

Commit 65e4c94

Browse files
author
mesut talebi
committed
Added Lazyload to Collapse, Added MTLazyloader.OpenModal function to open a modal on demand
1 parent 3c3322d commit 65e4c94

File tree

2 files changed

+134
-3
lines changed

2 files changed

+134
-3
lines changed

Scripts/MT.BootstrapLazyloader.js

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Nuget Package: MT.BootstrapLazyLoader.js
3-
Version: 1.2.4
3+
Version: 1.3.0
44
55
Created By: Mesut Talebi (mesut.talebi@gmail.com)
66
@@ -10,11 +10,110 @@ Thanks To ChrisDerrig: Added lazy loading feature to bootstrap pills and callbac
1010
1111
*/
1212

13+
var MTLazyloader = function () {
14+
return {
15+
/**
16+
* Create and Open a Modal Window
17+
* @param {string} url the url to get using ajax
18+
* @param {string} header the modal header
19+
* @param {string} size the size of the modal can be one of (modal-lg, modal-sm, ...)
20+
* @param {string} modalId the id for modal
21+
* @param {string} contentId the id for modal content
22+
* @param {bool} parDisableClose the value that indicates if enable the close button and esc button
23+
* @param {function} parCallback the callback function to run after modal opened
24+
* @returns {void}
25+
*/
26+
OpenModal: function (url, header, size, modalId, contentId, parDisableClose, parCallback) {
27+
var loader = '<i class="fa fa-spin fa-spinner fa-lg text-info modal-loader"></i>';
28+
29+
//Get Id Of Current Handler to set as Modal Id, the id of modal will be #HandlerId + Modal
30+
var currHandleId = modalId;
31+
32+
if (currHandleId == undefined || currHandleId == null) {
33+
currHandleId = "LazyloadModal";
34+
} else {
35+
currHandleId += "Modal";
36+
}
37+
38+
var disableClose = parDisableClose;
39+
40+
var modalContentId = '#LazyloadModalContent';
41+
if (contentId != undefined && contentId != null) {
42+
modalContentId = "#" + contentId;
43+
}
44+
45+
46+
var modalHtml = "<!--Lazyloaded Modal -->" +
47+
"<div class='modal fade' id='" + currHandleId + "' role='dialog' aria-labelledby='" + currHandleId + "'>" +
48+
"<div class='modal-dialog' role='document'>" +
49+
"<div class='modal-content'>" +
50+
"<div class='modal-header'>";
51+
52+
if (disableClose == undefined || disableClose === 'false' || disableClose == null) {
53+
modalHtml += "<button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>";
54+
}
55+
56+
modalHtml += "<h4 class='modal-title' id='LazyloadModalLabel'>Modal Header</h4>" +
57+
"</div>" +
58+
"<div id='" + modalContentId.substr(1) + "'></div>" +
59+
"</div>" +
60+
"</div>" +
61+
"</div>";
62+
63+
64+
65+
var modalHeader = header;
66+
var modalClass = size;
67+
var callback = parCallback;
68+
69+
70+
71+
var modal = $('#' + currHandleId);
72+
if (modal.length == 0) {
73+
$('body').append(modalHtml);
74+
75+
modal = $('#' + currHandleId);
76+
}
77+
else {
78+
$('body').append(modal);
79+
}
80+
81+
$(modal).find('.modal-header .modal-title').html(modalHeader);
82+
$(modal).find('.modal-dialog').removeAttr("class").addClass('modal-dialog').addClass(modalClass);
83+
84+
//Ajax Get
85+
$(modal).find(modalContentId).html('');
86+
//$(currHandle).append(loader);
87+
$.get(url, function (data) {
88+
$(modal).find(modalContentId).html(data);
89+
if (disableClose == undefined) {
90+
$(modal).modal('show');
91+
}
92+
else {
93+
$(modal).modal({
94+
keyboard: false,
95+
backdrop: 'static'
96+
});
97+
}
98+
}).fail(function () {
99+
var alertDiv = '<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> Error!!!</div>';
100+
$(modal).find(modalContentId).html(alertDiv);
101+
$(modal).modal('show');
102+
}).done(function () {
103+
if (callback) {
104+
eval(callback);
105+
}
106+
}).always(function () {
107+
//$(currHandle).find('.modal-loader').remove();
108+
});
109+
}
110+
}
111+
}();
13112

14113
$(function () {
15114
$(document).on('shown.bs.tab', '.nav-tabs.lazyload a[data-toggle="tab"]:not(.loaded)', function (e) {
16115
var loader = '<div class="text-center"><i class="fa fa-spin fa-spinner fa-2x text-muted"></i></div>';
17-
116+
18117
var pane = $(e.target).attr('href');
19118

20119
var url = $(e.target).data('url');
@@ -155,4 +254,36 @@ $(function () {
155254
$(currHandle).find('.modal-loader').remove();
156255
});
157256
});
257+
258+
//
259+
//Lazyload for collapse
260+
$(document).on('shown.bs.collapse', '.lazyload.collapse:not(.loaded)', function (e) {
261+
var loader = '<div class="text-center"><i class="fa fa-spin fa-spinner fa-2x text-muted"></i></div>';
262+
263+
var pane = $(e.target);
264+
265+
var url = $(e.target).data('url');
266+
267+
var callback = $(e.target).data('callback');
268+
269+
270+
if (url) {
271+
$(pane).html(loader);
272+
273+
var caller = $(this);
274+
$.get(url, function (data) {
275+
$(pane).html(data);
276+
$(caller).addClass('loaded');
277+
})
278+
.fail(function () {
279+
var alertDiv = '<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> Error!!!</div>';
280+
$(pane).html(alertDiv);
281+
})
282+
.done(function () {
283+
if (callback) {
284+
eval(callback);
285+
}
286+
});
287+
}
288+
});
158289
});

Scripts/MT.BootstrapLazyloader.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)