server/src/main/webapp/js/model.js
changeset 9 2ab67fa1e78d
equal deleted inserted replaced
8:8e67c9f6da51 9:2ab67fa1e78d
       
     1 Rkns.Model = {}
       
     2 
       
     3 Rkns.Model._BaseElement = function(_project, _props) {
       
     4     if (typeof _props !== "undefined") {
       
     5         this._project = _project;
       
     6         this.id = _props.id || Rkns.Utils.getUID(this.type);
       
     7         this.title = _props.title || "(untitled " + this.type + ")";
       
     8         this.description = _props.description || "";
       
     9         this.uri = _props.uri || "";
       
    10     }
       
    11 }
       
    12 
       
    13 Rkns.Model._BaseElement.prototype.addReference = function(_propName, _list, _id, _default) {
       
    14     var _element = _list.getElement(_id);
       
    15     if (typeof _element === "undefined" && typeof _default !== "undefined") {
       
    16         this[ _propName ] = _default;
       
    17         this[ _propName + "_id" ] = _default.id;
       
    18     } else {
       
    19         this[ _propName + "_id" ] = _id;
       
    20         this[ _propName ] = _element;
       
    21     }
       
    22 }
       
    23 
       
    24 Rkns.Model._BaseElement.prototype.updateGraphics = function() {
       
    25     this._project.renderer.redraw();
       
    26 }
       
    27 
       
    28 Rkns.Model._BaseElement.prototype.updateData = function() {
       
    29     this._project.serializer.save(this);
       
    30 }
       
    31 
       
    32 /* Element Class Generator */
       
    33 
       
    34 Rkns.Model._elementClass = function(_type) {
       
    35     return Rkns.Utils.inherit(Rkns.Model._BaseElement, function() {
       
    36         this.type = _type;
       
    37     });
       
    38 }
       
    39 
       
    40 /* User Model */
       
    41 
       
    42 Rkns.Model.User = Rkns.Model._elementClass("user");
       
    43 
       
    44 Rkns.Model.User.prototype._init = function(_project, _props) {
       
    45     this.color = _props.color || "#666666";
       
    46 }
       
    47 
       
    48 /* Node Model */
       
    49 
       
    50 Rkns.Model.Node = Rkns.Model._elementClass("node");
       
    51 
       
    52 Rkns.Model.Node.prototype._init = function(_project, _props) {
       
    53     this.addReference("created_by", this._project.users, _props.created_by, _project.current_user);
       
    54     this.position = _props.position;
       
    55     this.description = _props.description || "";
       
    56 }
       
    57 
       
    58 Rkns.Model.Node.prototype.setPosition = function(_x, _y) {
       
    59     if (typeof _x === "object") {
       
    60         if (typeof _x.x !== "undefined" && typeof _x.y !== "undefined") {
       
    61             this.position.x = _x.x;
       
    62             this.position.y = _x.y;
       
    63         } else {
       
    64             if (typeof _x.length !== "undefined") {
       
    65                 this.position.x = _x[0];
       
    66                 this.position.y = _x[1];
       
    67             }
       
    68         }
       
    69     } else {
       
    70         if (typeof _y !== "undefined") {
       
    71             this.position.x = +_x;
       
    72             this.position.y = +_y;
       
    73         }
       
    74     }
       
    75 }
       
    76 
       
    77 /* Edge Model */
       
    78 
       
    79 Rkns.Model.Edge = Rkns.Model._elementClass("edge");
       
    80 
       
    81 Rkns.Model.Edge.prototype._init = function(_project, _props) {
       
    82     this.addReference("created_by", this._project.users, _props.created_by, _project.current_user);
       
    83     this.addReference("from", this._project.nodes, _props.from);
       
    84     this.addReference("to", this._project.nodes, _props.to);
       
    85 }
       
    86 
       
    87 /* List Helper Functions -- See Metadataplayer */
       
    88 
       
    89 Rkns.Model.List = function() {
       
    90     Array.call(this);
       
    91     this.idIndex = [];
       
    92 }
       
    93 
       
    94 Rkns.Model.List.prototype = new Array();
       
    95 
       
    96 Rkns.Model.List.prototype.hasId = function(_id) {
       
    97     return Rkns._(this.idIndex).include(_id);
       
    98 }
       
    99 
       
   100 Rkns.Model.List.prototype.getIds = function(_id) {
       
   101     return this.idIndex;
       
   102 }
       
   103 
       
   104 /* On recent browsers, forEach and map are defined and do what we want.
       
   105  * Otherwise, we'll use the Underscore.js functions
       
   106  */
       
   107 if (typeof Array.prototype.forEach === "undefined") {
       
   108     Rkns.Model.List.prototype.forEach = function(_callback) {
       
   109         var _this = this;
       
   110         Rkns._(this).forEach(function(_value, _key) {
       
   111             _callback(_value, _key, _this);
       
   112         });
       
   113     }
       
   114 }
       
   115 
       
   116 if (typeof Array.prototype.map === "undefined") {
       
   117     Rkns.Model.List.prototype.map = function(_callback) {
       
   118         var _this = this;
       
   119         return Rkns._(this).map(function(_value, _key) {
       
   120             return _callback(_value, _key, _this);
       
   121         });
       
   122     }
       
   123 }
       
   124 
       
   125 /* We override Array's filter function because it doesn't return an Rkns.Model.List
       
   126  */
       
   127 Rkns.Model.List.prototype.filter = function(_callback) {
       
   128     var _this = this,
       
   129         _res = new Rkns.Model.List();
       
   130     _res.addElements(Rkns._(this).filter(function(_value, _key) {
       
   131         return _callback(_value, _key, _this);
       
   132     }));
       
   133     return _res;
       
   134 }
       
   135 
       
   136 Rkns.Model.List.prototype.slice = function(_start, _end) {
       
   137     var _res = new Rkns.Model.List();
       
   138     _res.addElements(Array.prototype.slice.call(this, _start, _end));
       
   139     return _res;
       
   140 }
       
   141 
       
   142 Rkns.Model.List.prototype.splice = function(_start, _end) {
       
   143     var _res = new Rkns.Model.List();
       
   144     _res.addElements(Array.prototype.splice.call(this, _start, _end));
       
   145     this.idIndex.splice(_start, _end);
       
   146     return _res;
       
   147 }
       
   148 
       
   149 /* Array has a sort function, but it's not as interesting as Underscore.js's sortBy
       
   150  * and won't return a new Rkns.Model.List
       
   151  */
       
   152 Rkns.Model.List.prototype.sortBy = function(_callback) {
       
   153     var _this = this,
       
   154         _res = new Rkns.Model.List();
       
   155     _res.addElements(Rkns._(this).sortBy(function(_value, _key) {
       
   156         return _callback(_value, _key, _this);
       
   157     }));
       
   158     return _res;
       
   159 }
       
   160 
       
   161 Rkns.Model.List.prototype.push = function(_el) {
       
   162     if (typeof _el === "undefined" || typeof _el.id === "undefined") {
       
   163         return;
       
   164     }
       
   165     var _index = (Rkns._(this.idIndex).indexOf(_el.id));
       
   166     if (_index === -1) {
       
   167         this.idIndex.push(_el.id);
       
   168         Array.prototype.push.call(this, _el);
       
   169     } else {
       
   170         this[_index] = _el;
       
   171     }
       
   172 }
       
   173 
       
   174 Rkns.Model.List.prototype.addElements = function(_array) {
       
   175     var _this = this;
       
   176     Rkns._(_array).forEach(function(_el) {
       
   177         _this.push(_el);
       
   178     });
       
   179 }
       
   180 
       
   181 Rkns.Model.List.prototype.removeId = function(_id) {
       
   182     var _index = (Rkns._(this.idIndex).indexOf(_id));
       
   183     if (_index !== -1) {
       
   184         this.splice(_index,1);
       
   185     }
       
   186 }
       
   187 
       
   188 Rkns.Model.List.prototype.removeIds = function(_list) {
       
   189     var _this = this;
       
   190     Rkns._(_list).forEach(function(_id) {
       
   191         _this.removeId(_id);
       
   192     });
       
   193 }
       
   194 
       
   195 Rkns.Model.List.prototype.getElement = function(_id) {
       
   196     var _index = Rkns._(this.idIndex).indexOf(_id);
       
   197     if (_index === -1) {
       
   198         return undefined;
       
   199     } else {
       
   200         return this[_index];
       
   201     }
       
   202 }