| 1 |
/** |
| 2 |
* Photonic Prompter |
| 3 |
* Used for interactive dialogs, e.g. the Photonic password prompter. |
| 4 |
* |
| 5 |
* GPL v3.0 |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* var myPrompter = Prompter('htmlID'); |
| 10 |
* |
| 11 |
* id: The HTML id of the object |
| 12 |
*/ |
| 13 |
export class Prompter { |
| 14 |
constructor(id) { |
| 15 |
this.events = { |
| 16 |
onShow : new Event('onShow'), |
| 17 |
onConfirm : new Event('onConfirm'), |
| 18 |
onHide : new Event('onHide') |
| 19 |
}; |
| 20 |
this.modal = document.getElementById(id); |
| 21 |
this.classClose = '.close'; |
| 22 |
this.classCancel = '.cancel'; |
| 23 |
this.classConfirm = '.confirm'; |
| 24 |
this.btnsOpen = []; |
| 25 |
} |
| 26 |
|
| 27 |
/* |
| 28 |
* Prompter.show() : |
| 29 |
* |
| 30 |
* Shows the modal |
| 31 |
*/ |
| 32 |
show() { |
| 33 |
this.modal.dispatchEvent(this.events.onShow); |
| 34 |
this.modal.style.display = "block"; |
| 35 |
return this; |
| 36 |
} |
| 37 |
|
| 38 |
/* Prompter.hide() : |
| 39 |
* |
| 40 |
* Hides the modal |
| 41 |
*/ |
| 42 |
hide() { |
| 43 |
this.modal.dispatchEvent(this.events.onHide); |
| 44 |
this.modal.style.display = "none"; |
| 45 |
return this; |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
* Prompter.removeEvents() : |
| 50 |
* |
| 51 |
* Removes the events (by cloning the modal) |
| 52 |
*/ |
| 53 |
removeEvents() { |
| 54 |
const clone = this.modal.cloneNode(true); |
| 55 |
this.modal.parentNode.replaceChild(clone, this.modal); |
| 56 |
this.modal = clone; |
| 57 |
return this; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* Prompter.on(event, callback): |
| 62 |
* |
| 63 |
* Connect an event. |
| 64 |
* |
| 65 |
* event: |
| 66 |
* - 'onShow': Called when the modal is shown (via Prompter.show() or a bound button) |
| 67 |
* - 'onConfirm': Called when the modal when the user sends the data (via the element with the class '.confirm') |
| 68 |
* - 'onHide': Called when the modal is hidden (via Prompter.hide() or a bound button) |
| 69 |
* callback: The function to call on the event |
| 70 |
* |
| 71 |
*/ |
| 72 |
on(event, callback) { |
| 73 |
this.modal.addEventListener(event, callback); |
| 74 |
return this; |
| 75 |
} |
| 76 |
|
| 77 |
/* |
| 78 |
* Prompter.attach() : |
| 79 |
* |
| 80 |
* Attaches the click events on the elements with classes ".confirm", ".hide", ".cancel" plus the elements to show the modal |
| 81 |
*/ |
| 82 |
attach() { |
| 83 |
let i; |
| 84 |
let items = []; |
| 85 |
const self = this; |
| 86 |
|
| 87 |
items = this.modal.querySelectorAll(self.classClose); |
| 88 |
for (i = items.length - 1; i >= 0; i--) { |
| 89 |
items[i].addEventListener('click', function(){ |
| 90 |
self.hide(); |
| 91 |
}); |
| 92 |
} |
| 93 |
|
| 94 |
items = self.modal.querySelectorAll(self.classCancel); |
| 95 |
for (i = items.length - 1; i >= 0; i--) { |
| 96 |
items[i].addEventListener('click', function(){ |
| 97 |
self.hide(); |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
items = self.modal.querySelectorAll(self.classConfirm); |
| 102 |
for (i = items.length - 1; i >= 0; i--) { |
| 103 |
items[i].addEventListener('click', function(){ |
| 104 |
self.modal.dispatchEvent(self.events.onConfirm); |
| 105 |
self.hide(); |
| 106 |
}); |
| 107 |
} |
| 108 |
|
| 109 |
for (i = self.btnsOpen.length - 1; i >= 0; i--) { |
| 110 |
self.btnsOpen[i].addEventListener('click', function(){ |
| 111 |
self.show(); |
| 112 |
}); |
| 113 |
} |
| 114 |
return self; |
| 115 |
} |
| 116 |
|
| 117 |
/* |
| 118 |
* Attach an external element that will open the modal. |
| 119 |
* Prompter.addOpenBtn(element) |
| 120 |
* |
| 121 |
* element: Any HTML element a button, div, span,... |
| 122 |
*/ |
| 123 |
addOpenBtn(element) { |
| 124 |
this.btnsOpen.push(element); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|