client/js/models.js
changeset 293 fba23fde14ba
parent 282 12ee99b182cd
child 298 2f35c2ae7de8
equal deleted inserted replaced
292:f67047a16084 293:fba23fde14ba
     1 (function() {
     1 (function() {
     2     "use strict";
     2     "use strict";
     3     var root = this;
     3     var root = this;
     4     
     4 
     5     var Backbone = root.Backbone;
     5     var Backbone = root.Backbone;
     6     
     6 
     7     var Models = root.Rkns.Models = {};
     7     var Models = root.Rkns.Models = {};
     8     
     8 
     9     
     9 
    10     Models.getUID = function(obj) {
    10     Models.getUID = function(obj) {
    11         var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    11         var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    12             var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    12             var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
    13             return v.toString(16);
    13             return v.toString(16);
    14         });
    14         });
    15         return obj.type + "-" + guid; 
    15         return obj.type + "-" + guid;
    16     };
    16     };
    17     
    17 
    18     
    18 
    19     var RenkanModel = Backbone.RelationalModel.extend({
    19     var RenkanModel = Backbone.RelationalModel.extend({
    20         idAttribute : "_id",
    20         idAttribute : "_id",
    21         constructor: function(options) {
    21         constructor: function(options) {
    22             
    22 
    23             if (typeof options !== "undefined") {
    23             if (typeof options !== "undefined") {
    24                 options._id = options._id || options.id || Models.getUID(this);
    24                 options._id = options._id || options.id || Models.getUID(this);
    25                 options.title = options.title || "";
    25                 options.title = options.title || "";
    26                 options.description = options.description || "";
    26                 options.description = options.description || "";
    27                 options.uri = options.uri || "";
    27                 options.uri = options.uri || "";
    28                 
    28 
    29                 if(typeof this.prepare === "function") {
    29                 if(typeof this.prepare === "function") {
    30                     options = this.prepare(options);
    30                     options = this.prepare(options);
    31                 }
    31                 }
    32             }
    32             }
    33             Backbone.RelationalModel.prototype.constructor.call(this, options);
    33             Backbone.RelationalModel.prototype.constructor.call(this, options);
    45             else {
    45             else {
    46                 _options[_propName ] = _element;
    46                 _options[_propName ] = _element;
    47             }
    47             }
    48         }
    48         }
    49     });
    49     });
    50         
    50 
    51     // USER
    51     // USER
    52     var User = Models.User = RenkanModel.extend({
    52     var User = Models.User = RenkanModel.extend({
    53         type: "user",
    53         type: "user",
    54         prepare: function(options) {
    54         prepare: function(options) {
    55             options.color = options.color || "#666666";
    55             options.color = options.color || "#666666";
    63                 description: this.get("description"),
    63                 description: this.get("description"),
    64                 color: this.get("color")
    64                 color: this.get("color")
    65             };
    65             };
    66         }
    66         }
    67     });
    67     });
    68     
    68 
    69     // NODE
    69     // NODE
    70     var Node = Models.Node = RenkanModel.extend({
    70     var Node = Models.Node = RenkanModel.extend({
    71         type: "node",
    71         type: "node",
    72         relations: [{
    72         relations: [{
    73             type: Backbone.HasOne,
    73             type: Backbone.HasOne,
    93                 size: this.get("size"),
    93                 size: this.get("size"),
    94                 clip_path: this.get("clip_path")
    94                 clip_path: this.get("clip_path")
    95             };
    95             };
    96         }
    96         }
    97     });
    97     });
    98     
    98 
    99     // EDGE
    99     // EDGE
   100     var Edge = Models.Edge = RenkanModel.extend({
   100     var Edge = Models.Edge = RenkanModel.extend({
   101         type: "edge",
   101         type: "edge",
   102         relations: [
   102         relations: [
   103           {
   103           {
   134                 color: this.get("color"),
   134                 color: this.get("color"),
   135                 created_by: this.get("created_by") ? this.get("created_by").get("_id") : null
   135                 created_by: this.get("created_by") ? this.get("created_by").get("_id") : null
   136             };
   136             };
   137         }
   137         }
   138     });
   138     });
   139     
   139 
   140     // View
   140     // View
   141     var View = Models.View = RenkanModel.extend({
   141     var View = Models.View = RenkanModel.extend({
   142         type: "view",
   142         type: "view",
   143         relations: [
   143         relations: [
   144             {
   144             {
   158                 created_by: this.get("created_by") ? this.get("created_by").get("_id") : null
   158                 created_by: this.get("created_by") ? this.get("created_by").get("_id") : null
   159                 // Don't need project id
   159                 // Don't need project id
   160             };
   160             };
   161         }
   161         }
   162     });
   162     });
   163         
   163 
   164     // PROJECT
   164     // PROJECT
   165     var Project = Models.Project = RenkanModel.extend({
   165     var Project = Models.Project = RenkanModel.extend({
   166         type: "project",
   166         type: "project",
   167         relations: [
   167         relations: [
   168           {
   168           {
   207             var _user = User.findOrCreate(_props);
   207             var _user = User.findOrCreate(_props);
   208             this.get("users").push(_user, _options);
   208             this.get("users").push(_user, _options);
   209             return _user;
   209             return _user;
   210         },
   210         },
   211         addNode: function(_props, _options) {
   211         addNode: function(_props, _options) {
   212             _props.project = this;            
   212             _props.project = this;
   213             var _node = Node.findOrCreate(_props);
   213             var _node = Node.findOrCreate(_props);
   214             this.get("nodes").push(_node, _options);
   214             this.get("nodes").push(_node, _options);
   215             return _node;
   215             return _node;
   216         },
   216         },
   217         addEdge: function(_props, _options) {
   217         addEdge: function(_props, _options) {
   256         initialize: function() {
   256         initialize: function() {
   257             var _this = this;
   257             var _this = this;
   258             this.on("remove:nodes", function(_node) {
   258             this.on("remove:nodes", function(_node) {
   259                 _this.get("edges").remove(
   259                 _this.get("edges").remove(
   260                     _this.get("edges").filter(function(_edge) {
   260                     _this.get("edges").filter(function(_edge) {
   261                         return _edge.get("from") == _node || _edge.get("to") == _node;
   261                         return _edge.get("from") === _node || _edge.get("to") === _node;
   262                     })
   262                     })
   263                 );
   263                 );
   264             });
   264             });
   265         }
   265         }
   266     });
   266     });
   267     
   267 
   268     var RosterUser = Models.RosterUser = Backbone.Model.extend({
   268     var RosterUser = Models.RosterUser = Backbone.Model.extend({
   269         type: "roster_user",
   269         type: "roster_user",
   270         idAttribute : "_id",
   270         idAttribute : "_id",
   271         
   271 
   272         constructor: function(options) {
   272         constructor: function(options) {
   273             
   273 
   274             if (typeof options !== "undefined") {
   274             if (typeof options !== "undefined") {
   275                 options._id = options._id || options.id || Models.getUID(this);
   275                 options._id = options._id || options.id || Models.getUID(this);
   276                 options.title = options.title || "(untitled " + this.type + ")";
   276                 options.title = options.title || "(untitled " + this.type + ")";
   277                 options.description = options.description || "";
   277                 options.description = options.description || "";
   278                 options.uri = options.uri || "";
   278                 options.uri = options.uri || "";
   279                 options.project = options.project || null;
   279                 options.project = options.project || null;
   280                 options.site_id = options.site_id || 0;
   280                 options.site_id = options.site_id || 0;
   281                 
   281 
   282                 if(typeof this.prepare === "function") {
   282                 if(typeof this.prepare === "function") {
   283                     options = this.prepare(options);
   283                     options = this.prepare(options);
   284                 }
   284                 }
   285             }
   285             }
   286             Backbone.Model.prototype.constructor.call(this, options);
   286             Backbone.Model.prototype.constructor.call(this, options);
   287         },
   287         },
   288         
   288 
   289         validate: function() {
   289         validate: function() {
   290             if(!this.type) {
   290             if(!this.type) {
   291                 return "object has no type";
   291                 return "object has no type";
   292             }
   292             }
   293         },
   293         },
   294         
   294 
   295         prepare: function(options) {
   295         prepare: function(options) {
   296             options.color = options.color || "#666666";
   296             options.color = options.color || "#666666";
   297             return options;
   297             return options;
   298         },
   298         },
   299         
   299 
   300         toJSON: function() {
   300         toJSON: function() {
   301             return {
   301             return {
   302                 _id: this.get("_id"),
   302                 _id: this.get("_id"),
   303                 title: this.get("title"),
   303                 title: this.get("title"),
   304                 uri: this.get("uri"),
   304                 uri: this.get("uri"),
   307                 project: (this.get("project") != null)?this.get("project").get("id"):null,
   307                 project: (this.get("project") != null)?this.get("project").get("id"):null,
   308                 site_id: this.get("site_id")
   308                 site_id: this.get("site_id")
   309             };
   309             };
   310         }
   310         }
   311     });
   311     });
   312     
   312 
   313     var UsersList = Models.UsersList = Backbone.Collection.extend({
   313     var UsersList = Models.UsersList = Backbone.Collection.extend({
   314         model: RosterUser
   314         model: RosterUser
   315     });
   315     });
   316     
   316 
   317 
   317 
   318 }).call(window);
   318 }).call(window);
   319