From e37e8829bcfdb8d2d59458eb70b3484d418e87f8 Mon Sep 17 00:00:00 2001 From: trunerd Date: Sat, 4 Jun 2016 16:45:47 -0600 Subject: [PATCH] Added some utilities to the sketch framework for javascript --- src/main/js/protobufUtils/classCreator.js | 30 +++++- .../js/protobufUtils/sketchProtoConverter.js | 95 ++++++++++++++++++- 2 files changed, 123 insertions(+), 2 deletions(-) diff --git a/src/main/js/protobufUtils/classCreator.js b/src/main/js/protobufUtils/classCreator.js index 737a444..ede29bf 100644 --- a/src/main/js/protobufUtils/classCreator.js +++ b/src/main/js/protobufUtils/classCreator.js @@ -63,9 +63,37 @@ define([], function () { } }; + /** + * Generates an rfc4122 version 4 compliant solution. + * + * found at http://stackoverflow.com/a/2117523/2187510 and further improved at + * http://stackoverflow.com/a/8809472/2187510 + * @returns {String} A unique id. + */ + function generateUuid() { + var d = new Date().getTime(); + var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + var r = (d + Math.random() * 16) % 16 | 0; + d = Math.floor(d / 16); + return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16); + }); + return uuid; + } + + /** + * Creates a number that represents the current time in milliseconds since jan 1st 1970. + * + * @return {Number} milliseconds since jan 1st 1970 + */ + function createTimeStamp() { + return new Date().getTime(); + } + return { isUndefined: isUndefined, - Inherits: Inherits + Inherits: Inherits, + generateUuid: generateUuid, + createTimeStamp: createTimeStamp }; }); diff --git a/src/main/js/protobufUtils/sketchProtoConverter.js b/src/main/js/protobufUtils/sketchProtoConverter.js index ad3abed..a479edf 100644 --- a/src/main/js/protobufUtils/sketchProtoConverter.js +++ b/src/main/js/protobufUtils/sketchProtoConverter.js @@ -2,13 +2,19 @@ * Created by David Windows on 5/17/2016. */ define(['./../generated_proto/sketch', // protoSketch + './../generated_proto/commands', // protoCommands + './../generated_proto/sketchUtil', // protoCommands './../protobufUtils/classCreator', // protobufUtils "require" // require ], function ( + protoCommands, protoSketch, + protoSketchUtil, protobufUtils, require) { var sketch = protoSketch.protobuf.srl.sketch; + var Commands = protoCommands.protobuf.srl.commands; + var sketchUtil = protoSketchUtil.protobuf.srl.utils; var ObjectType = sketch.ObjectType; var ObjectMessage = sketch.SrlObject; @@ -137,11 +143,98 @@ define(['./../generated_proto/sketch', // protoSketch } }; + /** + * Given a protobuf Command array an SrlUpdate is created. + * + * It is important to node that an SrlUpdate implies that the commands + * happened at the same time. + * + * @param {Array} commands - A list of commands stored as an array. + * @return {SrlUpdate} An update that holds the list of given commands. + */ + var createUpdateFromCommands = function createUpdateFromCommands(commands) { + var update = new Commands.SrlUpdate(); + update.setCommands(commands); + var n = protobufUtils.createTimeStamp(); + update.setTime('' + n); + update.setUpdateId(protobufUtils.generateUuid()); + return update; + }; + + /** + * Given a protobuf Command array an SrlUpdate is created. + * + * It is important to node that an SrlUpdate implies that the commands + * happened at the same time. + * + * @return {SrlUpdate} An empty update. + */ + var createBaseUpdate = function createBaseUpdate() { + var update = new Commands.SrlUpdate(); + var n = protobufUtils.createTimeStamp(); + update.commands = []; + update.setTime('' + n); + update.setUpdateId(protobufUtils.generateUuid()); + return update; + }; + + /** + * Creates a command given the commandType and if the user created. + * + * @param {CommandType} commandType - The enum object of the commandType (found at + * CourseSketch.prutil.CommandType). + * @param {Boolean} userCreated - True if the user created this command, false if the + * command is system created. + * @returns {SrlCommand} Creates a command with basic data. + */ + var createBaseCommand = function createBaseCommand(commandType, userCreated) { + var command = new Commands.SrlCommand(); + command.setCommandType(commandType); + command.setIsUserCreated(userCreated); + command.commandId = protobufUtils.generateUuid(); // unique ID + return command; + }; + + /** + * Creates a new sketch command. + * + * @param {String} id - the id of the sketch, undefined if you want a random id given. + * @param {Number} x - the x location of the sketch as an offset of its parent sketch. + * @param {Number} y - the y location of the sketch as an offset of its parent sketch. + * @param {Number} width - the width of the sketch. + * @param {Number} height - the height of the sketch. + * + * @return {SrlCommand} a create sketch command + */ + var createNewSketch = function createNewSketch(id, x, y, width, height) { + var command = createBaseCommand(Commands.CommandType.CREATE_SKETCH, false); + var idChain = sketchUtil.IdChain(); + if (!protobufUtils.isUndefined(id)) { + idChain.idChain = [ id ]; + } else { + idChain.idChain = [ protobufUtils.generateUuid() ]; + } + var createSketchAction = new Commands.ActionCreateSketch(); + createSketchAction.sketchId = idChain; + createSketchAction.x = x || (x === 0 ? 0 : -1); + createSketchAction.y = y || (y === 0 ? 0 : -1); + createSketchAction.width = width || (width === 0 ? 0 : -1); + createSketchAction.height = height || (height === 0 ? 0 : -1); + command.setCommandData(createSketchAction.toArrayBuffer()); + return command; + }; + return { decode: decode, encodeSrlObject: encodeSrlObject, decodeSrlObject: decodeSrlObject, - convertToUpgradedSketchObject: convertToUpgradedSketchObject + convertToUpgradedSketchObject: convertToUpgradedSketchObject, + commands: { + createUpdateFromCommands: createUpdateFromCommands, + createBaseUpdate: createBaseUpdate, + createBaseCommand: createBaseCommand, + createNewSketch: createNewSketch + } }; });