├── README.md ├── css ├── demo.css └── modal.css ├── img └── bg.jpg ├── index.html └── js ├── draggabilly.pkgd.js └── modal.js /README.md: -------------------------------------------------------------------------------- 1 | # draggable-google-modal 2 | Simple draggable modal with pure Javascript, inspired by *Google* Modal. 3 | 4 | ___ 5 | 6 | ### Features 7 | 8 | * Without external dependencies. 9 | * Drag and Drop 10 | * When is fixed, the drag is disabled. 11 | * Responsive 12 | * Shortcuts 13 | * Ctrl + **Up** Arrow: Fill the entire screen; 14 | * Ctrl + **Down** Arrow: Exit full screen; 15 | * Ctrl + **Left** Arrow: Align the left, filling half the screen*; 16 | * Ctrl + **Right** Arrow: Align the right, filling half the screen*; 17 | -------------------------------------------------------------------------------- /css/demo.css: -------------------------------------------------------------------------------- 1 | /* GENERAL */ 2 | *{ box-sizing: border-box;} 3 | 4 | html{ 5 | height: 100%; 6 | } 7 | body{ 8 | background: #f9f9f9 url("../img/bg.jpg") no-repeat left top; 9 | font-family: 'Lato', arial; 10 | } 11 | 12 | .btn-fixed{ 13 | position: fixed; 14 | left: 20px; 15 | bottom: 20px; 16 | } -------------------------------------------------------------------------------- /css/modal.css: -------------------------------------------------------------------------------- 1 | /* MODAL */ 2 | .modal-overlay{ 3 | visibility: hidden; 4 | opacity: 0; 5 | position: absolute; 6 | left: 0; 7 | top: 0; 8 | bottom: 0; 9 | right: 0; 10 | z-index: 98; 11 | } 12 | .modal{ 13 | visibility: hidden; 14 | opacity: 0; 15 | background: #fff; 16 | box-shadow: 0 4px 16px rgba(0,0,0,.2); 17 | border: 1px solid rgba(0,0,0,.333); 18 | width: 800px; 19 | height: 360px; 20 | position: absolute; 21 | left: 50%; 22 | top: 50%; 23 | margin-left: -400px; 24 | margin-top: -180px; 25 | z-index: 99; 26 | } 27 | .modal-body{ 28 | overflow-y: scroll; 29 | height: calc( 100% - 75px ); 30 | } 31 | .modal-content{ 32 | padding: 20px; 33 | transition: transform 0.7s cubic-bezier(0.165, 0.840, 0.440, 1.000); 34 | transform: translateY(-50px); 35 | } 36 | .modal.opening .modal-content{ 37 | transform: translateY(0px); 38 | } 39 | .modal-content p{ 40 | font-size: 15px; 41 | margin: 0 0 15px; 42 | } 43 | .modal-header{ 44 | transition: border-color 0.2s ease; 45 | box-sizing: border-box; 46 | background-color: #eee; 47 | border-bottom: 1px solid rgba(0,0,0,.2); 48 | font-size: 16px; 49 | height: 75px; 50 | line-height: 30px; 51 | margin: 0; 52 | padding: 22px 26px; 53 | vertical-align: middle; 54 | } 55 | .modal-header-title{ 56 | float: left; 57 | margin: 0; 58 | padding: 0; 59 | font-size: 15px; 60 | line-height: 28px; 61 | font-weight: 400; 62 | letter-spacing: -0.03em; 63 | cursor: default; 64 | } 65 | .modal-header-btn{ 66 | float: right; 67 | background-color: #4d90fe; 68 | background-image: -webkit-linear-gradient(top,#4d90fe,#4787ed); 69 | background-image: linear-gradient(top,#4d90fe,#4787ed); 70 | border: 1px solid #3079ed; 71 | color: #fff; 72 | border-radius: 2px; 73 | font-size: 11px; 74 | font-weight: 600; 75 | text-align: center; 76 | white-space: nowrap; 77 | margin-left: 5px; 78 | line-height: 26px; 79 | min-width: 70px; 80 | outline: 0; 81 | padding: 0 12px; 82 | cursor: pointer; 83 | } 84 | .modal-header-btn:hover{ 85 | opacity: 0.8; 86 | } 87 | .modal-header-btn:active { 88 | box-shadow: inset 0 1px 2px rgba(0,0,0,0.3); 89 | background: #357ae8; 90 | border: 1px solid #2f5bb7; 91 | border-top: 1px solid #2f5bb7; 92 | } 93 | 94 | /* STATUS */ 95 | 96 | .modal.is-full{ 97 | left: 0 !important; 98 | top: 0 !important; 99 | width: calc(100% - 30px) !important; 100 | height: calc(100% - 30px) !important; 101 | margin: 15px !important; 102 | } 103 | .modal.is-left{ 104 | left: 0px !important; 105 | right: auto !important; 106 | top: 0px !important; 107 | margin: 0px !important; 108 | height: 100% !important; 109 | } 110 | .modal.is-left{ 111 | left: 0px !important; 112 | right: auto !important; 113 | top: 0px !important; 114 | margin: 0px !important; 115 | height: 100% !important; 116 | } 117 | .modal.is-right{ 118 | left: auto !important; 119 | right: 0px !important; 120 | top: 0px !important; 121 | margin: 0px !important; 122 | height: 100% !important; 123 | } 124 | .modal.is-dragging{ 125 | border-color: #66afe9; 126 | outline: 0; 127 | box-shadow: 0 0 8px rgba(102,175,233,.6), 0 6px 20px rgba(0,0,0,.2); 128 | } 129 | .modal.opening, .modal-overlay.opening{ 130 | -webkit-animation: show 0.5s ease; 131 | animation: show 0.5s ease; 132 | -webkit-animation-fill-mode: forwards; 133 | animation-fill-mode: forwards; 134 | } 135 | .modal.closing, .modal-overlay.closing{ 136 | -webkit-animation: hide 0.5s ease; 137 | animation: hide 0.5s ease; 138 | -webkit-animation-fill-mode: forwards; 139 | animation-fill-mode: forwards; 140 | } 141 | @keyframes show { 142 | 0% { opacity: 0; } 143 | 100% {opacity: 1; } 144 | } 145 | @-webkit-keyframes show { 146 | 0% { opacity: 0; }.modal-header-title 147 | 100% { opacity: 1; } 148 | } 149 | @keyframes hide { 150 | 0% { opacity: 1; } 151 | 100% { opacity: 0; } 152 | } 153 | @-webkit-keyframes hide { 154 | 0% { opacity: 1; } 155 | 100% { opacity: 0; } 156 | } 157 | 158 | @media only screen and (max-width : 800px) { 159 | 160 | .modal{ 161 | width: 100%; 162 | left: 0 !important; 163 | margin-left: 0 !important; 164 | } 165 | 166 | } 167 | 168 | /* SCROLL */ 169 | .modal ::-webkit-scrollbar { 170 | overflow: visible; 171 | height: 13px; 172 | width: 14px; 173 | } 174 | .modal ::-webkit-scrollbar-thumb { 175 | background-color: rgba(0,0,0,.2); 176 | background-clip: padding-box; 177 | border: solid transparent; 178 | border-width: 3px; 179 | min-height: 28px; 180 | padding: 100px 0 0; 181 | box-shadow: inset 1px 1px 0 rgba(0,0,0,.1),inset 0 -1px 0 rgba(0,0,0,.07); 182 | } 183 | .modal ::-webkit-scrollbar-thumb:active { 184 | background-color: rgba(0,0,0,.4); 185 | } 186 | .modal ::-webkit-scrollbar-button { 187 | height: 0; 188 | width: 0; 189 | } 190 | .modal ::-webkit-scrollbar-track { 191 | background-clip: padding-box; 192 | border: solid transparent; 193 | border-width: 0 0 0 4px; 194 | } -------------------------------------------------------------------------------- /img/bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcelodolza/draggable-google-modal/4bb0e2aac1cff3483447c66f9bd719d6b738cc4c/img/bg.jpg -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Drag & Drop Modal 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 33 | 34 | 35 | 36 | 37 | 38 | 45 | 46 | -------------------------------------------------------------------------------- /js/draggabilly.pkgd.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Draggabilly PACKAGED v1.1.2 3 | * Make that shiz draggable 4 | * http://draggabilly.desandro.com 5 | * MIT license 6 | */ 7 | 8 | /*! 9 | * classie - class helper functions 10 | * from bonzo https://github.com/ded/bonzo 11 | * 12 | * classie.has( elem, 'my-class' ) -> true/false 13 | * classie.add( elem, 'my-new-class' ) 14 | * classie.remove( elem, 'my-unwanted-class' ) 15 | * classie.toggle( elem, 'my-class' ) 16 | */ 17 | 18 | /*jshint browser: true, strict: true, undef: true */ 19 | /*global define: false */ 20 | 21 | ( function( window ) { 22 | 23 | 24 | 25 | // class helper functions from bonzo https://github.com/ded/bonzo 26 | 27 | function classReg( className ) { 28 | return new RegExp("(^|\\s+)" + className + "(\\s+|$)"); 29 | } 30 | 31 | // classList support for class management 32 | // altho to be fair, the api sucks because it won't accept multiple classes at once 33 | var hasClass, addClass, removeClass; 34 | 35 | if ( 'classList' in document.documentElement ) { 36 | hasClass = function( elem, c ) { 37 | return elem.classList.contains( c ); 38 | }; 39 | addClass = function( elem, c ) { 40 | elem.classList.add( c ); 41 | }; 42 | removeClass = function( elem, c ) { 43 | elem.classList.remove( c ); 44 | }; 45 | } 46 | else { 47 | hasClass = function( elem, c ) { 48 | return classReg( c ).test( elem.className ); 49 | }; 50 | addClass = function( elem, c ) { 51 | if ( !hasClass( elem, c ) ) { 52 | elem.className = elem.className + ' ' + c; 53 | } 54 | }; 55 | removeClass = function( elem, c ) { 56 | elem.className = elem.className.replace( classReg( c ), ' ' ); 57 | }; 58 | } 59 | 60 | function toggleClass( elem, c ) { 61 | var fn = hasClass( elem, c ) ? removeClass : addClass; 62 | fn( elem, c ); 63 | } 64 | 65 | var classie = { 66 | // full names 67 | hasClass: hasClass, 68 | addClass: addClass, 69 | removeClass: removeClass, 70 | toggleClass: toggleClass, 71 | // short names 72 | has: hasClass, 73 | add: addClass, 74 | remove: removeClass, 75 | toggle: toggleClass 76 | }; 77 | 78 | // transport 79 | if ( typeof define === 'function' && define.amd ) { 80 | // AMD 81 | define( 'classie/classie',classie ); 82 | } else { 83 | // browser global 84 | window.classie = classie; 85 | } 86 | 87 | })( window ); 88 | 89 | /*! 90 | * EventEmitter v4.2.2 - git.io/ee 91 | * Oliver Caldwell 92 | * MIT license 93 | * @preserve 94 | */ 95 | 96 | (function () { 97 | 98 | 99 | /** 100 | * Class for managing events. 101 | * Can be extended to provide event functionality in other classes. 102 | * 103 | * @class EventEmitter Manages event registering and emitting. 104 | */ 105 | function EventEmitter() {} 106 | 107 | // Shortcuts to improve speed and size 108 | 109 | // Easy access to the prototype 110 | var proto = EventEmitter.prototype; 111 | 112 | /** 113 | * Finds the index of the listener for the event in it's storage array. 114 | * 115 | * @param {Function[]} listeners Array of listeners to search through. 116 | * @param {Function} listener Method to look for. 117 | * @return {Number} Index of the specified listener, -1 if not found 118 | * @api private 119 | */ 120 | function indexOfListener(listeners, listener) { 121 | var i = listeners.length; 122 | while (i--) { 123 | if (listeners[i].listener === listener) { 124 | return i; 125 | } 126 | } 127 | 128 | return -1; 129 | } 130 | 131 | /** 132 | * Alias a method while keeping the context correct, to allow for overwriting of target method. 133 | * 134 | * @param {String} name The name of the target method. 135 | * @return {Function} The aliased method 136 | * @api private 137 | */ 138 | function alias(name) { 139 | return function aliasClosure() { 140 | return this[name].apply(this, arguments); 141 | }; 142 | } 143 | 144 | /** 145 | * Returns the listener array for the specified event. 146 | * Will initialise the event object and listener arrays if required. 147 | * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them. 148 | * Each property in the object response is an array of listener functions. 149 | * 150 | * @param {String|RegExp} evt Name of the event to return the listeners from. 151 | * @return {Function[]|Object} All listener functions for the event. 152 | */ 153 | proto.getListeners = function getListeners(evt) { 154 | var events = this._getEvents(); 155 | var response; 156 | var key; 157 | 158 | // Return a concatenated array of all matching events if 159 | // the selector is a regular expression. 160 | if (typeof evt === 'object') { 161 | response = {}; 162 | for (key in events) { 163 | if (events.hasOwnProperty(key) && evt.test(key)) { 164 | response[key] = events[key]; 165 | } 166 | } 167 | } 168 | else { 169 | response = events[evt] || (events[evt] = []); 170 | } 171 | 172 | return response; 173 | }; 174 | 175 | /** 176 | * Takes a list of listener objects and flattens it into a list of listener functions. 177 | * 178 | * @param {Object[]} listeners Raw listener objects. 179 | * @return {Function[]} Just the listener functions. 180 | */ 181 | proto.flattenListeners = function flattenListeners(listeners) { 182 | var flatListeners = []; 183 | var i; 184 | 185 | for (i = 0; i < listeners.length; i += 1) { 186 | flatListeners.push(listeners[i].listener); 187 | } 188 | 189 | return flatListeners; 190 | }; 191 | 192 | /** 193 | * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful. 194 | * 195 | * @param {String|RegExp} evt Name of the event to return the listeners from. 196 | * @return {Object} All listener functions for an event in an object. 197 | */ 198 | proto.getListenersAsObject = function getListenersAsObject(evt) { 199 | var listeners = this.getListeners(evt); 200 | var response; 201 | 202 | if (listeners instanceof Array) { 203 | response = {}; 204 | response[evt] = listeners; 205 | } 206 | 207 | return response || listeners; 208 | }; 209 | 210 | /** 211 | * Adds a listener function to the specified event. 212 | * The listener will not be added if it is a duplicate. 213 | * If the listener returns true then it will be removed after it is called. 214 | * If you pass a regular expression as the event name then the listener will be added to all events that match it. 215 | * 216 | * @param {String|RegExp} evt Name of the event to attach the listener to. 217 | * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. 218 | * @return {Object} Current instance of EventEmitter for chaining. 219 | */ 220 | proto.addListener = function addListener(evt, listener) { 221 | var listeners = this.getListenersAsObject(evt); 222 | var listenerIsWrapped = typeof listener === 'object'; 223 | var key; 224 | 225 | for (key in listeners) { 226 | if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) { 227 | listeners[key].push(listenerIsWrapped ? listener : { 228 | listener: listener, 229 | once: false 230 | }); 231 | } 232 | } 233 | 234 | return this; 235 | }; 236 | 237 | /** 238 | * Alias of addListener 239 | */ 240 | proto.on = alias('addListener'); 241 | 242 | /** 243 | * Semi-alias of addListener. It will add a listener that will be 244 | * automatically removed after it's first execution. 245 | * 246 | * @param {String|RegExp} evt Name of the event to attach the listener to. 247 | * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. 248 | * @return {Object} Current instance of EventEmitter for chaining. 249 | */ 250 | proto.addOnceListener = function addOnceListener(evt, listener) { 251 | return this.addListener(evt, { 252 | listener: listener, 253 | once: true 254 | }); 255 | }; 256 | 257 | /** 258 | * Alias of addOnceListener. 259 | */ 260 | proto.once = alias('addOnceListener'); 261 | 262 | /** 263 | * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad. 264 | * You need to tell it what event names should be matched by a regex. 265 | * 266 | * @param {String} evt Name of the event to create. 267 | * @return {Object} Current instance of EventEmitter for chaining. 268 | */ 269 | proto.defineEvent = function defineEvent(evt) { 270 | this.getListeners(evt); 271 | return this; 272 | }; 273 | 274 | /** 275 | * Uses defineEvent to define multiple events. 276 | * 277 | * @param {String[]} evts An array of event names to define. 278 | * @return {Object} Current instance of EventEmitter for chaining. 279 | */ 280 | proto.defineEvents = function defineEvents(evts) { 281 | for (var i = 0; i < evts.length; i += 1) { 282 | this.defineEvent(evts[i]); 283 | } 284 | return this; 285 | }; 286 | 287 | /** 288 | * Removes a listener function from the specified event. 289 | * When passed a regular expression as the event name, it will remove the listener from all events that match it. 290 | * 291 | * @param {String|RegExp} evt Name of the event to remove the listener from. 292 | * @param {Function} listener Method to remove from the event. 293 | * @return {Object} Current instance of EventEmitter for chaining. 294 | */ 295 | proto.removeListener = function removeListener(evt, listener) { 296 | var listeners = this.getListenersAsObject(evt); 297 | var index; 298 | var key; 299 | 300 | for (key in listeners) { 301 | if (listeners.hasOwnProperty(key)) { 302 | index = indexOfListener(listeners[key], listener); 303 | 304 | if (index !== -1) { 305 | listeners[key].splice(index, 1); 306 | } 307 | } 308 | } 309 | 310 | return this; 311 | }; 312 | 313 | /** 314 | * Alias of removeListener 315 | */ 316 | proto.off = alias('removeListener'); 317 | 318 | /** 319 | * Adds listeners in bulk using the manipulateListeners method. 320 | * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added. 321 | * You can also pass it a regular expression to add the array of listeners to all events that match it. 322 | * Yeah, this function does quite a bit. That's probably a bad thing. 323 | * 324 | * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once. 325 | * @param {Function[]} [listeners] An optional array of listener functions to add. 326 | * @return {Object} Current instance of EventEmitter for chaining. 327 | */ 328 | proto.addListeners = function addListeners(evt, listeners) { 329 | // Pass through to manipulateListeners 330 | return this.manipulateListeners(false, evt, listeners); 331 | }; 332 | 333 | /** 334 | * Removes listeners in bulk using the manipulateListeners method. 335 | * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. 336 | * You can also pass it an event name and an array of listeners to be removed. 337 | * You can also pass it a regular expression to remove the listeners from all events that match it. 338 | * 339 | * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once. 340 | * @param {Function[]} [listeners] An optional array of listener functions to remove. 341 | * @return {Object} Current instance of EventEmitter for chaining. 342 | */ 343 | proto.removeListeners = function removeListeners(evt, listeners) { 344 | // Pass through to manipulateListeners 345 | return this.manipulateListeners(true, evt, listeners); 346 | }; 347 | 348 | /** 349 | * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level. 350 | * The first argument will determine if the listeners are removed (true) or added (false). 351 | * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. 352 | * You can also pass it an event name and an array of listeners to be added/removed. 353 | * You can also pass it a regular expression to manipulate the listeners of all events that match it. 354 | * 355 | * @param {Boolean} remove True if you want to remove listeners, false if you want to add. 356 | * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once. 357 | * @param {Function[]} [listeners] An optional array of listener functions to add/remove. 358 | * @return {Object} Current instance of EventEmitter for chaining. 359 | */ 360 | proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) { 361 | var i; 362 | var value; 363 | var single = remove ? this.removeListener : this.addListener; 364 | var multiple = remove ? this.removeListeners : this.addListeners; 365 | 366 | // If evt is an object then pass each of it's properties to this method 367 | if (typeof evt === 'object' && !(evt instanceof RegExp)) { 368 | for (i in evt) { 369 | if (evt.hasOwnProperty(i) && (value = evt[i])) { 370 | // Pass the single listener straight through to the singular method 371 | if (typeof value === 'function') { 372 | single.call(this, i, value); 373 | } 374 | else { 375 | // Otherwise pass back to the multiple function 376 | multiple.call(this, i, value); 377 | } 378 | } 379 | } 380 | } 381 | else { 382 | // So evt must be a string 383 | // And listeners must be an array of listeners 384 | // Loop over it and pass each one to the multiple method 385 | i = listeners.length; 386 | while (i--) { 387 | single.call(this, evt, listeners[i]); 388 | } 389 | } 390 | 391 | return this; 392 | }; 393 | 394 | /** 395 | * Removes all listeners from a specified event. 396 | * If you do not specify an event then all listeners will be removed. 397 | * That means every event will be emptied. 398 | * You can also pass a regex to remove all events that match it. 399 | * 400 | * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed. 401 | * @return {Object} Current instance of EventEmitter for chaining. 402 | */ 403 | proto.removeEvent = function removeEvent(evt) { 404 | var type = typeof evt; 405 | var events = this._getEvents(); 406 | var key; 407 | 408 | // Remove different things depending on the state of evt 409 | if (type === 'string') { 410 | // Remove all listeners for the specified event 411 | delete events[evt]; 412 | } 413 | else if (type === 'object') { 414 | // Remove all events matching the regex. 415 | for (key in events) { 416 | if (events.hasOwnProperty(key) && evt.test(key)) { 417 | delete events[key]; 418 | } 419 | } 420 | } 421 | else { 422 | // Remove all listeners in all events 423 | delete this._events; 424 | } 425 | 426 | return this; 427 | }; 428 | 429 | /** 430 | * Emits an event of your choice. 431 | * When emitted, every listener attached to that event will be executed. 432 | * If you pass the optional argument array then those arguments will be passed to every listener upon execution. 433 | * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately. 434 | * So they will not arrive within the array on the other side, they will be separate. 435 | * You can also pass a regular expression to emit to all events that match it. 436 | * 437 | * @param {String|RegExp} evt Name of the event to emit and execute listeners for. 438 | * @param {Array} [args] Optional array of arguments to be passed to each listener. 439 | * @return {Object} Current instance of EventEmitter for chaining. 440 | */ 441 | proto.emitEvent = function emitEvent(evt, args) { 442 | var listeners = this.getListenersAsObject(evt); 443 | var listener; 444 | var i; 445 | var key; 446 | var response; 447 | 448 | for (key in listeners) { 449 | if (listeners.hasOwnProperty(key)) { 450 | i = listeners[key].length; 451 | 452 | while (i--) { 453 | // If the listener returns true then it shall be removed from the event 454 | // The function is executed either with a basic call or an apply if there is an args array 455 | listener = listeners[key][i]; 456 | response = listener.listener.apply(this, args || []); 457 | if (response === this._getOnceReturnValue() || listener.once === true) { 458 | this.removeListener(evt, listener.listener); 459 | } 460 | } 461 | } 462 | } 463 | 464 | return this; 465 | }; 466 | 467 | /** 468 | * Alias of emitEvent 469 | */ 470 | proto.trigger = alias('emitEvent'); 471 | 472 | /** 473 | * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on. 474 | * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it. 475 | * 476 | * @param {String|RegExp} evt Name of the event to emit and execute listeners for. 477 | * @param {...*} Optional additional arguments to be passed to each listener. 478 | * @return {Object} Current instance of EventEmitter for chaining. 479 | */ 480 | proto.emit = function emit(evt) { 481 | var args = Array.prototype.slice.call(arguments, 1); 482 | return this.emitEvent(evt, args); 483 | }; 484 | 485 | /** 486 | * Sets the current value to check against when executing listeners. If a 487 | * listeners return value matches the one set here then it will be removed 488 | * after execution. This value defaults to true. 489 | * 490 | * @param {*} value The new value to check for when executing listeners. 491 | * @return {Object} Current instance of EventEmitter for chaining. 492 | */ 493 | proto.setOnceReturnValue = function setOnceReturnValue(value) { 494 | this._onceReturnValue = value; 495 | return this; 496 | }; 497 | 498 | /** 499 | * Fetches the current value to check against when executing listeners. If 500 | * the listeners return value matches this one then it should be removed 501 | * automatically. It will return true by default. 502 | * 503 | * @return {*|Boolean} The current value to check for or the default, true. 504 | * @api private 505 | */ 506 | proto._getOnceReturnValue = function _getOnceReturnValue() { 507 | if (this.hasOwnProperty('_onceReturnValue')) { 508 | return this._onceReturnValue; 509 | } 510 | else { 511 | return true; 512 | } 513 | }; 514 | 515 | /** 516 | * Fetches the events object and creates one if required. 517 | * 518 | * @return {Object} The events storage object. 519 | * @api private 520 | */ 521 | proto._getEvents = function _getEvents() { 522 | return this._events || (this._events = {}); 523 | }; 524 | 525 | // Expose the class either via AMD, CommonJS or the global object 526 | if (typeof define === 'function' && define.amd) { 527 | define('eventEmitter/EventEmitter',[],function () { 528 | return EventEmitter; 529 | }); 530 | } 531 | else if (typeof module === 'object' && module.exports){ 532 | module.exports = EventEmitter; 533 | } 534 | else { 535 | this.EventEmitter = EventEmitter; 536 | } 537 | }.call(this)); 538 | 539 | /*! 540 | * eventie v1.0.3 541 | * event binding helper 542 | * eventie.bind( elem, 'click', myFn ) 543 | * eventie.unbind( elem, 'click', myFn ) 544 | */ 545 | 546 | /*jshint browser: true, undef: true, unused: true */ 547 | /*global define: false */ 548 | 549 | ( function( window ) { 550 | 551 | 552 | 553 | var docElem = document.documentElement; 554 | 555 | var bind = function() {}; 556 | 557 | if ( docElem.addEventListener ) { 558 | bind = function( obj, type, fn ) { 559 | obj.addEventListener( type, fn, false ); 560 | }; 561 | } else if ( docElem.attachEvent ) { 562 | bind = function( obj, type, fn ) { 563 | obj[ type + fn ] = fn.handleEvent ? 564 | function() { 565 | var event = window.event; 566 | // add event.target 567 | event.target = event.target || event.srcElement; 568 | fn.handleEvent.call( fn, event ); 569 | } : 570 | function() { 571 | var event = window.event; 572 | // add event.target 573 | event.target = event.target || event.srcElement; 574 | fn.call( obj, event ); 575 | }; 576 | obj.attachEvent( "on" + type, obj[ type + fn ] ); 577 | }; 578 | } 579 | 580 | var unbind = function() {}; 581 | 582 | if ( docElem.removeEventListener ) { 583 | unbind = function( obj, type, fn ) { 584 | obj.removeEventListener( type, fn, false ); 585 | }; 586 | } else if ( docElem.detachEvent ) { 587 | unbind = function( obj, type, fn ) { 588 | obj.detachEvent( "on" + type, obj[ type + fn ] ); 589 | try { 590 | delete obj[ type + fn ]; 591 | } catch ( err ) { 592 | // can't delete window object properties 593 | obj[ type + fn ] = undefined; 594 | } 595 | }; 596 | } 597 | 598 | var eventie = { 599 | bind: bind, 600 | unbind: unbind 601 | }; 602 | 603 | // transport 604 | if ( typeof define === 'function' && define.amd ) { 605 | // AMD 606 | define( 'eventie/eventie',eventie ); 607 | } else { 608 | // browser global 609 | window.eventie = eventie; 610 | } 611 | 612 | })( this ); 613 | 614 | /*! 615 | * getStyleProperty by kangax 616 | * http://perfectionkills.com/feature-testing-css-properties/ 617 | */ 618 | 619 | /*jshint browser: true, strict: true, undef: true */ 620 | /*globals define: false */ 621 | 622 | ( function( window ) { 623 | 624 | 625 | 626 | var prefixes = 'Webkit Moz ms Ms O'.split(' '); 627 | var docElemStyle = document.documentElement.style; 628 | 629 | function getStyleProperty( propName ) { 630 | if ( !propName ) { 631 | return; 632 | } 633 | 634 | // test standard property first 635 | if ( typeof docElemStyle[ propName ] === 'string' ) { 636 | return propName; 637 | } 638 | 639 | // capitalize 640 | propName = propName.charAt(0).toUpperCase() + propName.slice(1); 641 | 642 | // test vendor specific properties 643 | var prefixed; 644 | for ( var i=0, len = prefixes.length; i < len; i++ ) { 645 | prefixed = prefixes[i] + propName; 646 | if ( typeof docElemStyle[ prefixed ] === 'string' ) { 647 | return prefixed; 648 | } 649 | } 650 | } 651 | 652 | // transport 653 | if ( typeof define === 'function' && define.amd ) { 654 | // AMD 655 | define( 'get-style-property/get-style-property',[],function() { 656 | return getStyleProperty; 657 | }); 658 | } else { 659 | // browser global 660 | window.getStyleProperty = getStyleProperty; 661 | } 662 | 663 | })( window ); 664 | 665 | /** 666 | * getSize v1.1.4 667 | * measure size of elements 668 | */ 669 | 670 | /*jshint browser: true, strict: true, undef: true, unused: true */ 671 | /*global define: false */ 672 | 673 | ( function( window, undefined ) { 674 | 675 | 676 | 677 | // -------------------------- helpers -------------------------- // 678 | 679 | var defView = document.defaultView; 680 | 681 | var getStyle = defView && defView.getComputedStyle ? 682 | function( elem ) { 683 | return defView.getComputedStyle( elem, null ); 684 | } : 685 | function( elem ) { 686 | return elem.currentStyle; 687 | }; 688 | 689 | // get a number from a string, not a percentage 690 | function getStyleSize( value ) { 691 | var num = parseFloat( value ); 692 | // not a percent like '100%', and a number 693 | var isValid = value.indexOf('%') === -1 && !isNaN( num ); 694 | return isValid && num; 695 | } 696 | 697 | // -------------------------- measurements -------------------------- // 698 | 699 | var measurements = [ 700 | 'paddingLeft', 701 | 'paddingRight', 702 | 'paddingTop', 703 | 'paddingBottom', 704 | 'marginLeft', 705 | 'marginRight', 706 | 'marginTop', 707 | 'marginBottom', 708 | 'borderLeftWidth', 709 | 'borderRightWidth', 710 | 'borderTopWidth', 711 | 'borderBottomWidth' 712 | ]; 713 | 714 | function getZeroSize() { 715 | var size = { 716 | width: 0, 717 | height: 0, 718 | innerWidth: 0, 719 | innerHeight: 0, 720 | outerWidth: 0, 721 | outerHeight: 0 722 | }; 723 | for ( var i=0, len = measurements.length; i < len; i++ ) { 724 | var measurement = measurements[i]; 725 | size[ measurement ] = 0; 726 | } 727 | return size; 728 | } 729 | 730 | 731 | 732 | function defineGetSize( getStyleProperty ) { 733 | 734 | // -------------------------- box sizing -------------------------- // 735 | 736 | var boxSizingProp = getStyleProperty('boxSizing'); 737 | var isBoxSizeOuter; 738 | 739 | /** 740 | * WebKit measures the outer-width on style.width on border-box elems 741 | * IE & Firefox measures the inner-width 742 | */ 743 | ( function() { 744 | if ( !boxSizingProp ) { 745 | return; 746 | } 747 | 748 | var div = document.createElement('div'); 749 | div.style.width = '200px'; 750 | div.style.padding = '1px 2px 3px 4px'; 751 | div.style.borderStyle = 'solid'; 752 | div.style.borderWidth = '1px 2px 3px 4px'; 753 | div.style[ boxSizingProp ] = 'border-box'; 754 | 755 | var body = document.body || document.documentElement; 756 | body.appendChild( div ); 757 | var style = getStyle( div ); 758 | 759 | isBoxSizeOuter = getStyleSize( style.width ) === 200; 760 | body.removeChild( div ); 761 | })(); 762 | 763 | 764 | // -------------------------- getSize -------------------------- // 765 | 766 | function getSize( elem ) { 767 | // use querySeletor if elem is string 768 | if ( typeof elem === 'string' ) { 769 | elem = document.querySelector( elem ); 770 | } 771 | 772 | // do not proceed on non-objects 773 | if ( !elem || typeof elem !== 'object' || !elem.nodeType ) { 774 | return; 775 | } 776 | 777 | var style = getStyle( elem ); 778 | 779 | // if hidden, everything is 0 780 | if ( style.display === 'none' ) { 781 | return getZeroSize(); 782 | } 783 | 784 | var size = {}; 785 | size.width = elem.offsetWidth; 786 | size.height = elem.offsetHeight; 787 | 788 | var isBorderBox = size.isBorderBox = !!( boxSizingProp && 789 | style[ boxSizingProp ] && style[ boxSizingProp ] === 'border-box' ); 790 | 791 | // get all measurements 792 | for ( var i=0, len = measurements.length; i < len; i++ ) { 793 | var measurement = measurements[i]; 794 | var value = style[ measurement ]; 795 | var num = parseFloat( value ); 796 | // any 'auto', 'medium' value will be 0 797 | size[ measurement ] = !isNaN( num ) ? num : 0; 798 | } 799 | 800 | var paddingWidth = size.paddingLeft + size.paddingRight; 801 | var paddingHeight = size.paddingTop + size.paddingBottom; 802 | var marginWidth = size.marginLeft + size.marginRight; 803 | var marginHeight = size.marginTop + size.marginBottom; 804 | var borderWidth = size.borderLeftWidth + size.borderRightWidth; 805 | var borderHeight = size.borderTopWidth + size.borderBottomWidth; 806 | 807 | var isBorderBoxSizeOuter = isBorderBox && isBoxSizeOuter; 808 | 809 | // overwrite width and height if we can get it from style 810 | var styleWidth = getStyleSize( style.width ); 811 | if ( styleWidth !== false ) { 812 | size.width = styleWidth + 813 | // add padding and border unless it's already including it 814 | ( isBorderBoxSizeOuter ? 0 : paddingWidth + borderWidth ); 815 | } 816 | 817 | var styleHeight = getStyleSize( style.height ); 818 | if ( styleHeight !== false ) { 819 | size.height = styleHeight + 820 | // add padding and border unless it's already including it 821 | ( isBorderBoxSizeOuter ? 0 : paddingHeight + borderHeight ); 822 | } 823 | 824 | size.innerWidth = size.width - ( paddingWidth + borderWidth ); 825 | size.innerHeight = size.height - ( paddingHeight + borderHeight ); 826 | 827 | size.outerWidth = size.width + marginWidth; 828 | size.outerHeight = size.height + marginHeight; 829 | 830 | return size; 831 | } 832 | 833 | return getSize; 834 | 835 | } 836 | 837 | // transport 838 | if ( typeof define === 'function' && define.amd ) { 839 | // AMD 840 | define( 'get-size/get-size',[ 'get-style-property/get-style-property' ], defineGetSize ); 841 | } else { 842 | // browser global 843 | window.getSize = defineGetSize( window.getStyleProperty ); 844 | } 845 | 846 | })( window ); 847 | 848 | /*! 849 | * Draggabilly v1.1.2 850 | * Make that shiz draggable 851 | * http://draggabilly.desandro.com 852 | * MIT license 853 | */ 854 | 855 | ( function( window ) { 856 | 857 | 858 | 859 | // vars 860 | var document = window.document; 861 | 862 | // -------------------------- helpers -------------------------- // 863 | 864 | // extend objects 865 | function extend( a, b ) { 866 | for ( var prop in b ) { 867 | a[ prop ] = b[ prop ]; 868 | } 869 | return a; 870 | } 871 | 872 | function noop() {} 873 | 874 | // ----- get style ----- // 875 | 876 | var defView = document.defaultView; 877 | 878 | var getStyle = defView && defView.getComputedStyle ? 879 | function( elem ) { 880 | return defView.getComputedStyle( elem, null ); 881 | } : 882 | function( elem ) { 883 | return elem.currentStyle; 884 | }; 885 | 886 | 887 | // http://stackoverflow.com/a/384380/182183 888 | var isElement = ( typeof HTMLElement === 'object' ) ? 889 | function isElementDOM2( obj ) { 890 | return obj instanceof HTMLElement; 891 | } : 892 | function isElementQuirky( obj ) { 893 | return obj && typeof obj === 'object' && 894 | obj.nodeType === 1 && typeof obj.nodeName === 'string'; 895 | }; 896 | 897 | // -------------------------- requestAnimationFrame -------------------------- // 898 | 899 | // https://gist.github.com/1866474 900 | 901 | var lastTime = 0; 902 | var prefixes = 'webkit moz ms o'.split(' '); 903 | // get unprefixed rAF and cAF, if present 904 | var requestAnimationFrame = window.requestAnimationFrame; 905 | var cancelAnimationFrame = window.cancelAnimationFrame; 906 | // loop through vendor prefixes and get prefixed rAF and cAF 907 | var prefix; 908 | for( var i = 0; i < prefixes.length; i++ ) { 909 | if ( requestAnimationFrame && cancelAnimationFrame ) { 910 | break; 911 | } 912 | prefix = prefixes[i]; 913 | requestAnimationFrame = requestAnimationFrame || window[ prefix + 'RequestAnimationFrame' ]; 914 | cancelAnimationFrame = cancelAnimationFrame || window[ prefix + 'CancelAnimationFrame' ] || 915 | window[ prefix + 'CancelRequestAnimationFrame' ]; 916 | } 917 | 918 | // fallback to setTimeout and clearTimeout if either request/cancel is not supported 919 | if ( !requestAnimationFrame || !cancelAnimationFrame ) { 920 | requestAnimationFrame = function( callback ) { 921 | var currTime = new Date().getTime(); 922 | var timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) ); 923 | var id = window.setTimeout( function() { 924 | callback( currTime + timeToCall ); 925 | }, timeToCall ); 926 | lastTime = currTime + timeToCall; 927 | return id; 928 | }; 929 | 930 | cancelAnimationFrame = function( id ) { 931 | window.clearTimeout( id ); 932 | }; 933 | } 934 | 935 | // -------------------------- definition -------------------------- // 936 | 937 | function draggabillyDefinition( classie, EventEmitter, eventie, getStyleProperty, getSize ) { 938 | 939 | // -------------------------- support -------------------------- // 940 | 941 | var transformProperty = getStyleProperty('transform'); 942 | // TODO fix quick & dirty check for 3D support 943 | var is3d = !!getStyleProperty('perspective'); 944 | 945 | // -------------------------- -------------------------- // 946 | 947 | function Draggabilly( element, options ) { 948 | // querySelector if string 949 | this.element = typeof element === 'string' ? 950 | document.querySelector( element ) : element; 951 | 952 | this.options = extend( {}, this.options ); 953 | extend( this.options, options ); 954 | 955 | this._create(); 956 | } 957 | 958 | // inherit EventEmitter methods 959 | extend( Draggabilly.prototype, EventEmitter.prototype ); 960 | 961 | Draggabilly.prototype.options = { 962 | }; 963 | 964 | Draggabilly.prototype._create = function() { 965 | 966 | // properties 967 | this.position = {}; 968 | this._getPosition(); 969 | 970 | this.startPoint = { x: 0, y: 0 }; 971 | this.dragPoint = { x: 0, y: 0 }; 972 | 973 | this.startPosition = extend( {}, this.position ); 974 | 975 | // set relative positioning 976 | var style = getStyle( this.element ); 977 | if ( style.position !== 'relative' && style.position !== 'absolute' ) { 978 | this.element.style.position = 'relative'; 979 | } 980 | 981 | this.enable(); 982 | this.setHandles(); 983 | 984 | }; 985 | 986 | /** 987 | * set this.handles and bind start events to 'em 988 | */ 989 | Draggabilly.prototype.setHandles = function() { 990 | this.handles = this.options.handle ? 991 | this.element.querySelectorAll( this.options.handle ) : [ this.element ]; 992 | 993 | this.bindHandles( true ); 994 | }; 995 | 996 | // -------------------------- bind -------------------------- // 997 | 998 | /** 999 | * @param {Boolean} isBind - will unbind if falsey 1000 | */ 1001 | Draggabilly.prototype.bindHandles = function( isBind ) { 1002 | var binder; 1003 | if ( window.navigator.pointerEnabled ) { 1004 | binder = this.bindPointer; 1005 | } else if ( window.navigator.msPointerEnabled ) { 1006 | binder = this.bindMSPointer; 1007 | } else { 1008 | binder = this.bindMouseTouch; 1009 | } 1010 | // munge isBind, default to true 1011 | isBind = isBind === undefined ? true : !!isBind; 1012 | for ( var i=0, len = this.handles.length; i < len; i++ ) { 1013 | var handle = this.handles[i]; 1014 | binder.call( this, handle, isBind ); 1015 | } 1016 | }; 1017 | 1018 | Draggabilly.prototype.bindPointer = function( handle, isBind ) { 1019 | // W3C Pointer Events, IE11. See https://coderwall.com/p/mfreca 1020 | var bindMethod = isBind ? 'bind' : 'unbind'; 1021 | eventie[ bindMethod ]( handle, 'pointerdown', this ); 1022 | // disable scrolling on the element 1023 | handle.style.touchAction = isBind ? 'none' : ''; 1024 | }; 1025 | 1026 | Draggabilly.prototype.bindMSPointer = function( handle, isBind ) { 1027 | // IE10 Pointer Events 1028 | var bindMethod = isBind ? 'bind' : 'unbind'; 1029 | eventie[ bindMethod ]( handle, 'MSPointerDown', this ); 1030 | // disable scrolling on the element 1031 | handle.style.msTouchAction = isBind ? 'none' : ''; 1032 | }; 1033 | 1034 | Draggabilly.prototype.bindMouseTouch = function( handle, isBind ) { 1035 | // listen for both, for devices like Chrome Pixel 1036 | // which has touch and mouse events 1037 | var bindMethod = isBind ? 'bind' : 'unbind'; 1038 | eventie[ bindMethod ]( handle, 'mousedown', this ); 1039 | eventie[ bindMethod ]( handle, 'touchstart', this ); 1040 | // TODO re-enable img.ondragstart when unbinding 1041 | if ( isBind ) { 1042 | disableImgOndragstart( handle ); 1043 | } 1044 | }; 1045 | 1046 | // remove default dragging interaction on all images in IE8 1047 | // IE8 does its own drag thing on images, which messes stuff up 1048 | 1049 | function noDragStart() { 1050 | return false; 1051 | } 1052 | 1053 | // TODO replace this with a IE8 test 1054 | var isIE8 = 'attachEvent' in document.documentElement; 1055 | 1056 | // IE8 only 1057 | var disableImgOndragstart = !isIE8 ? noop : function( handle ) { 1058 | 1059 | if ( handle.nodeName === 'IMG' ) { 1060 | handle.ondragstart = noDragStart; 1061 | } 1062 | 1063 | var images = handle.querySelectorAll('img'); 1064 | for ( var i=0, len = images.length; i < len; i++ ) { 1065 | var img = images[i]; 1066 | img.ondragstart = noDragStart; 1067 | } 1068 | }; 1069 | 1070 | // -------------------------- position -------------------------- // 1071 | 1072 | // get left/top position from style 1073 | Draggabilly.prototype._getPosition = function() { 1074 | // properties 1075 | var style = getStyle( this.element ); 1076 | 1077 | var x = parseInt( style.left, 10 ); 1078 | var y = parseInt( style.top, 10 ); 1079 | 1080 | // clean up 'auto' or other non-integer values 1081 | this.position.x = isNaN( x ) ? 0 : x; 1082 | this.position.y = isNaN( y ) ? 0 : y; 1083 | 1084 | this._addTransformPosition( style ); 1085 | }; 1086 | 1087 | // add transform: translate( x, y ) to position 1088 | Draggabilly.prototype._addTransformPosition = function( style ) { 1089 | if ( !transformProperty ) { 1090 | return; 1091 | } 1092 | var transform = style[ transformProperty ]; 1093 | // bail out if value is 'none' 1094 | if ( transform.indexOf('matrix') !== 0 ) { 1095 | return; 1096 | } 1097 | // split matrix(1, 0, 0, 1, x, y) 1098 | var matrixValues = transform.split(','); 1099 | // translate X value is in 12th or 4th position 1100 | var xIndex = transform.indexOf('matrix3d') === 0 ? 12 : 4; 1101 | var translateX = parseInt( matrixValues[ xIndex ], 10 ); 1102 | // translate Y value is in 13th or 5th position 1103 | var translateY = parseInt( matrixValues[ xIndex + 1 ], 10 ); 1104 | this.position.x += translateX; 1105 | this.position.y += translateY; 1106 | }; 1107 | 1108 | // -------------------------- events -------------------------- // 1109 | 1110 | // trigger handler methods for events 1111 | Draggabilly.prototype.handleEvent = function( event ) { 1112 | var method = 'on' + event.type; 1113 | if ( this[ method ] ) { 1114 | this[ method ]( event ); 1115 | } 1116 | }; 1117 | 1118 | // returns the touch that we're keeping track of 1119 | Draggabilly.prototype.getTouch = function( touches ) { 1120 | for ( var i=0, len = touches.length; i < len; i++ ) { 1121 | var touch = touches[i]; 1122 | if ( touch.identifier === this.pointerIdentifier ) { 1123 | return touch; 1124 | } 1125 | } 1126 | }; 1127 | 1128 | // ----- start event ----- // 1129 | 1130 | Draggabilly.prototype.onmousedown = function( event ) { 1131 | // dismiss clicks from right or middle buttons 1132 | var button = event.button; 1133 | if ( button && ( button !== 0 && button !== 1 ) ) { 1134 | return; 1135 | } 1136 | this.dragStart( event, event ); 1137 | }; 1138 | 1139 | Draggabilly.prototype.ontouchstart = function( event ) { 1140 | // disregard additional touches 1141 | if ( this.isDragging ) { 1142 | return; 1143 | } 1144 | 1145 | this.dragStart( event, event.changedTouches[0] ); 1146 | }; 1147 | 1148 | Draggabilly.prototype.onMSPointerDown = 1149 | Draggabilly.prototype.onpointerdown = function( event ) { 1150 | // disregard additional touches 1151 | if ( this.isDragging ) { 1152 | return; 1153 | } 1154 | 1155 | this.dragStart( event, event ); 1156 | }; 1157 | 1158 | function setPointerPoint( point, pointer ) { 1159 | point.x = pointer.pageX !== undefined ? pointer.pageX : pointer.clientX; 1160 | point.y = pointer.pageY !== undefined ? pointer.pageY : pointer.clientY; 1161 | } 1162 | 1163 | // hash of events to be bound after start event 1164 | var postStartEvents = { 1165 | mousedown: [ 'mousemove', 'mouseup' ], 1166 | touchstart: [ 'touchmove', 'touchend', 'touchcancel' ], 1167 | pointerdown: [ 'pointermove', 'pointerup', 'pointercancel' ], 1168 | MSPointerDown: [ 'MSPointerMove', 'MSPointerUp', 'MSPointerCancel' ] 1169 | }; 1170 | 1171 | /** 1172 | * drag start 1173 | * @param {Event} event 1174 | * @param {Event or Touch} pointer 1175 | */ 1176 | Draggabilly.prototype.dragStart = function( event, pointer ) { 1177 | if ( !this.isEnabled ) { 1178 | return; 1179 | } 1180 | 1181 | if ( event.preventDefault ) { 1182 | event.preventDefault(); 1183 | } else { 1184 | event.returnValue = false; 1185 | } 1186 | 1187 | // save pointer identifier to match up touch events 1188 | this.pointerIdentifier = pointer.pointerId !== undefined ? 1189 | // pointerId for pointer events, touch.indentifier for touch events 1190 | pointer.pointerId : pointer.identifier; 1191 | 1192 | this._getPosition(); 1193 | 1194 | this.measureContainment(); 1195 | 1196 | // point where drag began 1197 | setPointerPoint( this.startPoint, pointer ); 1198 | // position _when_ drag began 1199 | this.startPosition.x = this.position.x; 1200 | this.startPosition.y = this.position.y; 1201 | 1202 | // reset left/top style 1203 | this.setLeftTop(); 1204 | 1205 | this.dragPoint.x = 0; 1206 | this.dragPoint.y = 0; 1207 | 1208 | // bind move and end events 1209 | this._bindEvents({ 1210 | // get proper events to match start event 1211 | events: postStartEvents[ event.type ], 1212 | // IE8 needs to be bound to document 1213 | node: event.preventDefault ? window : document 1214 | }); 1215 | 1216 | classie.add( this.element, 'is-dragging' ); 1217 | 1218 | // reset isDragging flag 1219 | this.isDragging = true; 1220 | 1221 | this.emitEvent( 'dragStart', [ this, event, pointer ] ); 1222 | 1223 | // start animation 1224 | this.animate(); 1225 | }; 1226 | 1227 | Draggabilly.prototype._bindEvents = function( args ) { 1228 | for ( var i=0, len = args.events.length; i < len; i++ ) { 1229 | var event = args.events[i]; 1230 | eventie.bind( args.node, event, this ); 1231 | } 1232 | // save these arguments 1233 | this._boundEvents = args; 1234 | }; 1235 | 1236 | Draggabilly.prototype._unbindEvents = function() { 1237 | var args = this._boundEvents; 1238 | // IE8 can trigger dragEnd twice, check for _boundEvents 1239 | if ( !args || !args.events ) { 1240 | return; 1241 | } 1242 | 1243 | for ( var i=0, len = args.events.length; i < len; i++ ) { 1244 | var event = args.events[i]; 1245 | eventie.unbind( args.node, event, this ); 1246 | } 1247 | delete this._boundEvents; 1248 | }; 1249 | 1250 | Draggabilly.prototype.measureContainment = function() { 1251 | var containment = this.options.containment; 1252 | if ( !containment ) { 1253 | return; 1254 | } 1255 | 1256 | this.size = getSize( this.element ); 1257 | var elemRect = this.element.getBoundingClientRect(); 1258 | 1259 | // use element if element 1260 | var container = isElement( containment ) ? containment : 1261 | // fallback to querySelector if string 1262 | typeof containment === 'string' ? document.querySelector( containment ) : 1263 | // otherwise just `true`, use the parent 1264 | this.element.parentNode; 1265 | 1266 | this.containerSize = getSize( container ); 1267 | var containerRect = container.getBoundingClientRect(); 1268 | 1269 | this.relativeStartPosition = { 1270 | x: elemRect.left - containerRect.left, 1271 | y: elemRect.top - containerRect.top 1272 | }; 1273 | }; 1274 | 1275 | // ----- move event ----- // 1276 | 1277 | Draggabilly.prototype.onmousemove = function( event ) { 1278 | this.dragMove( event, event ); 1279 | }; 1280 | 1281 | Draggabilly.prototype.onMSPointerMove = 1282 | Draggabilly.prototype.onpointermove = function( event ) { 1283 | if ( event.pointerId === this.pointerIdentifier ) { 1284 | this.dragMove( event, event ); 1285 | } 1286 | }; 1287 | 1288 | Draggabilly.prototype.ontouchmove = function( event ) { 1289 | var touch = this.getTouch( event.changedTouches ); 1290 | if ( touch ) { 1291 | this.dragMove( event, touch ); 1292 | } 1293 | }; 1294 | 1295 | /** 1296 | * drag move 1297 | * @param {Event} event 1298 | * @param {Event or Touch} pointer 1299 | */ 1300 | Draggabilly.prototype.dragMove = function( event, pointer ) { 1301 | 1302 | setPointerPoint( this.dragPoint, pointer ); 1303 | var dragX = this.dragPoint.x - this.startPoint.x; 1304 | var dragY = this.dragPoint.y - this.startPoint.y; 1305 | 1306 | var grid = this.options.grid; 1307 | var gridX = grid && grid[0]; 1308 | var gridY = grid && grid[1]; 1309 | 1310 | dragX = applyGrid( dragX, gridX ); 1311 | dragY = applyGrid( dragY, gridY ); 1312 | 1313 | dragX = this.containDrag( 'x', dragX, gridX ); 1314 | dragY = this.containDrag( 'y', dragY, gridY ); 1315 | 1316 | // constrain to axis 1317 | dragX = this.options.axis === 'y' ? 0 : dragX; 1318 | dragY = this.options.axis === 'x' ? 0 : dragY; 1319 | 1320 | this.position.x = this.startPosition.x + dragX; 1321 | this.position.y = this.startPosition.y + dragY; 1322 | // set dragPoint properties 1323 | this.dragPoint.x = dragX; 1324 | this.dragPoint.y = dragY; 1325 | 1326 | this.emitEvent( 'dragMove', [ this, event, pointer ] ); 1327 | }; 1328 | 1329 | function applyGrid( value, grid, method ) { 1330 | method = method || 'round'; 1331 | return grid ? Math[ method ]( value / grid ) * grid : value; 1332 | } 1333 | 1334 | Draggabilly.prototype.containDrag = function( axis, drag, grid ) { 1335 | if ( !this.options.containment ) { 1336 | return drag; 1337 | } 1338 | var measure = axis === 'x' ? 'width' : 'height'; 1339 | 1340 | var rel = this.relativeStartPosition[ axis ]; 1341 | var min = applyGrid( -rel, grid, 'ceil' ); 1342 | var max = this.containerSize[ measure ] - rel - this.size[ measure ]; 1343 | max = applyGrid( max, grid, 'floor' ); 1344 | return Math.min( max, Math.max( min, drag ) ); 1345 | }; 1346 | 1347 | // ----- end event ----- // 1348 | 1349 | Draggabilly.prototype.onmouseup = function( event ) { 1350 | this.dragEnd( event, event ); 1351 | }; 1352 | 1353 | Draggabilly.prototype.onMSPointerUp = 1354 | Draggabilly.prototype.onpointerup = function( event ) { 1355 | if ( event.pointerId === this.pointerIdentifier ) { 1356 | this.dragEnd( event, event ); 1357 | } 1358 | }; 1359 | 1360 | Draggabilly.prototype.ontouchend = function( event ) { 1361 | var touch = this.getTouch( event.changedTouches ); 1362 | if ( touch ) { 1363 | this.dragEnd( event, touch ); 1364 | } 1365 | }; 1366 | 1367 | /** 1368 | * drag end 1369 | * @param {Event} event 1370 | * @param {Event or Touch} pointer 1371 | */ 1372 | Draggabilly.prototype.dragEnd = function( event, pointer ) { 1373 | this.isDragging = false; 1374 | 1375 | delete this.pointerIdentifier; 1376 | 1377 | // use top left position when complete 1378 | if ( transformProperty ) { 1379 | this.element.style[ transformProperty ] = ''; 1380 | this.setLeftTop(); 1381 | } 1382 | 1383 | // remove events 1384 | this._unbindEvents(); 1385 | 1386 | classie.remove( this.element, 'is-dragging' ); 1387 | 1388 | this.emitEvent( 'dragEnd', [ this, event, pointer ] ); 1389 | 1390 | }; 1391 | 1392 | // ----- cancel event ----- // 1393 | 1394 | // coerce to end event 1395 | 1396 | Draggabilly.prototype.onMSPointerCancel = 1397 | Draggabilly.prototype.onpointercancel = function( event ) { 1398 | if ( event.pointerId === this.pointerIdentifier ) { 1399 | this.dragEnd( event, event ); 1400 | } 1401 | }; 1402 | 1403 | Draggabilly.prototype.ontouchcancel = function( event ) { 1404 | var touch = this.getTouch( event.changedTouches ); 1405 | this.dragEnd( event, touch ); 1406 | }; 1407 | 1408 | // -------------------------- animation -------------------------- // 1409 | 1410 | Draggabilly.prototype.animate = function() { 1411 | // only render and animate if dragging 1412 | if ( !this.isDragging ) { 1413 | return; 1414 | } 1415 | 1416 | this.positionDrag(); 1417 | 1418 | var _this = this; 1419 | requestAnimationFrame( function animateFrame() { 1420 | _this.animate(); 1421 | }); 1422 | 1423 | }; 1424 | 1425 | // transform translate function 1426 | var translate = is3d ? 1427 | function( x, y ) { 1428 | return 'translate3d( ' + x + 'px, ' + y + 'px, 0)'; 1429 | } : 1430 | function( x, y ) { 1431 | return 'translate( ' + x + 'px, ' + y + 'px)'; 1432 | }; 1433 | 1434 | // left/top positioning 1435 | Draggabilly.prototype.setLeftTop = function() { 1436 | this.element.style.left = this.position.x + 'px'; 1437 | this.element.style.top = this.position.y + 'px'; 1438 | }; 1439 | 1440 | Draggabilly.prototype.positionDrag = transformProperty ? 1441 | function() { 1442 | // position with transform 1443 | this.element.style[ transformProperty ] = translate( this.dragPoint.x, this.dragPoint.y ); 1444 | } : Draggabilly.prototype.setLeftTop; 1445 | 1446 | // ----- ----- // 1447 | 1448 | Draggabilly.prototype.enable = function() { 1449 | this.isEnabled = true; 1450 | }; 1451 | 1452 | Draggabilly.prototype.disable = function() { 1453 | this.isEnabled = false; 1454 | if ( this.isDragging ) { 1455 | this.dragEnd(); 1456 | } 1457 | }; 1458 | 1459 | Draggabilly.prototype.destroy = function() { 1460 | this.disable(); 1461 | // reset styles 1462 | if ( transformProperty ) { 1463 | this.element.style[ transformProperty ] = ''; 1464 | } 1465 | this.element.style.left = ''; 1466 | this.element.style.top = ''; 1467 | this.element.style.position = ''; 1468 | // unbind handles 1469 | this.bindHandles( false ); 1470 | }; 1471 | 1472 | // ----- ----- // 1473 | 1474 | return Draggabilly; 1475 | 1476 | } // end definition 1477 | 1478 | // -------------------------- transport -------------------------- // 1479 | 1480 | if ( typeof define === 'function' && define.amd ) { 1481 | // AMD 1482 | define( [ 1483 | 'classie/classie', 1484 | 'eventEmitter/EventEmitter', 1485 | 'eventie/eventie', 1486 | 'get-style-property/get-style-property', 1487 | 'get-size/get-size' 1488 | ], 1489 | draggabillyDefinition ); 1490 | } else if ( typeof exports === 'object' ) { 1491 | // CommonJS 1492 | module.exports = draggabillyDefinition( 1493 | require('desandro-classie'), 1494 | require('wolfy87-eventemitter'), 1495 | require('eventie'), 1496 | require('desandro-get-style-property'), 1497 | require('get-size') 1498 | ); 1499 | } else { 1500 | // browser global 1501 | window.Draggabilly = draggabillyDefinition( 1502 | window.classie, 1503 | window.EventEmitter, 1504 | window.eventie, 1505 | window.getStyleProperty, 1506 | window.getSize 1507 | ); 1508 | } 1509 | 1510 | })( window ); 1511 | 1512 | -------------------------------------------------------------------------------- /js/modal.js: -------------------------------------------------------------------------------- 1 | var Modal = function(){ 2 | 3 | var modalOverlay = document.createElement('div'); 4 | modalOverlay.setAttribute('class', 'modal-overlay'); 5 | document.body.appendChild(modalOverlay); 6 | 7 | var config = { 8 | $modal: document.querySelector('.modal'), 9 | $modalOverlay: document.querySelector('.modal-overlay'), 10 | modalOverlayColor: 'rgba(255,255,255,0.7)', 11 | $modalClose: document.querySelector('.modal-close'), 12 | $modalTrigger: document.querySelector('.modal-trigger'), 13 | $draggie: new Draggabilly( document.querySelector('.modal'), { 14 | handle: '.modal-header', 15 | containment: 'html' 16 | }) 17 | } 18 | config.$modalOverlay.style.background = config.modalOverlayColor; 19 | 20 | 21 | var Modal = { 22 | createEvent: function(eventName, callback){ 23 | 24 | var event = document.createEvent('HTMLEvents'); 25 | event.initEvent(eventName, true, false); 26 | config.$modal.dispatchEvent(event); 27 | 28 | if (callback && typeof(callback) === "function") { 29 | callback(); 30 | } 31 | 32 | }, 33 | open: function(){ 34 | 35 | config.$modal.style.visibility = "visible"; 36 | config.$modal.classList.add('opening'); 37 | config.$modal.classList.remove('closing'); 38 | 39 | config.$modalOverlay.style.visibility = "visible"; 40 | config.$modalOverlay.classList.add('opening'); 41 | config.$modalOverlay.classList.remove('closing'); 42 | 43 | Modal.createEvent('modalOpened', function(){ 44 | //callback 45 | }) 46 | 47 | }, 48 | close: function(){ 49 | 50 | config.$modal.classList.add('closing'); 51 | config.$modal.classList.remove('opening'); 52 | config.$modalOverlay.classList.add('closing'); 53 | config.$modalOverlay.classList.remove('opening'); 54 | 55 | var timer = window.setTimeout(function(){ 56 | 57 | config.$modal.style.visibility = "hidden"; 58 | config.$modalOverlay.style.visibility = "hidden"; 59 | 60 | Modal.createEvent('modalClosed', function(){ 61 | //callback 62 | }); 63 | },500); 64 | 65 | }, 66 | init: function(){ 67 | 68 | config.$draggie.on( 'dragStart', function( instance, event, pointer ){ 69 | console.log( 'dragMove on ' + event.type + pointer.pageX + ', ' + pointer.pageY + ' position at ' + instance.position.x + ', ' + instance.position.y ); 70 | }); 71 | 72 | config.$modalClose.addEventListener("click", function(){ 73 | 74 | Modal.close(); 75 | 76 | }, false); 77 | 78 | config.$modalTrigger.addEventListener("click", function(){ 79 | 80 | Modal.open(); 81 | 82 | }, false); 83 | 84 | window.addEventListener("keydown", function(e){ 85 | console.log(e.keyCode); 86 | 87 | if (e.keyCode == 27) { 88 | 89 | if( config.$modal.style.display != 'none'){ 90 | Modal.close(); 91 | } 92 | 93 | } 94 | 95 | if (e.keyCode == 38 && e.ctrlKey) { 96 | console.log('full'); 97 | 98 | if(config.$modal.classList.contains("is-right") || config.$modal.classList.contains("is-left")){ 99 | config.$modal.classList.remove("is-right"); 100 | config.$modal.classList.remove("is-left"); 101 | config.$draggie.enable(); 102 | } else { 103 | config.$draggie.disable(); 104 | config.$modal.classList.add("is-full"); 105 | } 106 | 107 | } 108 | 109 | if (e.keyCode == 37 && e.ctrlKey) { 110 | console.log('left'); 111 | 112 | if(config.$modal.classList.contains("is-right") || config.$modal.classList.contains("is-full")){ 113 | config.$modal.classList.remove("is-right"); 114 | config.$modal.classList.remove("is-full"); 115 | config.$draggie.enable(); 116 | } else { 117 | config.$modal.classList.add("is-left"); 118 | config.$draggie.disable(); 119 | } 120 | 121 | } 122 | if (e.keyCode == 39 && e.ctrlKey) { 123 | console.log('right'); 124 | 125 | if(config.$modal.classList.contains("is-left") || config.$modal.classList.contains("is-full")){ 126 | config.$modal.classList.remove("is-left"); 127 | config.$modal.classList.remove("is-full"); 128 | config.$draggie.enable(); 129 | } else { 130 | config.$modal.classList.add("is-right"); 131 | config.$draggie.disable(); 132 | } 133 | 134 | } 135 | 136 | 137 | if (e.keyCode == 40 && e.ctrlKey) { 138 | 139 | if(config.$modal.classList.contains("is-full")){ 140 | config.$modal.classList.remove("is-full"); 141 | config.$draggie.enable(); 142 | } 143 | 144 | } 145 | 146 | }); 147 | 148 | config.$modal.addEventListener("modalOpened", function(){ 149 | console.log("Modal Opened"); 150 | }) 151 | config.$modal.addEventListener("modalClosed", function(){ 152 | console.log("Modal Closed"); 153 | }) 154 | 155 | } 156 | } 157 | 158 | Modal.config = config; 159 | return Modal; 160 | 161 | }(); --------------------------------------------------------------------------------