draggabilly.pkgd.js 44.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*!
 * Draggabilly PACKAGED v1.0.5
 * Make that shiz draggable
 * http://draggabilly.desandro.com
 */

/*!
 * classie - class helper functions
 * from bonzo https://github.com/ded/bonzo
 * 
 * classie.has( elem, 'my-class' ) -> true/false
 * classie.add( elem, 'my-new-class' )
 * classie.remove( elem, 'my-unwanted-class' )
 * classie.toggle( elem, 'my-class' )
 */

/*jshint browser: true, strict: true, undef: true */
/*global define: false */

( function( window ) {

22
    'use strict';
23 24 25

// class helper functions from bonzo https://github.com/ded/bonzo

26 27 28
    function classReg( className ) {
        return new RegExp("(^|\\s+)" + className + "(\\s+|$)");
    }
29 30 31

// classList support for class management
// altho to be fair, the api sucks because it won't accept multiple classes at once
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    var hasClass, addClass, removeClass;

    if ( 'classList' in document.documentElement ) {
        hasClass = function( elem, c ) {
            return elem.classList.contains( c );
        };
        addClass = function( elem, c ) {
            elem.classList.add( c );
        };
        removeClass = function( elem, c ) {
            elem.classList.remove( c );
        };
    }
    else {
        hasClass = function( elem, c ) {
            return classReg( c ).test( elem.className );
        };
        addClass = function( elem, c ) {
            if ( !hasClass( elem, c ) ) {
                elem.className = elem.className + ' ' + c;
            }
        };
        removeClass = function( elem, c ) {
            elem.className = elem.className.replace( classReg( c ), ' ' );
        };
    }

    function toggleClass( elem, c ) {
        var fn = hasClass( elem, c ) ? removeClass : addClass;
        fn( elem, c );
62
    }
63 64 65 66 67 68 69 70 71 72 73 74 75

    var classie = {
        // full names
        hasClass: hasClass,
        addClass: addClass,
        removeClass: removeClass,
        toggleClass: toggleClass,
        // short names
        has: hasClass,
        add: addClass,
        remove: removeClass,
        toggle: toggleClass
    };
76 77

// transport
78 79 80 81 82 83 84
    if ( typeof define === 'function' && define.amd ) {
        // AMD
        define("classie", classie);
    } else {
        // browser global
        window.classie = classie;
    }
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

})( window );

/*!
 * eventie v1.0.3
 * event binding helper
 *   eventie.bind( elem, 'click', myFn )
 *   eventie.unbind( elem, 'click', myFn )
 */

/*jshint browser: true, undef: true, unused: true */
/*global define: false */

( function( window ) {

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    'use strict';

    var docElem = document.documentElement;

    var bind = function() {};

    if ( docElem.addEventListener ) {
        bind = function( obj, type, fn ) {
            obj.addEventListener( type, fn, false );
        };
    } else if ( docElem.attachEvent ) {
        bind = function( obj, type, fn ) {
            obj[ type + fn ] = fn.handleEvent ?
                function() {
                    var event = window.event;
                    // add event.target
                    event.target = event.target || event.srcElement;
                    fn.handleEvent.call( fn, event );
                } :
                function() {
                    var event = window.event;
                    // add event.target
                    event.target = event.target || event.srcElement;
                    fn.call( obj, event );
                };
            obj.attachEvent( "on" + type, obj[ type + fn ] );
        };
127 128
    }

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    var unbind = function() {};

    if ( docElem.removeEventListener ) {
        unbind = function( obj, type, fn ) {
            obj.removeEventListener( type, fn, false );
        };
    } else if ( docElem.detachEvent ) {
        unbind = function( obj, type, fn ) {
            obj.detachEvent( "on" + type, obj[ type + fn ] );
            try {
                delete obj[ type + fn ];
            } catch ( err ) {
                // can't delete window object properties
                obj[ type + fn ] = undefined;
            }
        };
    }
146

147 148 149 150
    var eventie = {
        bind: bind,
        unbind: unbind
    };
151 152

// transport
153 154 155 156 157 158 159
    if ( typeof define === 'function' && define.amd ) {
        // AMD
        define("eventie", eventie);
    } else {
        // browser global
        window.eventie = eventie;
    }
160 161 162 163

})( this );

