From 9e3c10d4e12e896107c8551b4e6fc1dfbaf7bda1 Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Thu, 16 Feb 2012 21:42:18 -0800 Subject: Support for data attributes and adding a random string generator Signed-off-by: Valerio Virgillito --- js/lib/NJUtils.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/js/lib/NJUtils.js b/js/lib/NJUtils.js index 8daafa42..887743c5 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 */ var Montage = require("montage/core/core").Montage, + Uuid = require("montage/core/uuid").Uuid, ElementModel = require("js/models/element-model").ElementModel, Properties3D = require("js/models/properties-3d").Properties3D, ShapeModel = require("js/models/shape-model").ShapeModel, @@ -56,13 +57,18 @@ exports.NJUtils = Object.create(Object.prototype, { ///// Quick "createElement" function "attr" can be classname or object ///// with attribute key/values + ///// Suppor for data attributes make : { value: function(tag, attr) { var el = document.createElement(tag); if (typeof attr === 'object') { for (var a in attr) { if (attr.hasOwnProperty(a)) { - el[a] = attr[a]; + if(a.indexOf("data-") > -1) { + el.setAttribute(a, attr[a]); + } else { + el[a] = attr[a]; + } } } } else if (typeof attr === 'string') { @@ -247,6 +253,20 @@ exports.NJUtils = Object.create(Object.prototype, { } return status; } + }, + + /* ================= misc methods ================= */ + + // Generates an alpha-numeric random number + // len: number of chars + // default length is '8' + generateRandom: { + value: function(len) { + var length; + len ? length = len : length = 8; + + return Uuid.generate().substring(0,length); + } } }); -- cgit v1.2.3 From e0bead5a36ae987c558607b77745c4018caf59f8 Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Thu, 16 Feb 2012 21:42:56 -0800 Subject: Adding a data-RDGE-id attribute to each canvas to save/load canvas data Signed-off-by: Valerio Virgillito --- js/controllers/elements/shapes-controller.js | 1 + js/tools/BrushTool.js | 2 +- js/tools/LineTool.js | 2 +- js/tools/PenTool.js | 2 +- js/tools/ShapeTool.js | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/js/controllers/elements/shapes-controller.js b/js/controllers/elements/shapes-controller.js index 640119c4..15a18a51 100755 --- a/js/controllers/elements/shapes-controller.js +++ b/js/controllers/elements/shapes-controller.js @@ -59,6 +59,7 @@ exports.ShapesController = Montage.create(CanvasController, { break; case "useWebGl": var canvas = njModule.NJUtils.makeNJElement("canvas", "Canvas", "shape", el.className, true); + canvas.setAttribute("data-RDGE-id", njModule.NJUtils.generateRandom()); canvas.width = el.width; canvas.height = el.height; this.application.ninja.elementMediator.replaceElement(el, canvas); diff --git a/js/tools/BrushTool.js b/js/tools/BrushTool.js index ce8ffbb9..4d44326f 100755 --- a/js/tools/BrushTool.js +++ b/js/tools/BrushTool.js @@ -201,7 +201,7 @@ exports.BrushTool = Montage.create(ShapeTool, { var top = Math.round(midPt[1] - 0.5 * h); if (!canvas) { - var newCanvas = NJUtils.makeNJElement("canvas", "Brushstroke", "shape", null, true); + var newCanvas = NJUtils.makeNJElement("canvas", "Brushstroke", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true); var elementModel = TagTool.makeElement(w, h, planeMat, midPt, newCanvas); ElementMediator.addElement(newCanvas, elementModel.data, true); diff --git a/js/tools/LineTool.js b/js/tools/LineTool.js index 99c374a7..233316a5 100755 --- a/js/tools/LineTool.js +++ b/js/tools/LineTool.js @@ -82,7 +82,7 @@ exports.LineTool = Montage.create(ShapeTool, { h += ~~(yAdj*2); } - canvas = NJUtils.makeNJElement("canvas", "Canvas", "shape", null, true); + canvas = NJUtils.makeNJElement("canvas", "Canvas", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true); var elementModel = TagTool.makeElement(w, h, drawData.planeMat, drawData.midPt, canvas); ElementMediator.addElement(canvas, elementModel.data, true); diff --git a/js/tools/PenTool.js b/js/tools/PenTool.js index 9a69b53d..7749d525 100755 --- a/js/tools/PenTool.js +++ b/js/tools/PenTool.js @@ -570,7 +570,7 @@ exports.PenTool = Montage.create(ShapeTool, { if (!canvas) { var newCanvas = null; - newCanvas = NJUtils.makeNJElement("canvas", "Subpath", "shape", null, true); + newCanvas = NJUtils.makeNJElement("canvas", "Subpath", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true); var elementModel = TagTool.makeElement(parseInt(w), parseInt(h), planeMat, midPt, newCanvas); ElementMediator.addElement(newCanvas, elementModel.data, true); diff --git a/js/tools/ShapeTool.js b/js/tools/ShapeTool.js index 4ccb7a8d..29e9b99f 100755 --- a/js/tools/ShapeTool.js +++ b/js/tools/ShapeTool.js @@ -66,7 +66,7 @@ exports.ShapeTool = Montage.create(DrawingTool, { var canvas; if(!this._useExistingCanvas()) { - canvas = NJUtils.makeNJElement("canvas", "Canvas", "shape", null, true); + canvas = NJUtils.makeNJElement("canvas", "Canvas", "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true); var elementModel = TagTool.makeElement(~~drawData.width, ~~drawData.height, drawData.planeMat, drawData.midPt, canvas); -- cgit v1.2.3