diff options
Diffstat (limited to 'node_modules/montage/ui/dynamic-element.reel/dynamic-element.js')
-rw-r--r-- | node_modules/montage/ui/dynamic-element.reel/dynamic-element.js | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/node_modules/montage/ui/dynamic-element.reel/dynamic-element.js b/node_modules/montage/ui/dynamic-element.reel/dynamic-element.js new file mode 100644 index 00000000..f0af644d --- /dev/null +++ b/node_modules/montage/ui/dynamic-element.reel/dynamic-element.js | |||
@@ -0,0 +1,143 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2012 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | /** | ||
7 | @module "montage/ui/dynamic-element.reel" | ||
8 | @requires montage | ||
9 | @requires montage/ui/component | ||
10 | */ | ||
11 | var Montage = require("montage").Montage, | ||
12 | Component = require("ui/component").Component; | ||
13 | |||
14 | /** | ||
15 | The DynamicElement is a general purpose component that aims to expose all the properties of the element as a component. | ||
16 | @class module:"montage/ui/dynamic-element.reel".DynamicElement | ||
17 | @extends module:montage/ui/component.Component | ||
18 | */ | ||
19 | exports.DynamicElement = Montage.create(Component, /** @lends module:"montage/ui/dynamic-element.reel".DynamicElement# */ { | ||
20 | |||
21 | hasTemplate: { | ||
22 | value: false | ||
23 | }, | ||
24 | |||
25 | _innerHTML: { | ||
26 | value: null | ||
27 | }, | ||
28 | |||
29 | /** | ||
30 | The innerHTML displayed as the content of the DynamicElement | ||
31 | @type {Property} | ||
32 | @default null | ||
33 | */ | ||
34 | innerHTML: { | ||
35 | get: function() { | ||
36 | return this._innerHTML; | ||
37 | }, | ||
38 | set: function(value) { | ||
39 | if (this._innerHTML !== value) { | ||
40 | this._innerHTML = value; | ||
41 | this.needsDraw = true; | ||
42 | } | ||
43 | }, | ||
44 | serializable: true | ||
45 | }, | ||
46 | |||
47 | /** | ||
48 | The default html displayed if innerHTML is falsy. | ||
49 | @type {Property} | ||
50 | @default {String} "" | ||
51 | */ | ||
52 | defaultHTML: { | ||
53 | value: "" | ||
54 | }, | ||
55 | |||
56 | _allowedTagNames: { | ||
57 | value: null | ||
58 | }, | ||
59 | |||
60 | /** | ||
61 | White list of allowed tags in the innerHTML | ||
62 | @type {Property} | ||
63 | @default null | ||
64 | */ | ||
65 | allowedTagNames: { | ||
66 | get: function() { | ||
67 | return this._allowedTagNames; | ||
68 | }, | ||
69 | set: function(value) { | ||
70 | if (this._allowedTagNames !== value) { | ||
71 | this._allowedTagNames = value; | ||
72 | this.needsDraw = true; | ||
73 | } | ||
74 | }, | ||
75 | serializable: true | ||
76 | }, | ||
77 | |||
78 | _range: { | ||
79 | value: null | ||
80 | }, | ||
81 | |||
82 | prepareForDraw: { | ||
83 | value: function() { | ||
84 | var range = document.createRange(); | ||
85 | range.selectNodeContents(this.element); | ||
86 | this._range = range; | ||
87 | } | ||
88 | }, | ||
89 | |||
90 | _contentNode: { | ||
91 | value: null | ||
92 | }, | ||
93 | |||
94 | draw: { | ||
95 | value: function() { | ||
96 | // get correct value | ||
97 | var displayValue = (this.innerHTML || 0 === this.innerHTML ) ? this.innerHTML : this.defaultHTML, | ||
98 | content, allowedTagNames = this.allowedTagNames, range = this._range, elements; | ||
99 | |||
100 | //push to DOM | ||
101 | if (allowedTagNames !== null) { | ||
102 | //cleanup | ||
103 | this._contentNode = null; | ||
104 | range.deleteContents(); | ||
105 | //test for tag white list | ||
106 | content = range.createContextualFragment( displayValue ); | ||
107 | if(allowedTagNames.length !== 0) { | ||
108 | elements = content.querySelectorAll("*:not(" + allowedTagNames.join("):not(") + ")"); | ||
109 | } else { | ||
110 | elements = content.childNodes; | ||
111 | } | ||
112 | if (elements.length === 0) { | ||
113 | range.insertNode(content); | ||
114 | if(range.endOffset === 0) { | ||
115 | // according to https://bugzilla.mozilla.org/show_bug.cgi?id=253609 Firefox keeps a collapsed | ||
116 | // range collapsed after insertNode | ||
117 | range.selectNodeContents(this.element); | ||
118 | } | ||
119 | |||
120 | } else { | ||
121 | console.warn("Some Elements Not Allowed " , elements); | ||
122 | } | ||
123 | } else { | ||
124 | content = this._contentNode; | ||
125 | if(content === null) { | ||
126 | //cleanup | ||
127 | range.deleteContents(); | ||
128 | this._contentNode = content = document.createTextNode(displayValue); | ||
129 | range.insertNode(content); | ||
130 | if(range.endOffset === 0) { | ||
131 | // according to https://bugzilla.mozilla.org/show_bug.cgi?id=253609 Firefox keeps a collapsed | ||
132 | // range collapsed after insert | ||
133 | range.selectNodeContents(this.element); | ||
134 | } | ||
135 | |||
136 | } else { | ||
137 | content.data = displayValue; | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | } | ||
142 | |||
143 | }); | ||