/*!
164
 * EventEmitter v4.2.4 - git.io/ee
165 166 167 168 169
 * Oliver Caldwell
 * MIT license
 * @preserve
 */

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
(function () {
    'use strict';

    /**
     * Class for managing events.
     * Can be extended to provide event functionality in other classes.
     *
     * @class EventEmitter Manages event registering and emitting.
     */
    function EventEmitter() {}

    // Shortcuts to improve speed and size

    // Easy access to the prototype
    var proto = EventEmitter.prototype;

    /**
     * Finds the index of the listener for the event in it's storage array.
     *
     * @param {Function[]} listeners Array of listeners to search through.
     * @param {Function} listener Method to look for.
     * @return {Number} Index of the specified listener, -1 if not found
     * @api private
     */
    function indexOfListener(listeners, listener) {
        var i = listeners.length;
        while (i--) {
            if (listeners[i].listener === listener) {
                return i;
            }
        }

        return -1;
    }

    /**
     * Alias a method while keeping the context correct, to allow for overwriting of target method.
     *
     * @param {String} name The name of the target method.
     * @return {Function} The aliased method
     * @api private
     */
    function alias(name) {
        return function aliasClosure() {
            return this[name].apply(this, arguments);
        };
    }

    /**
     * Returns the listener array for the specified event.
     * Will initialise the event object and listener arrays if required.
     * 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.
     * Each property in the object response is an array of listener functions.
     *
     * @param {String|RegExp} evt Name of the event to return the listeners from.
     * @return {Function[]|Object} All listener functions for the event.
     */
    proto.getListeners = function getListeners(evt) {
        var events = this._getEvents();
        var response;
        var key;

        // Return a concatenated array of all matching events if
        // the selector is a regular expression.
        if (typeof evt === 'object') {
            response = {};
            for (key in events) {
                if (events.hasOwnProperty(key) && evt.test(key)) {
                    response[key] = events[key];
                }
            }
        }
        else {
            response = events[evt] || (events[evt] = []);
        }

        return response;
    };

    /**
     * Takes a list of listener objects and flattens it into a list of listener functions.
     *
     * @param {Object[]} listeners Raw listener objects.
     * @return {Function[]} Just the listener functions.
     */
    proto.flattenListeners = function flattenListeners(listeners) {
        var flatListeners = [];
        var i;

        for (i = 0; i < listeners.length; i += 1) {
            flatListeners.push(listeners[i].listener);
        }

        return flatListeners;
    };

    /**
     * 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.
     *
     * @param {String|RegExp} evt Name of the event to return the listeners from.
     * @return {Object} All listener functions for an event in an object.
     */
    proto.getListenersAsObject = function getListenersAsObject(evt) {
        var listeners = this.getListeners(evt);
        var response;

        if (listeners instanceof Array) {
            response = {};
            response[evt] = listeners;
        }

        return response || listeners;
    };

    /**
     * Adds a listener function to the specified event.
     * The listener will not be added if it is a duplicate.
     * If the listener returns true then it will be removed after it is called.
     * If you pass a regular expression as the event name then the listener will be added to all events that match it.
     *
     * @param {String|RegExp} evt Name of the event to attach the listener to.
     * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.addListener = function addListener(evt, listener) {
        var listeners = this.getListenersAsObject(evt);
        var listenerIsWrapped = typeof listener === 'object';
        var key;

        for (key in listeners) {
            if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) {
                listeners[key].push(listenerIsWrapped ? listener : {
                    listener: listener,
                    once: false
                });
            }
        }

        return this;
    };

    /**
     * Alias of addListener
     */
    proto.on = alias('addListener');

    /**
     * Semi-alias of addListener. It will add a listener that will be
     * automatically removed after it's first execution.
     *
     * @param {String|RegExp} evt Name of the event to attach the listener to.
     * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.addOnceListener = function addOnceListener(evt, listener) {
        return this.addListener(evt, {
            listener: listener,
            once: true
        });
    };

    /**
     * Alias of addOnceListener.
     */
    proto.once = alias('addOnceListener');

    /**
     * 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.
     * You need to tell it what event names should be matched by a regex.
     *
     * @param {String} evt Name of the event to create.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.defineEvent = function defineEvent(evt) {
        this.getListeners(evt);
        return this;
    };

    /**
     * Uses defineEvent to define multiple events.
     *
     * @param {String[]} evts An array of event names to define.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.defineEvents = function defineEvents(evts) {
        for (var i = 0; i < evts.length; i += 1) {
            this.defineEvent(evts[i]);
        }
        return this;
    };

    /**
     * Removes a listener function from the specified event.
     * When passed a regular expression as the event name, it will remove the listener from all events that match it.
     *
     * @param {String|RegExp} evt Name of the event to remove the listener from.
     * @param {Function} listener Method to remove from the event.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.removeListener = function removeListener(evt, listener) {
        var listeners = this.getListenersAsObject(evt);
        var index;
        var key;

        for (key in listeners) {
            if (listeners.hasOwnProperty(key)) {
                index = indexOfListener(listeners[key], listener);

                if (index !== -1) {
                    listeners[key].splice(index, 1);
                }
            }
        }

        return this;
    };

    /**
     * Alias of removeListener
     */
    proto.off = alias('removeListener');

    /**
     * Adds listeners in bulk using the manipulateListeners method.
     * 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.
     * You can also pass it a regular expression to add the array of listeners to all events that match it.
     * Yeah, this function does quite a bit. That's probably a bad thing.
     *
     * @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.
     * @param {Function[]} [listeners] An optional array of listener functions to add.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.addListeners = function addListeners(evt, listeners) {
        // Pass through to manipulateListeners
        return this.manipulateListeners(false, evt, listeners);
    };

    /**
     * Removes listeners in bulk using the manipulateListeners method.
     * 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.
     * You can also pass it an event name and an array of listeners to be removed.
     * You can also pass it a regular expression to remove the listeners from all events that match it.
     *
     * @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.
     * @param {Function[]} [listeners] An optional array of listener functions to remove.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.removeListeners = function removeListeners(evt, listeners) {
        // Pass through to manipulateListeners
        return this.manipulateListeners(true, evt, listeners);
    };

    /**
     * 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.
     * The first argument will determine if the listeners are removed (true) or added (false).
     * 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.
     * You can also pass it an event name and an array of listeners to be added/removed.
     * You can also pass it a regular expression to manipulate the listeners of all events that match it.
     *
     * @param {Boolean} remove True if you want to remove listeners, false if you want to add.
     * @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.
     * @param {Function[]} [listeners] An optional array of listener functions to add/remove.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) {
        var i;
        var value;
        var single = remove ? this.removeListener : this.addListener;
        var multiple = remove ? this.removeListeners : this.addListeners;

        // If evt is an object then pass each of it's properties to this method
        if (typeof evt === 'object' && !(evt instanceof RegExp)) {
            for (i in evt) {
                if (evt.hasOwnProperty(i) && (value = evt[i])) {
                    // Pass the single listener straight through to the singular method
                    if (typeof value === 'function') {
                        single.call(this, i, value);
                    }
                    else {
                        // Otherwise pass back to the multiple function
                        multiple.call(this, i, value);
                    }
                }
            }
        }
        else {
            // So evt must be a string
            // And listeners must be an array of listeners
            // Loop over it and pass each one to the multiple method
            i = listeners.length;
            while (i--) {
                single.call(this, evt, listeners[i]);
            }
        }

        return this;
    };

    /**
     * Removes all listeners from a specified event.
     * If you do not specify an event then all listeners will be removed.
     * That means every event will be emptied.
     * You can also pass a regex to remove all events that match it.
     *
     * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.removeEvent = function removeEvent(evt) {
        var type = typeof evt;
        var events = this._getEvents();
        var key;

        // Remove different things depending on the state of evt
        if (type === 'string') {
            // Remove all listeners for the specified event
            delete events[evt];
        }
        else if (type === 'object') {
            // Remove all events matching the regex.
            for (key in events) {
                if (events.hasOwnProperty(key) && evt.test(key)) {
                    delete events[key];
                }
            }
        }
        else {
            // Remove all listeners in all events
            delete this._events;
        }

        return this;
    };

    /**
     * Alias of removeEvent.
     *
     * Added to mirror the node API.
     */
    proto.removeAllListeners = alias('removeEvent');

    /**
     * Emits an event of your choice.
     * When emitted, every listener attached to that event will be executed.
     * If you pass the optional argument array then those arguments will be passed to every listener upon execution.
     * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately.
     * So they will not arrive within the array on the other side, they will be separate.
     * You can also pass a regular expression to emit to all events that match it.
     *
     * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
     * @param {Array} [args] Optional array of arguments to be passed to each listener.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.emitEvent = function emitEvent(evt, args) {
        var listeners = this.getListenersAsObject(evt);
        var listener;
        var i;
        var key;
        var response;

        for (key in listeners) {
            if (listeners.hasOwnProperty(key)) {
                i = listeners[key].length;

                while (i--) {
                    // If the listener returns true then it shall be removed from the event
                    // The function is executed either with a basic call or an apply if there is an args array
                    listener = listeners[key][i];

                    if (listener.once === true) {
                        this.removeListener(evt, listener.listener);
                    }

                    response = listener.listener.apply(this, args || []);

                    if (response === this._getOnceReturnValue()) {
                        this.removeListener(evt, listener.listener);
                    }
                }
            }
        }

        return this;
    };

    /**
     * Alias of emitEvent
     */
    proto.trigger = alias('emitEvent');

    /**
     * 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.
     * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it.
     *
     * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
     * @param {...*} Optional additional arguments to be passed to each listener.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.emit = function emit(evt) {
        var args = Array.prototype.slice.call(arguments, 1);
        return this.emitEvent(evt, args);
    };

    /**
     * Sets the current value to check against when executing listeners. If a
     * listeners return value matches the one set here then it will be removed
     * after execution. This value defaults to true.
     *
     * @param {*} value The new value to check for when executing listeners.
     * @return {Object} Current instance of EventEmitter for chaining.
     */
    proto.setOnceReturnValue = function setOnceReturnValue(value) {
        this._onceReturnValue = value;
        return this;
    };

    /**
     * Fetches the current value to check against when executing listeners. If
     * the listeners return value matches this one then it should be removed
     * automatically. It will return true by default.
     *
     * @return {*|Boolean} The current value to check for or the default, true.
     * @api private
     */
    proto._getOnceReturnValue = function _getOnceReturnValue() {
        if (this.hasOwnProperty('_onceReturnValue')) {
            return this._onceReturnValue;
        }
        else {
            return true;
        }
    };

    /**
     * Fetches the events object and creates one if required.
     *
     * @return {Object} The events storage object.
     * @api private
     */
    proto._getEvents = function _getEvents() {
        return this._events || (this._events = {});
    };

    // Expose the class either via AMD, CommonJS or the global object
    if (typeof define === 'function' && define.amd) {
        define("EventEmitter", function () {
            return EventEmitter;
        });
    }
    else if (typeof module === 'object' && module.exports){
        module.exports = EventEmitter;
    }
    else {
        this.EventEmitter = EventEmitter;
    }
}.call(this));

