Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib/sugar-web/graphics/palette.js
blob: 9a194a14815d3f93c1a4c93a7acc491048f5a1ef (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
define(function () {
    var palettesGroup = [];

    function getOffset(elem) {
        // Ugly hack to consider the palette margin.
        var style = elem.currentStyle || window.getComputedStyle(elem, '');

        // Remove 'px' from the strings.
        var x = -2 * style.marginLeft.slice(0, -2);
        var y = -1 * style.marginTop.slice(0, -2);

        var rect = elem.getBoundingClientRect();
        x += rect.left;
        y += rect.top;
        return {
            top: y,
            left: x,
            width: rect.width,
            height: rect.height
        };
    }

    var palette = {};

    palette.Palette = function (invoker, primaryText) {
        this.invoker = invoker;
        if (this.invoker.classList.contains("toolbutton")) {
            this.invoker.classList.add("invoker");
        }
        this.primaryText = primaryText;
        var paletteElem;
        var wrapperElem;
        var headerElem;
        var headerSeparatorElem;
        var containerElem;
        var that = this;
        palettesGroup.push(this);

        invoker.addEventListener('click', function (event) {
            if (!that.invoker.classList.contains("toolbutton")) {
                updatePosition(event.x, event.y);
            }
            that.toggle();
        });

        function updatePosition(clickX, clickY) {
            var paletteX;
            var paletteY;

            if (typeof (clickX) !== 'undefined' &&
                typeof (clickY) !== 'undefined') {
                paletteX = clickX;
                paletteY = clickY;
            } else {
                var invokerOffset = getOffset(that.invoker);
                paletteX = invokerOffset.left;
                paletteY = invokerOffset.top;
            }

            paletteElem.style.left = paletteX + "px";
            paletteElem.style.top = paletteY + "px";
        }

        // A palette element can have a header, content, one or both.

        function createPaletteElement() {
            if (paletteElem !== undefined) {
                return;
            }
            paletteElem = document.createElement('div');
            paletteElem.className = "palette";
            paletteElem.style.visibility = "hidden";
            document.body.appendChild(paletteElem);

            if (that.invoker.classList.contains("toolbutton")) {
                invokerElem = document.createElement('div');
                invokerElem.className = "palette-invoker";
                var style = that.invoker.currentStyle ||
                    window.getComputedStyle(that.invoker, '');
                invokerElem.style.backgroundImage = style.backgroundImage;

                invokerElem.addEventListener('click', function (e) {
                    that.toggle();
                });

                paletteElem.appendChild(invokerElem);

            }

            wrapperElem = document.createElement('div');
            wrapperElem.className = "wrapper";
            paletteElem.appendChild(wrapperElem);

            if (that.primaryText !== undefined) {
                headerElem = document.createElement('div');
                headerElem.className = "header";
                headerElem.innerText = that.primaryText;
                wrapperElem.appendChild(headerElem);
            }

            headerSeparatorElem = document.createElement('hr');
            headerSeparatorElem.className = "header-separator";
            headerSeparatorElem.style.display = "none";
            wrapperElem.appendChild(headerSeparatorElem);

            containerElem = document.createElement('div');
            containerElem.className = "container";
            wrapperElem.appendChild(containerElem);

            updatePosition();
        }

        this.getPalette = function () {
            if (paletteElem === undefined) {
                createPaletteElement();
            }
            return paletteElem;
        };

        this.setContent = function (elementsList) {
            if (paletteElem === undefined) {
                createPaletteElement();
            }

            (function removePreviousContent() {
                for (var i = 0; i < containerElem.children.length; i++) {
                    var child = containerElem.children[i];
                    containerElem.removeChild(child);
                }
            }());

            (function addNewContent() {
                for (var i = 0; i < elementsList.length; i++) {
                    var child = elementsList[i];
                    containerElem.appendChild(child);
                }
            }());

            // The header separator will be visible only if there are
            // both, header and content.
            if (elementsList.length > 0 && this.primaryText !== undefined) {
                headerSeparatorElem.style.display = "block";
            } else {
                headerSeparatorElem.style.display = "none";
            }
        };

        this.isDown = function () {
            return paletteElem === undefined ||
                paletteElem.style.visibility == "hidden";
        };

    };

    palette.Palette.prototype.popUp = function () {
        for (var i = 0; i < palettesGroup.length; i++) {
            otherPalette = palettesGroup[i];
            if (otherPalette != this) {
                otherPalette.popDown();
            }
        }
        this.getPalette().style.visibility = "visible";
    };

    palette.Palette.prototype.popDown = function () {
        this.getPalette().style.visibility = "hidden";
    };

    palette.Palette.prototype.toggle = function () {
        if (this.isDown()) {
            this.popUp();
        } else {
            this.popDown();
        }
    };

    return palette;

});