aboutsummaryrefslogtreecommitdiff
path: root/js/lib/NJUtils.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/NJUtils.js')
-rwxr-xr-x[-rw-r--r--]js/lib/NJUtils.js61
1 files changed, 60 insertions, 1 deletions
diff --git a/js/lib/NJUtils.js b/js/lib/NJUtils.js
index d6548871..887743c5 100644..100755
--- a/js/lib/NJUtils.js
+++ b/js/lib/NJUtils.js
@@ -5,6 +5,7 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
5</copyright> */ 5</copyright> */
6 6
7var Montage = require("montage/core/core").Montage, 7var Montage = require("montage/core/core").Montage,
8 Uuid = require("montage/core/uuid").Uuid,
8 ElementModel = require("js/models/element-model").ElementModel, 9 ElementModel = require("js/models/element-model").ElementModel,
9 Properties3D = require("js/models/properties-3d").Properties3D, 10 Properties3D = require("js/models/properties-3d").Properties3D,
10 ShapeModel = require("js/models/shape-model").ShapeModel, 11 ShapeModel = require("js/models/shape-model").ShapeModel,
@@ -56,13 +57,18 @@ exports.NJUtils = Object.create(Object.prototype, {
56 57
57 ///// Quick "createElement" function "attr" can be classname or object 58 ///// Quick "createElement" function "attr" can be classname or object
58 ///// with attribute key/values 59 ///// with attribute key/values
60 ///// Suppor for data attributes
59 make : { 61 make : {
60 value: function(tag, attr) { 62 value: function(tag, attr) {
61 var el = document.createElement(tag); 63 var el = document.createElement(tag);
62 if (typeof attr === 'object') { 64 if (typeof attr === 'object') {
63 for (var a in attr) { 65 for (var a in attr) {
64 if (attr.hasOwnProperty(a)) { 66 if (attr.hasOwnProperty(a)) {
65 el[a] = attr[a]; 67 if(a.indexOf("data-") > -1) {
68 el.setAttribute(a, attr[a]);
69 } else {
70 el[a] = attr[a];
71 }
66 } 72 }
67 } 73 }
68 } else if (typeof attr === 'string') { 74 } else if (typeof attr === 'string') {
@@ -183,6 +189,27 @@ exports.NJUtils = Object.create(Object.prototype, {
183 189
184 }, 190 },
185 191
192 // Returns the numerical value and unit string from a string.
193 // Useful for element properties.
194 // 100px will return the following array: [100, px]
195 getValueAndUnits: {
196 value: function(input) {
197 var numberValue = parseFloat(input);
198
199 // Ignore all whitespace, digits, negative sign and "." when looking for units label
200 // The units must come after one or more digits
201 var objRegExp = /(\-*\d+\.*\d*)(\s*)(\w*\%*)/;
202 var unitsString = input.replace(objRegExp, "$3");
203 if(unitsString) {
204 var noSpaces = /(\s*)(\S*)(\s*)/;
205 // strip out spaces and convert to lower case
206 var match = (unitsString.replace(noSpaces, "$2")).toLowerCase();
207 }
208
209 return [numberValue, match];
210 }
211 },
212
186 /* ================= Style methods ================= */ 213 /* ================= Style methods ================= */
187 214
188 ///// Get computed height of element 215 ///// Get computed height of element
@@ -206,8 +233,40 @@ exports.NJUtils = Object.create(Object.prototype, {
206 ///// Return the last part of a path (e.g. filename) 233 ///// Return the last part of a path (e.g. filename)
207 getFileNameFromPath : { 234 getFileNameFromPath : {
208 value: function(path) { 235 value: function(path) {
236 path = path.replace(/[/\\]$/g,"");
237 path = path.replace(/\\/g,"/");
209 return path.substr(path.lastIndexOf('/') + 1); 238 return path.substr(path.lastIndexOf('/') + 1);
210 } 239 }
240 },
241 /***
242 * file name validation
243 */
244 isValidFileName:{
245 value: function(fileName){
246 var status = false;
247 if(fileName !== ""){
248 fileName = fileName.replace(/^\s+|\s+$/g,"");
249 status = !(/[/\\]/g.test(fileName));
250 if(status && navigator.userAgent.indexOf("Macintosh") != -1){//for Mac files beginning with . are hidden
251 status = !(/^\./g.test(fileName));
252 }
253 }
254 return status;
255 }
256 },
257
258 /* ================= misc methods ================= */
259
260 // Generates an alpha-numeric random number
261 // len: number of chars
262 // default length is '8'
263 generateRandom: {
264 value: function(len) {
265 var length;
266 len ? length = len : length = 8;
267
268 return Uuid.generate().substring(0,length);
269 }
211 } 270 }
212 271
213}); 272});