626 627 628 629 630 631 632 633 634 635
/*!
 * getStyleProperty by kangax
 * http://perfectionkills.com/feature-testing-css-properties/
 */

/*jshint browser: true, strict: true, undef: true */
/*globals define: false */

( function( window ) {

636
    'use strict';
637

638 639
    var prefixes = 'Webkit Moz ms Ms O'.split(' ');
    var docElemStyle = document.documentElement.style;
640

641 642 643 644
    function getStyleProperty( propName ) {
        if ( !propName ) {
            return;
        }
645

646 647 648 649
        // test standard property first
        if ( typeof docElemStyle[ propName ] === 'string' ) {
            return propName;
        }
650

651 652
        // capitalize
        propName = propName.charAt(0).toUpperCase() + propName.slice(1);
653

654 655 656 657 658 659 660 661
        // test vendor specific properties
        var prefixed;
        for ( var i=0, len = prefixes.length; i < len; i++ ) {
            prefixed = prefixes[i] + propName;
            if ( typeof docElemStyle[ prefixed ] === 'string' ) {
                return prefixed;
            }
        }
662 663 664
    }

// transport
665 666 667 668 669 670 671 672 673
    if ( typeof define === 'function' && define.amd ) {
        // AMD
        define("getStyleProperty", function() {
            return getStyleProperty;
        });
    } else {
        // browser global
        window.getStyleProperty = getStyleProperty;
    }
674 675 676 677

})( window );

