aboutsummaryrefslogtreecommitdiff
path: root/js/lib/NJUtils.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/NJUtils.js')
-rw-r--r--js/lib/NJUtils.js213
1 files changed, 213 insertions, 0 deletions
diff --git a/js/lib/NJUtils.js b/js/lib/NJUtils.js
new file mode 100644
index 00000000..d6548871
--- /dev/null
+++ b/js/lib/NJUtils.js
@@ -0,0 +1,213 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7var Montage = require("montage/core/core").Montage,
8 ElementModel = require("js/models/element-model").ElementModel,
9 Properties3D = require("js/models/properties-3d").Properties3D,
10 ShapeModel = require("js/models/shape-model").ShapeModel,
11 ControllerFactory = require("js/controllers/elements/controller-factory").ControllerFactory;
12
13exports.NJUtils = Object.create(Object.prototype, {
14
15
16
17 /* =============== DOM Access ================ */
18
19 ///// Quick "getElementById"
20 $ : {
21 value: function(id) {
22 return document.getElementById(id);
23 }
24 },
25
26 ///// Quick "getElementsByClassName" which also returns as an Array
27 ///// Can return as NodeList by passing true as second argument
28 $$ : {
29 value: function(className, asNodeList) {
30 var list = document.getElementsByClassName(className);
31 return (asNodeList) ? list : this.toArray(list);
32 }
33 },
34
35 ///// Get child nodes of element
36 ///// Omit filter to only return element nodes
37 ///// Pass in filter function to minimize collection, or
38 ///// set to true to include all nodes
39 children : {
40 value : function(el, filter) {
41 var f = filter || function(item) {
42 return item.nodeType === 1;
43 };
44 return this.toArray(el.childNodes).filter(f);
45 }
46 },
47
48 /* ============= DOM Manipulation ============= */
49
50 ///// Creates and returns text node from string
51 textNode : {
52 value: function(text) {
53 return document.createTextNode(text);
54 }
55 },
56
57 ///// Quick "createElement" function "attr" can be classname or object
58 ///// with attribute key/values
59 make : {
60 value: function(tag, attr) {
61 var el = document.createElement(tag);
62 if (typeof attr === 'object') {
63 for (var a in attr) {
64 if (attr.hasOwnProperty(a)) {
65 el[a] = attr[a];
66 }
67 }
68 } else if (typeof attr === 'string') {
69 el.className = (el.className + ' ' + attr).trim();
70 }
71
72 return el;
73 }
74 },
75
76 ///// Element factory function for Ninja Elements
77 ///// selection is the string displayed in the PI
78 makeNJElement: {
79 value: function(tag, selection, controller, attr, isShape) {
80 var el = this.make(tag, attr);
81 this.makeElementModel(el, selection, controller, isShape);
82
83 return el;
84 }
85 },
86
87 ///// Element Model creation for existing elements
88 ///// TODO: find a different place for this function
89 makeElementModel: {
90 value: function(el, selection, controller, isShape) {
91 var p3d = Montage.create(Properties3D).init(el);
92 var shapeProps = null;
93 if(isShape) {
94 shapeProps = Montage.create(ShapeModel);
95 }
96
97 el.elementModel = Montage.create(ElementModel, {
98 type: { value: el.nodeName},
99 selection: { value: selection},
100 controller: { value: ControllerFactory.getController(controller)},
101 pi: { value: controller + "Pi"},
102 props3D: { value: p3d},
103 shapeModel: { value: shapeProps}
104 });
105
106 }
107 },
108
109 ///// Element Model creation for existing elements based on element type.
110 ///// TODO: find a different place for this function and return different element models based on type.
111 makeElementModel2: {
112 value: function(el) {
113 this.makeElementModel(el, "Div", "block", false);
114 }
115 },
116
117 ///// Removes all child nodes and returns node
118 ///// Accepts a single node, or an array of dom nodes
119 empty : {
120 value: function(node) {
121 var elements = [],
122 self = this;
123 if (node.constructor === Array) {
124 node.forEach(function(el) { self.empty(el) });
125 } else {
126 this.toArray(node.childNodes).forEach(function(child) {
127 child.parentNode.removeChild(child);
128 });
129 }
130
131 return node;
132 }
133 },
134
135 queryParentSelector : {
136 value: function(el, strSelector) {
137 // queryParentSelector:
138 // Given a DOM element el (required), walk up the DOM tree
139 // and find the first parent that matches selector strSelector (required).
140 // Returns: The element that matches, or false if there is no match
141 // or if insufficient parameters are supplied.
142
143 if ((typeof(el) === "undefined") || (typeof(strSelector) === "undefined")) {
144 // Parameters are required, m'kay?
145 return false;
146 } else if ((typeof(el) !== "object") || (typeof(strSelector) !== "string" )) {
147 // You also have to use the right parameters.
148 return false;
149 }
150
151 // First, get an empty clone of the parent.
152 var myParent = el.parentNode;
153 var clone = myParent.cloneNode(false);
154 if (clone === null) {
155 return false;
156 }
157
158 // If we're at the top of the DOM, our clone will be an htmlDocument.
159 // htmlDocument has no tagName.
160 if (typeof(clone.tagName) !== "undefined") {
161 // create a bogus div to use as a base for querySelector
162 var temp = document.createElement("div");
163
164 // Append the clone to the bogus div
165 temp.appendChild(clone);
166
167 // Now we can use querySelector! Sweet.
168 var selectorTest = temp.querySelector(strSelector);
169
170 // What has querySelector returned?
171 if (selectorTest === null) {
172 // No match, so recurse.
173 return this.queryParentSelector(myParent, strSelector);
174 } else {
175 // Match! Return the element.
176 return myParent;
177 }
178 } else {
179 // We're at the top of the DOM so we're done.
180 return false;
181 }
182 }
183
184 },
185
186 /* ================= Style methods ================= */
187
188 ///// Get computed height of element
189 height : {
190 value: function(node, pseudo) {
191 return node.ownerDocument.defaultView.getComputedStyle(node, pseudo).getPropertyValue('height');
192 }
193 },
194
195 /* ================= Array methods ================= */
196
197 ///// Return an array from an array-like object
198 toArray : {
199 value: function(arrayLikeObj) {
200 return Array.prototype.slice.call(arrayLikeObj);
201 }
202 },
203
204 /* ================= String methods ================= */
205
206 ///// Return the last part of a path (e.g. filename)
207 getFileNameFromPath : {
208 value: function(path) {
209 return path.substr(path.lastIndexOf('/') + 1);
210 }
211 }
212
213});