| 1 |
/** |
| 2 |
* ModaliseJS - Alexis Paques |
| 3 |
* GPL v3.0 |
| 4 |
*/ |
| 5 |
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"===typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"===typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ |
| 6 |
(function( window, undefined ) { |
| 7 |
"use strict"; |
| 8 |
|
| 9 |
var document = window.document, |
| 10 |
extend = require('./utils/extend'); |
| 11 |
|
| 12 |
/* |
| 13 |
* var myModal = Modalise('htmlID', options); |
| 14 |
* |
| 15 |
* id: The HTML id of the object |
| 16 |
* options: options can modify the class name to which are bind the close, cancel and confirm functions, plus the buttons to open the modal. |
| 17 |
var options = { |
| 18 |
"classClose": ".close", |
| 19 |
"classCancel": ".cancel", |
| 20 |
"classConfirm": ".confirm", |
| 21 |
"btnsOpen": [ HTMLelements ] |
| 22 |
} |
| 23 |
*/ |
| 24 |
var Modalise = function(id, options) { |
| 25 |
var self = this, |
| 26 |
init; |
| 27 |
self.callbacks = {} |
| 28 |
init = { |
| 29 |
start: function() { |
| 30 |
self.events = { |
| 31 |
onShow : new Event('onShow'), |
| 32 |
onConfirm : new Event('onConfirm'), |
| 33 |
onHide : new Event('onHide') |
| 34 |
}; |
| 35 |
self.modal = document.getElementById(id); |
| 36 |
self.classClose = '.close'; |
| 37 |
self.classCancel = '.cancel'; |
| 38 |
self.classConfirm = '.confirm'; |
| 39 |
self.btnsOpen = []; |
| 40 |
self.utils = { |
| 41 |
extend: extend |
| 42 |
}; |
| 43 |
|
| 44 |
self.utils.extend(self, options); |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
/* |
| 49 |
* Modalise.show() : |
| 50 |
* |
| 51 |
* Shows the modal |
| 52 |
*/ |
| 53 |
this.show = function(){ |
| 54 |
self.modal.dispatchEvent(self.events.onShow); |
| 55 |
self.modal.style.display = "block"; |
| 56 |
return self; |
| 57 |
} |
| 58 |
|
| 59 |
/* Modalise.hide() : |
| 60 |
* |
| 61 |
* Hides the modal |
| 62 |
*/ |
| 63 |
this.hide = function(){ |
| 64 |
self.modal.dispatchEvent(self.events.onHide); |
| 65 |
self.modal.style.display = "none"; |
| 66 |
return self; |
| 67 |
} |
| 68 |
|
| 69 |
/* |
| 70 |
* Modalise.removeEvents() : |
| 71 |
* |
| 72 |
* Removes the events (by cloning the modal) |
| 73 |
*/ |
| 74 |
this.removeEvents = function(){ |
| 75 |
var clone = self.modal.cloneNode(true); |
| 76 |
self.modal.parentNode.replaceChild(clone, self.modal); |
| 77 |
self.modal = clone; |
| 78 |
return self; |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* Modalise.on(event, callback): |
| 83 |
* |
| 84 |
* Connect an event. |
| 85 |
* |
| 86 |
* event: |
| 87 |
* - 'onShow': Called when the modal is shown (via Modalise.show() or a binded button) |
| 88 |
* - 'onConfirm': Called when the modal when the user sends the data (via the element with the class '.confirm') |
| 89 |
* - 'onHide': Called when the modal is hidden (via Modalise.hide() or a binded button) |
| 90 |
* callback: The function to call on the event |
| 91 |
* |
| 92 |
*/ |
| 93 |
this.on = function(event, callback){ |
| 94 |
this.modal.addEventListener(event, callback); |
| 95 |
return self; |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* Modalise.attach() : |
| 100 |
* |
| 101 |
* Attaches the click events on the elements with classes ".confirm", ".hide", ".cancel" plus the elements to show the modal |
| 102 |
*/ |
| 103 |
this.attach = function() { |
| 104 |
var items = []; |
| 105 |
|
| 106 |
items = self.modal.querySelectorAll(self.classClose); |
| 107 |
for (var i = items.length - 1; i >= 0; i--) { |
| 108 |
items[i].addEventListener('click', function(){ |
| 109 |
self.hide(); |
| 110 |
}); |
| 111 |
} |
| 112 |
|
| 113 |
items = self.modal.querySelectorAll(self.classCancel); |
| 114 |
for (var i = items.length - 1; i >= 0; i--) { |
| 115 |
items[i].addEventListener('click', function(){ |
| 116 |
self.hide(); |
| 117 |
}); |
| 118 |
} |
| 119 |
|
| 120 |
items = self.modal.querySelectorAll(self.classConfirm); |
| 121 |
for (var i = items.length - 1; i >= 0; i--) { |
| 122 |
items[i].addEventListener('click', function(){ |
| 123 |
self.modal.dispatchEvent(self.events.onConfirm); |
| 124 |
self.hide(); |
| 125 |
}); |
| 126 |
} |
| 127 |
|
| 128 |
for (var i = self.btnsOpen.length - 1; i >= 0; i--) { |
| 129 |
self.btnsOpen[i].addEventListener('click', function(){ |
| 130 |
self.show(); |
| 131 |
}); |
| 132 |
} |
| 133 |
return self; |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* Attach an external element that will open the modal. |
| 138 |
* Modalise.addOpenBtn(element) |
| 139 |
* |
| 140 |
* element: Any HTML element a button, div, span,... |
| 141 |
*/ |
| 142 |
this.addOpenBtn = function(element) { |
| 143 |
self.btnsOpen.push(element); |
| 144 |
}; |
| 145 |
|
| 146 |
init.start(); |
| 147 |
return self; |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
// AMD (Asynchronous Module Definition API) support, more information about AMD : https://github.com/amdjs/amdjs-api/wiki/AMD |
| 152 |
if (typeof define === 'function' && define.amd) { |
| 153 |
define(function () { return Modalise; }); |
| 154 |
} |
| 155 |
|
| 156 |
module.exports = Modalise; |
| 157 |
window.Modalise = Modalise; |
| 158 |
|
| 159 |
})(window); |
| 160 |
},{"./utils/extend":2}],2:[function(require,module,exports){ |
| 161 |
/* |
| 162 |
* Source: https://github.com/segmentio/extend |
| 163 |
*/ |
| 164 |
|
| 165 |
module.exports = function extend (object) { |
| 166 |
// Takes an unlimited number of extenders. |
| 167 |
var args = Array.prototype.slice.call(arguments, 1); |
| 168 |
|
| 169 |
// For each extender, copy their properties on our object. |
| 170 |
for (var i = 0, source; source = args[i]; i++) { |
| 171 |
if (!source) continue; |
| 172 |
for (var property in source) { |
| 173 |
object[property] = source[property]; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return object; |
| 178 |
}; |
| 179 |
},{}]},{},[1]); |
| 180 |
|
| 181 |
|