/**
678
 * getSize v1.1.4
679 680 681 682 683 684 685 686
 * measure size of elements
 */

/*jshint browser: true, strict: true, undef: true, unused: true */
/*global define: false */

( function( window, undefined ) {

687
    'use strict';
688 689 690

// -------------------------- helpers -------------------------- //

691
    var defView = document.defaultView;
692

693 694 695 696 697 698 699
    var getStyle = defView && defView.getComputedStyle ?
        function( elem ) {
            return defView.getComputedStyle( elem, null );
        } :
        function( elem ) {
            return elem.currentStyle;
        };
700 701

// get a number from a string, not a percentage
702 703 704 705 706 707
    function getStyleSize( value ) {
        var num = parseFloat( value );
        // not a percent like '100%', and a number
        var isValid = value.indexOf('%') === -1 && !isNaN( num );
        return isValid && num;
    }
708 709 710

// -------------------------- measurements -------------------------- //

711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
    var measurements = [
        'paddingLeft',
        'paddingRight',
        'paddingTop',
        'paddingBottom',
        'marginLeft',
        'marginRight',
        'marginTop',
        'marginBottom',
        'borderLeftWidth',
        'borderRightWidth',
        'borderTopWidth',
        'borderBottomWidth'
    ];

    function getZeroSize() {
        var size = {
            width: 0,
            height: 0,
            innerWidth: 0,
            innerHeight: 0,
            outerWidth: 0,
            outerHeight: 0
        };
        for ( var i=0, len = measurements.length; i < len; i++ ) {
            var measurement = measurements[i];
            size[ measurement ] = 0;
        }
        return size;
    }



    function defineGetSize( getStyleProperty ) {
745 746 747

// -------------------------- box sizing -------------------------- //

748 749
        var boxSizingProp = getStyleProperty('boxSizing');
        var isBoxSizeOuter;
750

751 752 753 754 755 756 757 758
        /**
         * WebKit measures the outer-width on style.width on border-box elems
         * IE & Firefox measures the inner-width
         */
        ( function() {
            if ( !boxSizingProp ) {
                return;
            }
759

760 761 762 763 764 765
            var div = document.createElement('div');
            div.style.width = '200px';
            div.style.padding = '1px 2px 3px 4px';
            div.style.borderStyle = 'solid';
            div.style.borderWidth = '1px 2px 3px 4px';
            div.style[ boxSizingProp ] = 'border-box';
766

767 768 769
            var body = document.body || document.documentElement;
            body.appendChild( div );
            var style = getStyle( div );
770

771 772 773
            isBoxSizeOuter = getStyleSize( style.width ) === 200;
            body.removeChild( div );
        })();
774 775 776 777


// -------------------------- getSize -------------------------- //

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
        function getSize( elem ) {
            // use querySeletor if elem is string
            if ( typeof elem === 'string' ) {
                elem = document.querySelector( elem );
            }

            // do not proceed on non-objects
            if ( !elem || typeof elem !== 'object' || !elem.nodeType ) {
                return;
            }

            var style = getStyle( elem );

            // if hidden, everything is 0
            if ( style.display === 'none' ) {
                return getZeroSize();
            }

            var size = {};
            size.width = elem.offsetWidth;
            size.height = elem.offsetHeight;

            var isBorderBox = size.isBorderBox = !!( boxSizingProp &&
                style[ boxSizingProp ] && style[ boxSizingProp ] === 'border-box' );

            // get all measurements
            for ( var i=0, len = measurements.length; i < len; i++ ) {
                var measurement = measurements[i];
                var value = style[ measurement ];
                var num = parseFloat( value );
                // any 'auto', 'medium' value will be 0
                size[ measurement ] = !isNaN( num ) ? num : 0;
            }

            var paddingWidth = size.paddingLeft + size.paddingRight;
            var paddingHeight = size.paddingTop + size.paddingBottom;
            var marginWidth = size.marginLeft + size.marginRight;
            var marginHeight = size.marginTop + size.marginBottom;
            var borderWidth = size.borderLeftWidth + size.borderRightWidth;
            var borderHeight = size.borderTopWidth + size.borderBottomWidth;

            var isBorderBoxSizeOuter = isBorderBox && isBoxSizeOuter;

            // overwrite width and height if we can get it from style
            var styleWidth = getStyleSize( style.width );
            if ( styleWidth !== false ) {
                size.width = styleWidth +
                    // add padding and border unless it's already including it
                    ( isBorderBoxSizeOuter ? 0 : paddingWidth + borderWidth );
            }

            var styleHeight = getStyleSize( style.height );
            if ( styleHeight !== false ) {
                size.height = styleHeight +
                    // add padding and border unless it's already including it
                    ( isBorderBoxSizeOuter ? 0 : paddingHeight + borderHeight );
            }

            size.innerWidth = size.width - ( paddingWidth + borderWidth );
            size.innerHeight = size.height - ( paddingHeight + borderHeight );

            size.outerWidth = size.width + marginWidth;
            size.outerHeight = size.height + marginHeight;

            return size;
        }

        return getSize;

    }
848 849

// transport
850 851 852 853 854 855 856
    if ( typeof define === 'function' && define.amd ) {
        // AMD
        define("getSize", [ 'getStyleProperty' ], defineGetSize );
    } else {
        // browser global
        window.getSize = defineGetSize( window.getStyleProperty );
    }
857 858 859 860 861 862 863 864 865 866 867

})( window );

