aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/rich-text-editor/overlays/rich-text-linkpopup.reel/rich-text-linkpopup.js
blob: 02598030d5782610f417bf24a3c65ef3317e2a90 (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
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
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */
/**
	@module "montage/ui/rich-text-editor/overlays/rich-text-resizer.reel"
    @requires montage/core/core
    @requires montage/ui/component
*/
var Montage = require("montage").Montage,
    Component = require("ui/component").Component;

/**
    @class module:"montage/ui/rich-text-editor/overlays/rich-text-linkpopup.reel".RichTextLinkPopup
    @extends module:montage/ui/component.Component
*/
exports.RichTextLinkPopup = Montage.create(Component,/** @lends module:"montage/ui/rich-text-editor/overlays/rich-text-linkpopup.reel".RichTextLinkPopup# */ {

    /**
      Description TODO
      @private
    */
    _isActive: {
        enumerable: false,
        value: false
    },
    /**
      Description TODO
      @private
    */
    _editor: {
        enumerable: false,
        value: null
    },

    /**
      Description TODO
      @private
    */
    target: {
        enumerable: false,
        value: null
    },

    /**
      Description TODO
      @private
    */
    _needsReset: {
        enumerable: false,
        value: false
    },

    /**
      Description TODO
     @type {Function}
    */
    initWithEditor: {
        value: function(editor) {
            this._editor = editor;
        }
    },

    /**
      Description TODO
     @type {Function}
    */
    editorMouseUp: {
        value: function(event) {
            var element;

            if (this._editor.activeOverlay != this) {
                // Check if the caret is inside an image within an anchor element
                if (event.target.nodeName == "IMG") {
                    element = event.target;
                    while (element && element != this._element) {
                        if (element.nodeName == "A") {
                            if (element != this.target) {
                                this.target = element;
                                this._needsReset = true;
                                if (this._isActive) {
                                    this.needsDraw = true;
                                } else {
                                    this._editor.showOverlay(this);
                                }
                            }
                            return true;
                        }
                        element = element.parentElement;
                    }
                }
            }
        }
    },

    /**
      Description TODO
     @type {Function}
    */
    editorTouchEnd: {
        value: function(event) {
            this.editorMouseUp(event);
        }
    },

    /**
      Description TODO
     @type {Function}
    */
    editorSelectionDidChange: {
        value: function(range) {
            var element;

            // Check if the caret is inside an anchor element
            if (range && range.collapsed) {
                element = range.commonAncestorContainer;
                while (element && element != this._element) {
                    if (element.nodeName == "A") {
                        if (element != this.target) {
                            this.target = element;
                            this._needsReset = true;
                            if (this._isActive) {
                                this.needsDraw = true;
                            } else {
                                this._editor.showOverlay(this);
                            }
                        }
                        return true;
                    }
                    element = element.parentElement;
                }
            }

            if (this._editor.activeOverlay == this) {
                this._editor.hideOverlay();
            }
            this.target = null;

            return false;
        }
    },

    /**
    Description TODO
    @function
    */
    didBecomeActive: {
        value: function() {
            this._isActive = true;
            window.addEventListener("resize", this, false);
        }
    },

    /**
    Description TODO
    @function
    */
    didBecomeInactive: {
        value: function() {
            this._isActive = false;
            window.removeEventListener("resize", this, false);

            //Reset the resizer internal
            this.target = null;
            this._needsReset = false;
        }
    },

    /**
    Description TODO
    @function
    */
    prepareForDraw: {
        enumerable: false,
        value: function() {
            this._popupExtraWidth = this.element.offsetWidth;
        }
    },

    /**
    Description TODO
    @function
    */
    draw: {
        enumerable: false,
        value: function() {
            var element = this.element,
                target = this.target,
                editorElement = this._editor.innerElement;

            if (this._needsReset) {
                var offsetLeft,
                    offsetTop,
                    oh = editorElement.offsetHeight,
                    ow = editorElement.offsetWidth,
                    st = editorElement.scrollTop,
                    sl = editorElement.scrollLeft,
                    w = target.offsetWidth,
                    h = target.offsetHeight,
                    l, t,
                    left, leftWidth, right, rightWidth,
                    style = "";

                var _findOffset = function(node) {
                        offsetTop = node.offsetTop;
                        offsetLeft = node.offsetLeft;

                        while ((node = node.offsetParent) && node != editorElement) {
                            offsetTop += node.offsetTop;
                            offsetLeft += node.offsetLeft;
                        }
                    };
                _findOffset(target);

                l  = offsetLeft;
                t  = offsetTop;

                // Should we display the popup on top or below the element?
                if (t > 60 && t - st + h + 50 > oh) {
                    style = "bottom: " + (oh - t + 5) + "px;";
                } else {
                    style = "top: " + (t + h + 5 ) + "px;";
                }

                // Should we display the popup aligned on the left or right of the element?
                left = sl;
                right = sl + ow;
                leftWidth = right - l;
                rightWidth = l + w - left;

                if (leftWidth  > rightWidth) {
                    //Let's align the popup to the left of the element or to the far left
                    if (leftWidth < 150) {
                        style += " left: " + (left + 5) + "px;";
                        style += " max-width: " + (ow - 10 - this._popupExtraWidth) + "px;";
                    } else {
                        style += " left: " + (left + l) + "px;";
                        style += " max-width: " + (leftWidth - 5 - this._popupExtraWidth) + "px;";
                    }
                } else {
                    if (rightWidth < 150) {
                        style += " right: " + (left + 5) + "px;";
                        style += " max-width: " + (ow - 10 - this._popupExtraWidth) + "px;";
                    } else {
                        style += " right: " + (right - (left + l + w + 1)) + "px;";
                        style += " max-width: " + (rightWidth - this._popupExtraWidth) + "px;";
                    }
                }

                // Position and size the popup
                element.setAttribute("style", style);

                // Setup the anchor
                this.link.href = target.href;
                this.link.textContent = target.href;

                this._needsReset = false;
            }
        }
    },

    /**
    Description TODO
    @function
    */
    handleResize: {
        enumerable: false,
        value: function() {
            this._needsReset = true;
            this.needsDraw = true;
        }
    }

});