PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / assets / js / inspector.js
vikappointments / admin / assets / js Last commit date
adapter 2 days ago index.html 2 days ago inspector.js 2 days ago mediamanager.js 2 days ago vikappointments.js 2 days ago wizard.js 2 days ago
inspector.js
215 lines
1 /**
2 * jQuery add-on used to support a sidebar inspector.
3 * Here's a list of supported options.
4 *
5 * - title string The title to use for the inspector.
6 * - url string An optional URL to load the contents via AJAX.
7 *
8 * List of methods supported by the add-on.
9 *
10 * - open|show Manually displays the popup menu.
11 * - close|dismiss|hide Manually disposes the popup menu.
12 */
13 (function($) {
14 $.fn.inspector = function(method, data) {
15 // check if we should dismiss the inspector
16 if (typeof method === 'string' && method.match(/^(close|dismiss|hide)$/i)) {
17 if (!this.is(':visible')) {
18 // do not propagate error in case the inspector is currently closed
19 return this;
20 }
21
22 // trigger close event for subscribed listeners
23 var event = $.Event('inspector.close');
24 this.trigger(event);
25
26 // check if we should prevent closure in case a subscribed
27 // listener stopped the event propagation
28 if (event.isPropagationStopped() === true) {
29 // do not go ahead
30 return this;
31 }
32
33 if (typeof $.fn.select2 !== 'undefined') {
34 // immediately auto-dismiss any open Select2 dropdowns
35 this.find('select.select2-offscreen').select2('close');
36 }
37
38 // hide class to slide out the inspector (takes 300ms for completion)
39 this.removeClass('slide-in');
40
41 // turn off events
42 this.find('.inspector-head a.dismiss').off('click');
43 this.closest('.record-inspector-overlay').off('mousedown');
44
45 // hide overlay once the inspector has disappeared
46 setTimeout(() => {
47 this.closest('.record-inspector-overlay').hide();
48
49 // restore body scroll once all the overlays are hidden
50 if ($('.record-inspector-overlay:visible').length == 0) {
51 $('body').css('overflow', 'auto');
52 }
53 }, 300);
54
55 return this;
56 }
57
58 if (typeof data === 'string') {
59 // URL was passed
60 data = {url: data};
61 } else if (typeof data !== 'object') {
62 // use an empty object in case the second argument is not an object
63 data = {};
64 }
65
66 // inject received parameters within the event to dispatch
67 var event = $.Event('inspector.show');
68 event.params = data;
69
70 // fallback to opening method
71 this.trigger(event);
72
73 // check if we should prevent opening in case a subscribed
74 // listener stopped the event propagation
75 if (event.isPropagationStopped() === true) {
76 // do not go ahead
77 return this;
78 }
79
80 if (data.title) {
81 this.find('.inspector-head h3').html(data.title);
82 }
83
84 var self = this;
85
86 // find close button
87 var closeButton = this.find('.inspector-head a.dismiss');
88 // if exists, register click event to dismiss the inspector
89 if (closeButton.length) {
90 closeButton.on('click', (event) => {
91 self.inspector('dismiss');
92 });
93 }
94
95 // in case of close button or ESC key, make the overlay clickable
96 if (closeButton.length || this.data('esc') == 1) {
97 this.closest('.record-inspector-overlay').on('mousedown', function(event) {
98 // close inspector only in case the clicked element was exactly the overlay
99 if ($(event.originalEvent.srcElement).is($(this))) {
100 self.inspector('dismiss');
101 }
102 });
103 }
104
105 // show the overlay
106 this.closest('.record-inspector-overlay').show();
107
108 // prevent body from being scrolled
109 $('body').css('overflow', 'hidden');
110
111 // slide in the inspector after a few milliseconds
112 setTimeout(() => {
113 this.addClass('slide-in');
114 }, 32);
115
116 // create promise
117 return new Promise((resolve, reject) => {
118 // in case a URL was passed, recover contents via AJAX with the given HREF
119 if (data.url) {
120 // add loading HTML to inspector body
121 this.find('.inspector-body').html('<div class="inspectory-body-loading"></div>');
122
123 UIAjax.do(
124 // reach the specified end-point
125 data.url,
126 // do not use POST data
127 {},
128 // handle successful response
129 function(resp) {
130 // try to decode JSON response
131 try {
132 resp = JSON.parse(resp);
133
134 if (Array.isArray(resp)) {
135 // extract HTML from array
136 resp = resp.shift();
137 }
138 } catch (err) {
139 // no JSON, plain HTML was returned
140 }
141
142 // push HTML within the body inspector
143 this.find('.inspector-body').html(resp);
144
145 // resolve promise
146 resolve();
147 },
148 // handle error
149 function(error) {
150 // reject promise
151 reject(error);
152 }
153 );
154 }
155 // otherwise immediately resolve the promise
156 else {
157 resolve();
158 }
159 }).then(() => {
160 // inject received parameters within the event to dispatch
161 var event = $.Event('inspector.aftershow');
162 event.params = data;
163
164 // trigger event once the inspector is already visible
165 this.trigger(event);
166
167 }).catch((error, inspector) => {
168 // remove loading HTML from inspector body
169 this.find('.inspector-body').html('');
170
171 // display error
172 if (!error.responseText) {
173 // use default connection lost error
174 error.responseText = Joomla.JText._('VAPCONNECTIONLOSTERROR');
175 }
176
177 setTimeout(() => {
178 // wait some milliseconds to let the process clears the
179 // loading box before showing the alert
180 alert(error.responseText);
181
182 // then auto-close inspector
183 this.inspector('dismiss');
184 }, 32);
185
186 });
187
188 return this;
189 }
190 })(jQuery);
191
192 /**
193 * Shortcut to open the inspector.
194 *
195 * @param string id The id attribute of the inspector.
196 * @param mixed data The inspector configuration.
197 *
198 * @return mixed The inspector.
199 */
200 function vapOpenInspector(id, data) {
201 return jQuery('.record-inspector#' + id).inspector('show', data);
202 }
203
204 /**
205 * Shortcut to close the inspector.
206 *
207 * @param string id The id attribute of the inspector.
208 *
209 * @return mixed The inspector.
210 */
211 function vapCloseInspector(id) {
212 // shortcut to close the inspector
213 return jQuery('.record-inspector#' + id).inspector('dismiss');
214 }
215