/*!
 * Draggabilly v1.0.5
 * Make that shiz draggable
 * http://draggabilly.desandro.com
 */

( function( window ) {

868
    'use strict';
869 870

// vars
871
    var document = window.document;
872 873 874 875

// -------------------------- helpers -------------------------- //

// extend objects
876 877 878 879 880 881
    function extend( a, b ) {
        for ( var prop in b ) {
            a[ prop ] = b[ prop ];
        }
        return a;
    }
882

883
    function noop() {}
884 885 886

// ----- get style ----- //

887
    var defView = document.defaultView;
888

889 890 891 892 893 894 895
    var getStyle = defView && defView.getComputedStyle ?
        function( elem ) {
            return defView.getComputedStyle( elem, null );
        } :
        function( elem ) {
            return elem.currentStyle;
        };
896 897 898


// http://stackoverflow.com/a/384380/182183
899 900 901 902 903 904 905 906
    var isElement = ( typeof HTMLElement === 'object' ) ?
        function isElementDOM2( obj ) {
            return obj instanceof HTMLElement;
        } :
        function isElementQuirky( obj ) {
            return obj && typeof obj === 'object' &&
                obj.nodeType === 1 && typeof obj.nodeName === 'string';
        };
907 908 909 910 911

// -------------------------- requestAnimationFrame -------------------------- //

// https://gist.github.com/1866474

912 913
    var lastTime = 0;
    var prefixes = 'webkit moz ms o'.split(' ');
914
// get unprefixed rAF and cAF, if present
915 916
    var requestAnimationFrame = window.requestAnimationFrame;
    var cancelAnimationFrame = window.cancelAnimationFrame;
917
// loop through vendor prefixes and get prefixed rAF and cAF
918 919 920 921 922 923 924 925 926 927
    var prefix;
    for( var i = 0; i < prefixes.length; i++ ) {
        if ( requestAnimationFrame && cancelAnimationFrame ) {
            break;
        }
        prefix = prefixes[i];
        requestAnimationFrame = requestAnimationFrame || window[ prefix + 'RequestAnimationFrame' ];
        cancelAnimationFrame  = cancelAnimationFrame  || window[ prefix + 'CancelAnimationFrame' ] ||
            window[ prefix + 'CancelRequestAnimationFrame' ];
    }
928 929

// fallback to setTimeout and clearTimeout if either request/cancel is not supported
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
    if ( !requestAnimationFrame || !cancelAnimationFrame )  {
        requestAnimationFrame = function( callback ) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) );
            var id = window.setTimeout( function() {
                callback( currTime + timeToCall );
            }, timeToCall );
            lastTime = currTime + timeToCall;
            return id;
        };

        cancelAnimationFrame = function( id ) {
            window.clearTimeout( id );
        };
    }
