-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ui.dialog-lazy.js
161 lines (140 loc) · 4.47 KB
/
jquery.ui.dialog-lazy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// jquery.ui.dialog-lazy:
// Extend jquery.ui.dialog to provide a delayed loading or "lazy loading" functionality
// Copyright (C) 2013 Brian J. Dowling
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
(function($){
var delayedOptions = {title: true,
width: true,
appendTo: true, // XXXX Caveats?
height: true};
$.widget('ui.dialog', $.ui.dialog, {
version: "0.2",
options: {
href: "",
dialogLoaded: "",
loadingTitle: "",
loadingDialog: '<div style="text-align:center;"><strong>Fetching widgets ...</strong><br>'
+ '<br><img src="/new_images/loader2.gif"></div>',
},
_init: function() {
// if (this.options.href !== "") {
// this.options.autoOpen = false;
// }
// Do the same thing _super() would do, but call our open() instead, is this an $.widget inheritance considered a bug?
// if ( this.options.autoOpen ) {
// this.open();
// }
return this._super();
},
_create: function() {
if (this.options.href === "" || // Nothing to load
this._wasCreated === true) { // or it was loaded
return this._super();
} else {
return false;
}
},
// Next two methods are just to protect from calling parent early
// There are a host of other methods in ui.dialog that are unsafe, but this
// is one called from our persona plugin
_setOption: function (key, val) {
if (!this._wasCreated) { // && key in delayedOptions) {
return true;
} else {
return this._super(key,val);
}
},
refresh: function(e) {
if (this._wasCreated) {
this._setOptions(this.options); // Bit Overkill, Could this backfire?
// Alternatively have an array of keys that should be refreshed
// this._createButtons();
}
// return this._super(e);
},
_createButtons: function () { // Unsafe until loaded
if (this._wasCreated) {
return this._super();
}
},
button: function () { // Unsafe until loaded
if (this._wasCreated) {
return this._super();
}
},
open: function(e) {
if (this.options.href === "" || // Nothing to load
this._wasCreated === true) { // or it was loaded
return this._superApply(arguments);
} else {
this._openNext = true;
return this.loadDialog(e);
}
},
reload: function(e) {
this._openNext = true;
if (e)
this.refresh(e);
return this.loadDialog(e);
},
loadDialog: function(e) {
if (this._loader)
return;
if (this._openNext) {
this._loader = $(this.options.loadingDialog);
this._loader.attr('title', this.options.loadingTitle ||
this.options.title ?
(this.options.title + " (loading)") : "Loading ...");
this._loader.appendTo("body").hide().dialog({autoOpen: true, modal:true});
}
this._lastEvent = e;
$.ajax({
url: this.options.href,
method: 'GET'
})
.then($.proxy(this._loadDialog, this));
// function(a) { log(a); }, // XXXX
// function(a) { log(a); }); // XXXX
return true;
},
_loadDialog: function(data, textStatus, jqXHR) {
var e = this._lastEvent;
var element = this.element[0];
var div;
if (typeof data == "object" && data.html) { // JSON is possible
div = $(data.html);
} else { // otherwise assumed to be HTML
div = $(data);
}
$(element).hide().html($(div).html());
$.each(["class", "title", "name", "id"], function (i,a) {
$(element).attr(a, $(div).attr(a));
});
if (this._loader) {
this._loader.remove();
delete this._loader;
}
this._wasCreated = true;
this._create();
this._trigger('loaded', null, {'dialog': this});
if (this._openNext) {
this._openNext = false;
return this.open(e);
} else {
return this;
}
},
_openSuper: function() {
return $.ui.dialog.prototype.open.call( this );
},
});
})(jQuery);