summaryrefslogtreecommitdiff
path: root/deck.js/extensions/clone/deck.clone.js
blob: 847d8f8ae35e920a913b16e126c0c2409f321612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
127
/*!
Deck JS - deck.clone
Copyright (c) 2011 Remi BARRAQUAND
Dual licensed under the MIT license and GPL license.
https://github.com/imakewebthings/deck.js/blob/master/MIT-license.txt
https://github.com/imakewebthings/deck.js/blob/master/GPL-license.txt
*/

/*
This module provides a support for cloning the deck.
*/

(function($, deck, undefined) {
    var $d = $(document);
    var clones = new Array();
        
    $.extend(true, $[deck].defaults, {	
        selectors: {
            clonepointer: ".clonepointer"
        },
        classes: {
            hasClones: 'has-clones'
        },
        keys: {
            clone: 67 // c
        }
    });

    var cleanClones = function() {
        var opts = $[deck]('getOptions');
        // remove closed windows
        $.each(clones, function(index, clone) {
            if (clone.closed()) {
                clones.splice(index, 1); // remove element "index"
            }
        });
        // tag/untag the current container depending on the presence of clones
        if (clones.length > 0) {
            $("body").addClass(opts.classes.hasClones);
        } else {
            $("body").removeClass(opts.classes.hasClones);
        }
    };
    /*
	jQuery.deck('addClone')
	
	Create a clone of this window and add it to the clones list.
	*/
    $[deck]('extend', 'addClone', function() {
        clone = new DeckClone();
        clones.push(clone);
        cleanClones();
        return clone;
    });
    $[deck]('extend', 'pointerAt', function(rx, ry) {
        var opts = $[deck]('getOptions');
        var r = $(".deck-current").get(0).getBoundingClientRect();
        var x = r.left + r.width * rx;
        var y = r.top + r.height * ry;
        var pos = {left: x, top: y};
        var current = $(".deck-current").get(0);
        var pointers = $(opts.selectors.clonepointer);
        if (pointers.get(0).parentNode != current) { // move them within the new slide if it changed
            pointers.show().appendTo(".deck-current");
        }
        pointers.offset(pos);
    });
      
    /*
        jQuery.deck('Init')
        */
    $d.bind('deck.init', function() {
        var opts = $[deck]('getOptions');
        var container = $[deck]('getContainer');
        
        $(opts.selectors.clonepointer).hide();

        /* Bind key events */
        $d.unbind('keydown.deckclone').bind('keydown.deckclone', function(e) {
            if (e.which === opts.keys.clone || $.inArray(e.which, opts.keys.clone) > -1) {
                $[deck]('addClone');
                e.preventDefault();
            }
        });
    })
    /* Update current slide number with each change event */
    .bind('deck.change', function(e, from, to) {
        var opts = $[deck]('getOptions');
        var slideTo = $[deck]('getSlide', to);
        var container = $[deck]('getContainer');
        cleanClones();
        $.each(clones, function(index, clone) {
           clone.deck('go', to);
        });
    })
    /* Do the animations locally */
    .bind('deck.step', function(e, delta) {
        cleanClones();
        $.each(clones, function(index, clone) {
            if (delta == -1) clone.deck('stepPrev');
            else if (delta == 1) clone.deck('stepNext');
        });
    })
    /* Replicate mouse cursor */
    .bind('mousemove', function(e) {
        var r = $(".deck-current").get(0).getBoundingClientRect();
        var x = (e.clientX - r.left) / r.width;
        var y = (e.clientY - r.top) / r.height;
        cleanClones();
        $.each(clones, function(index, clone) {
            clone.deck('pointerAt', x, y);
        });
    });
    
    /*
        Simple Clone manager (must be improved, by for instance adding cloning
        option e.g. propagate change, etc.)
        */
    var DeckClone = function() {
        var clone = window.open(window.location);
        this.closed = function() {return clone.closed;}
        this.deck = function() {
            if (clone.closed) return;
            if (clone['$']) clone['$'].deck.apply(clone['$'], arguments)
        }
    }
})(jQuery, 'deck');