945 946 947

// -------------------------- definition -------------------------- //

948
    function draggabillyDefinition( classie, EventEmitter, eventie, getStyleProperty, getSize ) {
949 950 951

// -------------------------- support -------------------------- //

952
        var transformProperty = getStyleProperty('transform');
953
// TODO fix quick & dirty check for 3D support
954
        var is3d = !!getStyleProperty('perspective');
955 956 957

// --------------------------  -------------------------- //

958 959
        function Draggabilly( element, options ) {
            this.element = element;
960

961 962
            this.options = extend( {}, this.options );
            extend( this.options, options );
963

964
            this._create();
965

966
        }
967 968

// inherit EventEmitter methods
969
        extend( Draggabilly.prototype, EventEmitter.prototype );
970

971 972
        Draggabilly.prototype.options = {
        };
973

974
        Draggabilly.prototype._create = function() {
975

976 977 978
            // properties
            this.position = {};
            this._getPosition();
979

980 981
            this.startPoint = { x: 0, y: 0 };
            this.dragPoint = { x: 0, y: 0 };
982

983
            this.startPosition = extend( {}, this.position );
984

985 986 987 988 989
            // set relative positioning
            var style = getStyle( this.element );
            if ( style.position !== 'relative' && style.position !== 'absolute' ) {
                this.element.style.position = 'relative';
            }
990

991 992
            this.enable();
            this.setHandles();
993

994
        };
995

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
        /**
         * set this.handles and bind start events to 'em
         */
        Draggabilly.prototype.setHandles = function() {
            this.handles = this.options.handle ?
                this.element.querySelectorAll( this.options.handle ) : [ this.element ];

            for ( var i=0, len = this.handles.length; i < len; i++ ) {
                var handle = this.handles[i];
                // bind pointer start event
                // listen for both, for devices like Chrome Pixel
                //   which has touch and mouse events
                eventie.bind( handle, 'mousedown', this );
                eventie.bind( handle, 'touchstart', this );
                disableImgOndragstart( handle );
            }
        };
1013 1014 1015 1016

// remove default dragging interaction on all images in IE8
// IE8 does its own drag thing on images, which messes stuff up

1017 1018 1019
        function noDragStart() {
            return false;
        }
1020 1021

// TODO replace this with a IE8 test
1022
        var isIE8 = 'attachEvent' in document.documentElement;
1023 1024

// IE8 only
1025
        var disableImgOndragstart = !isIE8 ? noop : function( handle ) {
1026

1027 1028 1029
            if ( handle.nodeName === 'IMG' ) {
                handle.ondragstart = noDragStart;
            }
1030

1031 1032 1033 1034 1035 1036
            var images = handle.querySelectorAll('img');
            for ( var i=0, len = images.length; i < len; i++ ) {
                var img = images[i];
                img.ondragstart = noDragStart;
            }
        };
1037 1038 1039


// get left/top position from style
1040 1041 1042
        Draggabilly.prototype._getPosition = function() {
            // properties
            var style = getStyle( this.element );
1043

1044 1045
            var x = parseInt( style.left, 10 );
            var y = parseInt( style.top, 10 );
1046

1047 1048 1049
            // clean up 'auto' or other non-integer values
            this.position.x = isNaN( x ) ? 0 : x;
            this.position.y = isNaN( y ) ? 0 : y;
1050

1051 1052
            this._addTransformPosition( style );
        };
1053 1054

// add transform: translate( x, y ) to position
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
        Draggabilly.prototype._addTransformPosition = function( style ) {
            if ( !transformProperty ) {
                return;
            }
            var transform = style[ transformProperty ];
            // bail out if value is 'none'
            if ( transform.indexOf('matrix') !== 0 ) {
                return;
            }
            // split matrix(1, 0, 0, 1, x, y)
            var matrixValues = transform.split(',');
            // translate X value is in 12th or 4th position
            var xIndex = transform.indexOf('matrix3d') === 0 ? 12 : 4;
            var translateX = parseInt( matrixValues[ xIndex ], 10 );
            // translate Y value is in 13th or 5th position
            var translateY = parseInt( matrixValues[ xIndex + 1 ], 10 );
            this.position.x += translateX;
            this.position.y += translateY;
        };
1074 1075 1076 1077

// -------------------------- events -------------------------- //

// trigger handler methods for events
1078 1079 1080 1081 1082 1083
        Draggabilly.prototype.handleEvent = function( event ) {
            var method = 'on' + event.type;
            if ( this[ method ] ) {
                this[ method ]( event );
            }
        };
1084 1085

// returns the touch that we're keeping track of
1086 1087 1088 1089 1090 1091 1092 1093
        Draggabilly.prototype.getTouch = function( touches ) {
            for ( var i=0, len = touches.length; i < len; i++ ) {
                var touch = touches[i];
                if ( touch.identifier === this.pointerIdentifier ) {
                    return touch;
                }
            }
        };
1094 1095 1096

// ----- start event ----- //

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
        Draggabilly.prototype.onmousedown = function( event ) {
            this.dragStart( event, event );
        };

        Draggabilly.prototype.ontouchstart = function( event ) {
            // disregard additional touches
            if ( this.isDragging ) {
                return;
            }

            this.dragStart( event, event.changedTouches[0] );
        };

        function setPointerPoint( point, pointer ) {
            point.x = pointer.pageX !== undefined ? pointer.pageX : pointer.clientX;
            point.y = pointer.pageY !== undefined ? pointer.pageY : pointer.clientY;
        }

        /**
         * drag start
         * @param {Event} event
         * @param {Event or Touch} pointer
         */
        Draggabilly.prototype.dragStart = function( event, pointer ) {
            if ( !this.isEnabled ) {
                return;
            }

            if ( event.preventDefault ) {
                event.preventDefault();
            } else {
                event.returnValue = false;
            }

            var isTouch = event.type === 'touchstart';

            // save pointer identifier to match up touch events
            this.pointerIdentifier = pointer.identifier;

            this._getPosition();

            this.measureContainment();

            // point where drag began
            setPointerPoint( this.startPoint, pointer );
            // position _when_ drag began
            this.startPosition.x = this.position.x;
            this.startPosition.y = this.position.y;

            // reset left/top style
            this.setLeftTop();

            this.dragPoint.x = 0;
            this.dragPoint.y = 0;

            // bind move and end events
            this._bindEvents({
                events: isTouch ? [ 'touchmove', 'touchend', 'touchcancel' ] :
                    [ 'mousemove', 'mouseup' ],
                // IE8 needs to be bound to document
                node: event.preventDefault ? window : document
            });

            classie.add( this.element, 'is-dragging' );

            // reset isDragging flag
            this.isDragging = true;

            this.emitEvent( 'dragStart', [ this, event, pointer ] );

            // start animation
            this.animate();
        };

        Draggabilly.prototype._bindEvents = function( args ) {
            for ( var i=0, len = args.events.length; i < len; i++ ) {
                var event = args.events[i];
                eventie.bind( args.node, event, this );
            }
            // save these arguments
            this._boundEvents = args;
        };

        Draggabilly.prototype._unbindEvents = function() {
            var args = this._boundEvents;
            for ( var i=0, len = args.events.length; i < len; i++ ) {
                var event = args.events[i];
                eventie.unbind( args.node, event, this );
            }
            delete this._boundEvents;
        };

        Draggabilly.prototype.measureContainment = function() {
            var containment = this.options.containment;
            if ( !containment ) {
                return;
            }

            this.size = getSize( this.element );
            var elemRect = this.element.getBoundingClientRect();

            // use element if element
            var container = isElement( containment ) ? containment :
                // fallback to querySelector if string
                typeof containment === 'string' ? document.querySelector( containment ) :
                    // otherwise just `true`, use the parent
                    this.element.parentNode;

            this.containerSize = getSize( container );
            var containerRect = container.getBoundingClientRect();

            this.relativeStartPosition = {
                x: elemRect.left - containerRect.left,
                y: elemRect.top  - containerRect.top
            };
        };
1213 1214 1215

// ----- move event ----- //

1216 1217 1218
        Draggabilly.prototype.onmousemove = function( event ) {
            this.dragMove( event, event );
        };
1219

1220 1221 1222 1223 1224 1225
        Draggabilly.prototype.ontouchmove = function( event ) {
            var touch = this.getTouch( event.changedTouches );
            if ( touch ) {
                this.dragMove( event, touch );
            }
        };
1226

1227 1228 1229 1230 1231 1232
        /**
         * drag move
         * @param {Event} event
         * @param {Event or Touch} pointer
         */
        Draggabilly.prototype.dragMove = function( event, pointer ) {
1233

1234 1235 1236
            setPointerPoint( this.dragPoint, pointer );
            this.dragPoint.x -= this.startPoint.x;
            this.dragPoint.y -= this.startPoint.y;
1237

1238 1239 1240 1241 1242 1243 1244 1245
            if ( this.options.containment ) {
                var relX = this.relativeStartPosition.x;
                var relY = this.relativeStartPosition.y;
                this.dragPoint.x = Math.max( this.dragPoint.x, -relX );
                this.dragPoint.y = Math.max( this.dragPoint.y, -relY );
                this.dragPoint.x = Math.min( this.dragPoint.x, this.containerSize.width - relX - this.size.width );
                this.dragPoint.y = Math.min( this.dragPoint.y, this.containerSize.height - relY - this.size.height );
            }
1246

1247 1248
            this.position.x = this.startPosition.x + this.dragPoint.x;
            this.position.y = this.startPosition.y + this.dragPoint.y;
1249

1250 1251
            this.emitEvent( 'dragMove', [ this, event, pointer ] );
        };
1252 1253 1254 1255


// ----- end event ----- //

1256 1257 1258
        Draggabilly.prototype.onmouseup = function( event ) {
            this.dragEnd( event, event );
        };
1259

1260 1261 1262 1263 1264 1265
        Draggabilly.prototype.ontouchend = function( event ) {
            var touch = this.getTouch( event.changedTouches );
            if ( touch ) {
                this.dragEnd( event, touch );
            }
        };
1266

1267 1268 1269 1270 1271 1272 1273
        /**
         * drag end
         * @param {Event} event
         * @param {Event or Touch} pointer
         */
        Draggabilly.prototype.dragEnd = function( event, pointer ) {
            this.isDragging = false;
1274

1275
            delete this.pointerIdentifier;
1276

1277 1278 1279 1280 1281
            // use top left position when complete
            if ( transformProperty ) {
                this.element.style[ transformProperty ] = '';
                this.setLeftTop();
            }
1282

1283 1284
            // remove events
            this._unbindEvents();
1285

1286
            classie.remove( this.element, 'is-dragging' );
1287

1288
            this.emitEvent( 'dragEnd', [ this, event, pointer ] );
1289

1290
        };
1291 1292 1293 1294

// ----- cancel event ----- //

// coerce to end event
1295 1296 1297 1298
        Draggabilly.prototype.ontouchcancel = function( event ) {
            var touch = this.getTouch( event.changedTouches );
            this.dragEnd( event, touch );
        };
1299 1300 1301

// -------------------------- animation -------------------------- //

1302 1303 1304 1305 1306
        Draggabilly.prototype.animate = function() {
            // only render and animate if dragging
            if ( !this.isDragging ) {
                return;
            }
1307

1308
            this.positionDrag();
1309

1310 1311 1312 1313
            var _this = this;
            requestAnimationFrame( function animateFrame() {
                _this.animate();
            });
1314

1315
        };
1316 1317

// transform translate function
1318 1319 1320 1321 1322 1323 1324
        var translate = is3d ?
            function( x, y ) {
                return 'translate3d( ' + x + 'px, ' + y + 'px, 0)';
            } :
            function( x, y ) {
                return 'translate( ' + x + 'px, ' + y + 'px)';
            };
1325 1326

// left/top positioning
1327 1328 1329 1330
        Draggabilly.prototype.setLeftTop = function() {
            this.element.style.left = this.position.x + 'px';
            this.element.style.top  = this.position.y + 'px';
        };
1331

1332 1333 1334 1335 1336
        Draggabilly.prototype.positionDrag = transformProperty ?
            function() {
                // position with transform
                this.element.style[ transformProperty ] = translate( this.dragPoint.x, this.dragPoint.y );
            } : Draggabilly.prototype.setLeftTop;
1337

1338 1339 1340
        Draggabilly.prototype.enable = function() {
            this.isEnabled = true;
        };
1341

1342 1343 1344 1345 1346 1347
        Draggabilly.prototype.disable = function() {
            this.isEnabled = false;
            if ( this.isDragging ) {
                this.dragEnd();
            }
        };
1348

1349
        return Draggabilly;
1350

1351
    } // end definition
1352 1353 1354

// -------------------------- transport -------------------------- //

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
    if ( typeof define === 'function' && define.amd ) {
        // AMD
        define('draggabilly', [
            'classie',
            'EventEmitter',
            'eventie',
            'getStyleProperty',
            'getSize'
        ],
            draggabillyDefinition );
    } else {
        // browser global
        window.Draggabilly = draggabillyDefinition(
            window.classie,
            window.EventEmitter,
            window.eventie,
            window.getStyleProperty,
            window.getSize
        );
    }
1375

1376
})( window );