Statistiques
| Révision :

root / tmp / org.txm.edition.rcp / res / js / functions.js @ 3119

Historique | Voir | Annoter | Télécharger (6,83 ko)

1
getNodeText = function getNodeText(node) {
2
        if (typeof node == 'string')
3
                return node;
4
        else if (typeof node.innerText != 'undefined')
5
                return node.innerText;
6
        else if (typeof node.InnerText != 'undefined')
7
                return node.InnerText;
8
        else if (typeof node.textContent != 'undefined')
9
                return node.textContent;
10
        else
11
                return null;
12
}
13
txmGetParent = function txmGetParent(node) {
14
        if (typeof node.parentNode != 'undefined')
15
                return node.parentNode;
16
        else
17
                return null;
18
}
19
txmGetChildren = function txmGetChildren(node) {
20
        if (typeof node == 'string')
21
                return 'STRING HTML';
22
        else if (typeof node.children != 'undefined')
23
                return node.children;
24
        else if (typeof node.ChildNodes != 'undefined')
25
                return node.ChildNodes;
26
        else if (typeof node.childNodes != 'undefined')
27
                return node.childNodes;
28
        else
29
                return null;
30
}
31
txmGetSelection = function txmGetSelection() {
32
        if (typeof window.getSelection != 'undefined') {
33
                return window.getSelection();
34
        } else if (typeof document.selection != 'undefined') {
35
                return document.selection;
36
        } else
37
                return 'NO SELECTION';
38
}
39
txmGetRangeSize = function txmGetRangeSize(selection) {
40
        if (typeof selection.rangeCount != 'undefined') {
41
                return selection.rangeCount;
42
        } else if (typeof selection.createRangeCollection != 'undefined') {
43
                return selection.createRangeCollection().length();
44
        } // IE5 has no multiple selection
45
}
46
txmGetRange = function txmGetRange(selection, i) {
47
        if (typeof selection.getRangeAt != 'undefined') {
48
                return selection.getRangeAt(i);
49
        } else if (typeof selection.createRangeCollection != 'undefined') {
50
                return selection.createRangeCollection().item(i);
51
        } else if (typeof selection.createRange != 'undefined') {
52
                return selection.createRange();
53
        } // IE5 has no multiple selection
54
        else
55
                return 'NO RANGE';
56
}
57
txmGetParentElementRange = function txmGetParentElementRange(range) {
58
        if (typeof range.parentElement != 'undefined') {
59
                return range.parentElement();
60
        } else if (typeof range.startContainer != 'undefined') {
61
                return range.startContainer.parentNode;
62
        } else
63
                return 'NO PARENT';
64
}
65
txmGetFragment = function txmGetFragment(range) {
66
        if (typeof range.cloneContents != 'undefined') {
67
                return range.cloneContents();
68
        } else if (typeof range.htmlText != 'undefined') {
69
                var node = document.createElement('sel');
70
                node.innerHTML = range.htmlText;
71
                return node;
72
        } else
73
                return 'NO FRAG';
74
}
75
txmGetTagName = function txmGetTagName(node) {
76
        if (typeof node.tagName != 'undefined') {
77
                return node.tagName;
78
        } else if (typeof node.nodeName != 'undefined') {
79
                return node.nodeName;
80
        } else if (typeof node.name != 'undefined') {
81
                return node.name;
82
        } else
83
                return 'NO TAGNAME';
84
}
85
findSpans = function findSpans(children, all) {
86
        for (var i = 0; i < children.length; i++) {
87
                var node = children.item(i);
88
                if (node.nodeType == 1) {
89
                        var id = node.getAttribute('id');
90
                        if (node.tagName == 'SPAN' && id != null && id.indexOf('w_') == 0
91
                                        && getNodeText(node).length > 0) {
92
                                if (id != null)
93
                                        all.push(id);
94
                        } else {
95
                                findSpans(txmGetChildren(node), all)
96
                        }
97
                }
98
        }
99
}
100
findIdsInString = function findIdsInString(str, all) {
101
        for (var i = 0; i < children.length; i++) {
102
                var node = children.item(i);
103
                if (node.nodeType == 1) {
104
                        var id = node.getAttribute('id');
105
                        if (node.tagName == 'SPAN' && id != null && id.indexOf('w_') == 0
106
                                        && getNodeText(node).length > 0) {
107
                                if (id != null)
108
                                        all.push(id);
109
                        } else {
110
                                findSpans(txmGetChildren(node), all)
111
                        }
112
                }
113
        }
114
}
115
get_type = function get_type(thing) {
116
        if (thing === null)
117
                return "[object Null]";
118
        return Object.prototype.toString.call(thing);
119
}
120

    
121
getDetailedObject = function getDetailedObject(inputObject) {
122
        var detailedObject = {};
123
        var properties;
124
        do {
125
                properties = Object.getOwnPropertyNames(inputObject);
126
                for ( var o in properties) {
127
                        detailedObject[properties[o]] = inputObject[properties[o]];
128
                }
129
        } while (inputObject = Object.getPrototypeOf(inputObject));
130
        return detailedObject;
131
}
132

    
133
try {
134
        if ("undefined" == typeof sheet || "undefined" == typeof txmstyle) {
135
                txmstyle = document.createElement("style");
136
                txmstyle.title = "txm_btt"
137
                try {
138
                        txmstyle.appendChild(document.createTextNode(""));
139
                } catch (e) {
140
                }
141
                
142
                document.body.appendChild(txmstyle);
143
                sheet = txmstyle.sheet;
144
        }
145
} catch (e) {
146
        sheet = 'error';
147
}
148

    
149
// try {
150

    
151
// if (!Element.prototype.scrollIntoViewIfNeeded) {
152
// Element.prototype.scrollIntoViewIfNeeded = Element.prototype.scrollIntoView;
153
// //Element.prototype.scrollIntoViewIfNeeded = function(centerIfNeeded) {
154
// //centerIfNeeded = arguments.length === 0 ? true : !!centerIfNeeded;
155
// //var parent = this.parentNode, parentComputedStyle = window
156
// //.getComputedStyle(parent, null), parentBorderTopWidth =
157
// parseInt(parentComputedStyle
158
// //.getPropertyValue('border-top-width')), parentBorderLeftWidth =
159
// parseInt(parentComputedStyle
160
// //.getPropertyValue('border-left-width')), overTop = this.offsetTop
161
// //- parent.offsetTop < parent.scrollTop, overBottom = (this.offsetTop
162
// //- parent.offsetTop + this.clientHeight - parentBorderTopWidth) >
163
// (parent.scrollTop + parent.clientHeight), overLeft = this.offsetLeft
164
// //- parent.offsetLeft < parent.scrollLeft, overRight = (this.offsetLeft
165
// //- parent.offsetLeft + this.clientWidth - parentBorderLeftWidth) >
166
// (parent.scrollLeft + parent.clientWidth), alignWithTop = overTop
167
// //&& !overBottom;
168
// //if ((overTop || overBottom) && centerIfNeeded) {
169
// //parent.scrollTop = this.offsetTop - parent.offsetTop
170
// //- parent.clientHeight / 2 - parentBorderTopWidth
171
// //+ this.clientHeight / 2;
172
// //}
173
// //if ((overLeft || overRight) && centerIfNeeded) {
174
// //parent.scrollLeft = this.offsetLeft - parent.offsetLeft
175
// //- parent.clientWidth / 2 - parentBorderLeftWidth
176
// //+ this.clientWidth / 2;
177
// //}
178
// //if ((overTop || overBottom || overLeft || overRight)
179
// //&& !centerIfNeeded) {
180
// //this.scrollIntoView(alignWithTop);
181
// //}
182
// //};
183
// }
184
// } catch (e) {
185
// Element.prototype.scrollIntoViewIfNeeded = Element.prototype.scrollIntoView
186
// }
187

    
188
showElementIfNeeded = function showElementIfNeeded(el) {
189
        if (!Element.prototype.scrollIntoViewIfNeeded) {
190
                if (!isAnyPartOfElementInViewport(el))
191
                        el.scrollIntoView(false);
192
        } else {
193
                el.scrollIntoViewIfNeeded();
194
        }
195
}
196

    
197
isAnyPartOfElementInViewport = function isAnyPartOfElementInViewport(el) {
198
        try {
199
                const rect = el.getBoundingClientRect();
200
                // DOMRect { x: 8, y: 8, width: 100, height: 100, top: 8, right: 108,
201
                // bottom: 108, left: 8 }
202
                const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
203
                const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
204

    
205
                // http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap
206
                const vertInView = (rect.top <= windowHeight)
207
                                && ((rect.top + rect.height) >= 0);
208
                const horInView = (rect.left <= windowWidth)
209
                                && ((rect.left + rect.width) >= 0);
210

    
211
                return (vertInView && horInView);
212
        } catch (e) {
213
                return false;
214
        }
215
}
216

    
217
return true