UNPKG

24 kBJavaScriptView Raw
1/*!
2 * ModalPrompt v2.0.0-alpha.1 (https://github.com/quark-dev/Phonon-Framework)
3 * Copyright 2015-2019 qathom
4 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
5 */
6'use strict';
7
8function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
9
10var Util = _interopDefault(require('../util.js'));
11
12/*! *****************************************************************************
13Copyright (c) Microsoft Corporation. All rights reserved.
14Licensed under the Apache License, Version 2.0 (the "License"); you may not use
15this file except in compliance with the License. You may obtain a copy of the
16License at http://www.apache.org/licenses/LICENSE-2.0
17
18THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
20WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
21MERCHANTABLITY OR NON-INFRINGEMENT.
22
23See the Apache Version 2.0 License for specific language governing permissions
24and limitations under the License.
25***************************************************************************** */
26/* global Reflect, Promise */
27
28var extendStatics = function(d, b) {
29 extendStatics = Object.setPrototypeOf ||
30 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
31 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
32 return extendStatics(d, b);
33};
34
35function __extends(d, b) {
36 extendStatics(d, b);
37 function __() { this.constructor = d; }
38 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
39}
40
41function __awaiter(thisArg, _arguments, P, generator) {
42 return new (P || (P = Promise))(function (resolve, reject) {
43 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
44 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
45 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
46 step((generator = generator.apply(thisArg, _arguments || [])).next());
47 });
48}
49
50function __generator(thisArg, body) {
51 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
52 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
53 function verb(n) { return function (v) { return step([n, v]); }; }
54 function step(op) {
55 if (f) throw new TypeError("Generator is already executing.");
56 while (_) try {
57 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
58 if (y = 0, t) op = [op[0] & 2, t.value];
59 switch (op[0]) {
60 case 0: case 1: t = op; break;
61 case 4: _.label++; return { value: op[1], done: false };
62 case 5: _.label++; y = op[1]; op = [0]; continue;
63 case 7: op = _.ops.pop(); _.trys.pop(); continue;
64 default:
65 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
66 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
67 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
68 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
69 if (t[2]) _.ops.pop();
70 _.trys.pop(); continue;
71 }
72 op = body.call(thisArg, _);
73 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
74 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
75 }
76}
77
78var Component = (function () {
79 function Component(name, defaultProps, props) {
80 var _this = this;
81 this.template = '';
82 this.id = null;
83 this.eventHandlers = [];
84 this.registeredElements = [];
85 this.name = name;
86 var element = typeof props.element === 'string'
87 ? document.querySelector(props.element) : props.element;
88 var config = {};
89 if (element) {
90 var dataConfig = Util.Selector.attrConfig(element);
91 if (dataConfig) {
92 config = dataConfig;
93 }
94 }
95 this.defaultProps = defaultProps;
96 this.props = Object.assign(defaultProps, config, props, { element: element });
97 this.id = this.uid();
98 this.elementListener = function (event) { return _this.onBeforeElementEvent(event); };
99 this.setEventsHandler();
100 }
101 Component.prototype.setTemplate = function (template) {
102 this.template = template;
103 };
104 Component.prototype.getTemplate = function () {
105 return this.template;
106 };
107 Component.prototype.getElement = function () {
108 return this.getProp('element') || null;
109 };
110 Component.prototype.setElement = function (element) {
111 this.props.element = element;
112 };
113 Component.prototype.getId = function () {
114 return this.id;
115 };
116 Component.prototype.uid = function () {
117 return Math.random().toString(36).substr(2, 10);
118 };
119 Component.prototype.getName = function () {
120 return this.name;
121 };
122 Component.prototype.getProps = function () {
123 return this.props;
124 };
125 Component.prototype.getProp = function (name) {
126 var defaultValue = this.defaultProps[name];
127 return typeof this.props[name] !== 'undefined' ? this.props[name] : defaultValue;
128 };
129 Component.prototype.setProps = function (props) {
130 var componentProps = Object.assign({}, props);
131 this.props = Object.assign(this.props, componentProps);
132 };
133 Component.prototype.setProp = function (name, value) {
134 if (typeof this.props[name] === 'undefined') {
135 throw new Error('Cannot set an invalid prop');
136 }
137 this.props[name] = value;
138 };
139 Component.prototype.registerElements = function (elements) {
140 var _this = this;
141 elements.forEach(function (element) { return _this.registerElement(element); });
142 };
143 Component.prototype.registerElement = function (element) {
144 element.target.addEventListener(element.event, this.elementListener);
145 this.registeredElements.push(element);
146 };
147 Component.prototype.unregisterElements = function () {
148 var _this = this;
149 this.registeredElements.forEach(function (element) {
150 _this.unregisterElement(element);
151 });
152 };
153 Component.prototype.unregisterElement = function (element) {
154 var registeredElementIndex = this.registeredElements
155 .findIndex(function (el) { return el.target === element.target && el.event === element.event; });
156 if (registeredElementIndex > -1) {
157 element.target.removeEventListener(element.event, this.elementListener);
158 this.registeredElements.splice(registeredElementIndex, 1);
159 }
160 else {
161 console.error('Warning! Could not remove element:'
162 + ' ' + (element.target + " with event: " + element.event + "."));
163 }
164 };
165 Component.prototype.triggerEvent = function (eventName, detail, objectEventOnly) {
166 var _this = this;
167 if (detail === void 0) { detail = {}; }
168 if (objectEventOnly === void 0) { objectEventOnly = false; }
169 var eventNameObject = eventName.split('.').reduce(function (acc, current, index) {
170 if (index === 0) {
171 return current;
172 }
173 return acc + current.charAt(0).toUpperCase() + current.slice(1);
174 });
175 var eventNameAlias = "on" + eventNameObject
176 .charAt(0).toUpperCase() + eventNameObject.slice(1);
177 var props = this.getProps();
178 this.eventHandlers.forEach(function (scope) {
179 if (typeof scope[eventNameObject] === 'function') {
180 scope[eventNameObject].apply(_this, [detail]);
181 }
182 if (typeof scope[eventNameAlias] === 'function') {
183 props[eventNameAlias].apply(_this, [detail]);
184 }
185 });
186 if (objectEventOnly) {
187 return;
188 }
189 var element = this.getElement();
190 if (element) {
191 Util.Dispatch.elementEvent(element, eventName, this.name, detail);
192 }
193 else {
194 Util.Dispatch.winDocEvent(eventName, this.name, detail);
195 }
196 };
197 Component.prototype.preventClosable = function () {
198 return false;
199 };
200 Component.prototype.destroy = function () {
201 this.unregisterElements();
202 };
203 Component.prototype.onElementEvent = function (event) {
204 };
205 Component.prototype.setEventsHandler = function () {
206 var props = this.getProps();
207 var scope = Object.keys(props).reduce(function (cur, key) {
208 if (typeof props[key] === 'function') {
209 cur[key] = props[key];
210 }
211 return cur;
212 }, {});
213 if (Object.keys(scope).length > 0) {
214 this.eventHandlers.push(scope);
215 }
216 };
217 Component.prototype.onBeforeElementEvent = function (event) {
218 if (this.preventClosable()) {
219 return;
220 }
221 this.onElementEvent(event);
222 };
223 return Component;
224}());
225
226var Modal = (function (_super) {
227 __extends(Modal, _super);
228 function Modal(props, autoCreate) {
229 if (autoCreate === void 0) { autoCreate = true; }
230 var _this = _super.call(this, 'modal', {
231 title: null,
232 message: null,
233 cancelable: true,
234 background: null,
235 cancelableKeyCodes: [
236 27,
237 13,
238 ],
239 buttons: [
240 { event: 'confirm', text: 'Ok', dismiss: true, class: 'btn btn-primary' },
241 ],
242 center: true,
243 }, props) || this;
244 _this.backdropSelector = 'modal-backdrop';
245 _this.elementGenerated = false;
246 _this.setTemplate(''
247 + '<div class="modal" tabindex="-1" role="modal" data-no-boot>'
248 + '<div class="modal-inner" role="document">'
249 + '<div class="modal-content">'
250 + '<div class="modal-header">'
251 + '<h5 class="modal-title"></h5>'
252 + '<button type="button" class="icon-close" data-dismiss="modal" aria-label="Close">'
253 + '<span class="icon" aria-hidden="true"></span>'
254 + '</button>'
255 + '</div>'
256 + '<div class="modal-body">'
257 + '<p></p>'
258 + '</div>'
259 + '<div class="modal-footer">'
260 + '</div>'
261 + '</div>'
262 + '</div>'
263 + '</div>');
264 if (autoCreate && _this.getElement() === null) {
265 _this.build();
266 }
267 return _this;
268 }
269 Modal.attachDOM = function () {
270 var className = 'modal';
271 Util.Observer.subscribe({
272 componentClass: className,
273 onAdded: function (element, create) {
274 create(new Modal({ element: element }));
275 },
276 onRemoved: function (element, remove) {
277 remove('Modal', element);
278 },
279 });
280 document.addEventListener(Util.Event.CLICK, function (event) {
281 var target = event.target;
282 if (!target) {
283 return;
284 }
285 var toggleEl = Util.Selector.closest(target, "[data-toggle=\"" + className + "\"]");
286 if (toggleEl) {
287 var selector = toggleEl.getAttribute('data-target');
288 if (!selector) {
289 return;
290 }
291 var modal = document.querySelector(selector);
292 if (!modal) {
293 return;
294 }
295 var modalComponent = Util.Observer.getComponent(className, { element: modal });
296 if (!modalComponent) {
297 return;
298 }
299 target.blur();
300 modalComponent.show();
301 }
302 });
303 };
304 Modal.prototype.build = function () {
305 var _this = this;
306 this.elementGenerated = true;
307 var builder = document.createElement('div');
308 builder.innerHTML = this.getTemplate();
309 this.setElement(builder.firstChild);
310 var element = this.getElement();
311 var title = this.getProp('title');
312 if (title !== null) {
313 element.querySelector('.modal-title').innerHTML = title;
314 }
315 var message = this.getProp('message');
316 if (message !== null) {
317 element.querySelector('.modal-body').firstChild.innerHTML = message;
318 }
319 else {
320 this.removeTextBody();
321 }
322 var cancelable = this.getProp('cancelable');
323 if (!cancelable) {
324 element.querySelector('.close').style.display = 'none';
325 }
326 var buttons = this.getProp('buttons');
327 if (Array.isArray(buttons) && buttons.length > 0) {
328 buttons.forEach(function (button) {
329 element.querySelector('.modal-footer').appendChild(_this.buildButton(button));
330 });
331 }
332 else {
333 this.removeFooter();
334 }
335 document.body.appendChild(element);
336 };
337 Modal.prototype.show = function () {
338 var _this = this;
339 var element = this.getElement();
340 if (element === null) {
341 this.build();
342 }
343 if (element.classList.contains('show')) {
344 return false;
345 }
346 document.body.style.overflow = 'hidden';
347 (function () { return __awaiter(_this, void 0, void 0, function () {
348 var onShown;
349 var _this = this;
350 return __generator(this, function (_a) {
351 switch (_a.label) {
352 case 0: return [4, Util.sleep(20)];
353 case 1:
354 _a.sent();
355 this.triggerEvent(Util.Event.SHOW);
356 this.buildBackdrop();
357 this.attachEvents();
358 onShown = function () {
359 _this.triggerEvent(Util.Event.SHOWN);
360 element.removeEventListener(Util.Event.TRANSITION_END, onShown);
361 };
362 element.addEventListener(Util.Event.TRANSITION_END, onShown);
363 if (this.getProp('center')) {
364 this.center();
365 }
366 element.classList.add('show');
367 return [2];
368 }
369 });
370 }); })();
371 return true;
372 };
373 Modal.prototype.hide = function () {
374 var _this = this;
375 var element = this.getElement();
376 if (!element.classList.contains('show')) {
377 return false;
378 }
379 document.body.style.overflow = 'visible';
380 this.triggerEvent(Util.Event.HIDE);
381 this.detachEvents();
382 element.classList.add('hide');
383 element.classList.remove('show');
384 var backdrop = this.getBackdrop();
385 var onHidden = function () {
386 if (backdrop) {
387 document.body.removeChild(backdrop);
388 backdrop.removeEventListener(Util.Event.TRANSITION_END, onHidden);
389 }
390 element.classList.remove('hide');
391 _this.triggerEvent(Util.Event.HIDDEN);
392 if (_this.elementGenerated) {
393 document.body.removeChild(element);
394 }
395 };
396 if (backdrop) {
397 backdrop.addEventListener(Util.Event.TRANSITION_END, onHidden);
398 backdrop.classList.add('fadeout');
399 }
400 return true;
401 };
402 Modal.prototype.onElementEvent = function (event) {
403 if (event.type === 'keyup') {
404 var keycodes = this.getProp('cancelableKeyCodes');
405 if (keycodes.find(function (k) { return k === event.keyCode; })) {
406 this.hide();
407 }
408 return;
409 }
410 if (event.type === Util.Event.START) {
411 this.hide();
412 return;
413 }
414 if (event.type === Util.Event.CLICK) {
415 var target = event.target;
416 var eventName = target.getAttribute('data-event');
417 if (eventName) {
418 this.triggerEvent(eventName);
419 }
420 var dismissButton = Util.Selector.closest(target, '[data-dismiss]');
421 if (dismissButton && dismissButton.getAttribute('data-dismiss') === 'modal') {
422 this.hide();
423 }
424 }
425 };
426 Modal.prototype.setBackgroud = function () {
427 var element = this.getElement();
428 var background = this.getProp('background');
429 if (!background) {
430 return;
431 }
432 if (!element.classList.contains("modal-" + background)) {
433 element.classList.add("modal-" + background);
434 }
435 if (!element.classList.contains('text-white')) {
436 element.classList.add('text-white');
437 }
438 };
439 Modal.prototype.buildButton = function (buttonInfo) {
440 var button = document.createElement('button');
441 button.setAttribute('type', 'button');
442 button.setAttribute('class', buttonInfo.class || 'btn');
443 button.setAttribute('data-event', buttonInfo.event);
444 button.innerHTML = buttonInfo.text;
445 if (buttonInfo.dismiss) {
446 button.setAttribute('data-dismiss', 'modal');
447 }
448 return button;
449 };
450 Modal.prototype.buildBackdrop = function () {
451 var backdrop = document.createElement('div');
452 backdrop.setAttribute('data-id', this.getId());
453 backdrop.classList.add(this.backdropSelector);
454 document.body.appendChild(backdrop);
455 };
456 Modal.prototype.getBackdrop = function () {
457 return document.querySelector("." + this.backdropSelector + "[data-id=\"" + this.getId() + "\"]");
458 };
459 Modal.prototype.removeTextBody = function () {
460 var element = this.getElement();
461 element.querySelector('.modal-body')
462 .removeChild(element.querySelector('.modal-body').firstChild);
463 };
464 Modal.prototype.removeFooter = function () {
465 var element = this.getElement();
466 var footer = element.querySelector('.modal-footer');
467 element.querySelector('.modal-content').removeChild(footer);
468 };
469 Modal.prototype.center = function () {
470 var element = this.getElement();
471 var computedStyle = window.getComputedStyle(element);
472 if (computedStyle && computedStyle.height) {
473 var height = computedStyle.height.slice(0, computedStyle.height.length - 2);
474 var top_1 = (window.innerHeight / 2) - (parseFloat(height) / 2);
475 element.style.top = top_1 + "px";
476 }
477 };
478 Modal.prototype.attachEvents = function () {
479 var _this = this;
480 var element = this.getElement();
481 var buttons = Array
482 .from(element.querySelectorAll('[data-dismiss], .modal-footer button') || []);
483 buttons.forEach(function (button) { return _this.registerElement({
484 target: button,
485 event: Util.Event.CLICK,
486 }); });
487 var cancelable = this.getProp('cancelable');
488 var backdrop = this.getBackdrop();
489 if (cancelable && backdrop) {
490 this.registerElement({ target: backdrop, event: Util.Event.START });
491 this.registerElement({ target: document, event: 'keyup' });
492 }
493 };
494 Modal.prototype.detachEvents = function () {
495 var _this = this;
496 var element = this.getElement();
497 var buttons = Array
498 .from(element.querySelectorAll('[data-dismiss], .modal-footer button') || []);
499 buttons.forEach(function (button) { return _this.unregisterElement({
500 target: button,
501 event: Util.Event.CLICK,
502 }); });
503 var cancelable = this.getProp('cancelable');
504 if (cancelable) {
505 var backdrop = this.getBackdrop();
506 this.unregisterElement({ target: backdrop, event: Util.Event.START });
507 this.unregisterElement({ target: document, event: 'keyup' });
508 }
509 };
510 return Modal;
511}(Component));
512Modal.attachDOM();
513
514var ModalPrompt = (function (_super) {
515 __extends(ModalPrompt, _super);
516 function ModalPrompt(props) {
517 var _this = _super.call(this, Object.assign({
518 buttons: [
519 { event: 'cancel', text: 'Cancel', dismiss: true, class: 'btn btn-secondary' },
520 { event: 'confirm', text: 'Ok', dismiss: true, class: 'btn btn-primary' },
521 ],
522 inputValue: '',
523 }, props), false) || this;
524 _this.setTemplate(''
525 + '<div class="modal" tabindex="-1" role="modal" data-no-boot>'
526 + '<div class="modal-inner" role="document">'
527 + '<div class="modal-content">'
528 + '<div class="modal-header">'
529 + '<h5 class="modal-title"></h5>'
530 + '<button type="button" class="icon-close" data-dismiss="modal" aria-label="Close">'
531 + '<span class="icon" aria-hidden="true"></span>'
532 + '</button>'
533 + '</div>'
534 + '<div class="modal-body">'
535 + '<p></p>'
536 + '<input class="form-control" type="text" value="">'
537 + '</div>'
538 + '<div class="modal-footer">'
539 + '</div>'
540 + '</div>'
541 + '</div>'
542 + '</div>');
543 if (_this.getElement() === null) {
544 _this.build();
545 }
546 return _this;
547 }
548 ModalPrompt.prototype.show = function () {
549 _super.prototype.show.call(this);
550 var defaultValue = this.getProp('inputValue');
551 if (typeof defaultValue === 'string') {
552 this.setInputValue(defaultValue);
553 }
554 this.attachInputEvent();
555 return true;
556 };
557 ModalPrompt.prototype.hide = function () {
558 _super.prototype.hide.call(this);
559 this.detachInputEvent();
560 return true;
561 };
562 ModalPrompt.prototype.setInputValue = function (value) {
563 if (value === void 0) { value = ''; }
564 this.getInput().value = value;
565 };
566 ModalPrompt.prototype.getInputValue = function () {
567 return this.getInput().value;
568 };
569 ModalPrompt.prototype.onElementEvent = function (event) {
570 if (event.target === this.getInput()) {
571 return;
572 }
573 };
574 ModalPrompt.prototype.getInput = function () {
575 return this.getElement().querySelector('.form-control');
576 };
577 ModalPrompt.prototype.attachInputEvent = function () {
578 this.registerElement({ target: this.getInput(), event: 'keyup' });
579 };
580 ModalPrompt.prototype.detachInputEvent = function () {
581 this.unregisterElement({ target: this.getInput(), event: 'keyup' });
582 };
583 return ModalPrompt;
584}(Modal));
585
586module.exports = ModalPrompt;
587//# sourceMappingURL=modalprompt.js.map