server/src/main/webapp/js/main.js
changeset 13 6d0e0100f4e8
parent 9 2ab67fa1e78d
child 14 f7d9fe3ef6f3
equal deleted inserted replaced
9:2ab67fa1e78d 13:6d0e0100f4e8
     1 /* 
       
     2  *  Copyright 2012 Institut de recherche et d'innovation 
       
     3  *  contributor(s) : Samuel Huron, Raphael Velt
       
     4  *   
       
     5  *  contact@iri.centrepompidou.fr
       
     6  *  http://www.iri.centrepompidou.fr 
       
     7  *   
       
     8  *  This software is a computer program whose purpose is to show and add annotations on a video .
       
     9  *  This software is governed by the CeCILL-C license under French law and
       
    10  *  abiding by the rules of distribution of free software. You can  use, 
       
    11  *  modify and/ or redistribute the software under the terms of the CeCILL-C
       
    12  *  license as circulated by CEA, CNRS and INRIA at the following URL
       
    13  *  "http://www.cecill.info". 
       
    14  *  
       
    15  *  The fact that you are presently reading this means that you have had
       
    16  *  knowledge of the CeCILL-C license and that you accept its terms.
       
    17 */
       
    18 
       
    19 /* Declaring the Renkan Namespace Rkns and Default values */
       
    20 
       
    21 Rkns = {
       
    22     _MIN_DRAG_DISTANCE: 4,
       
    23     _NODE_RADIUS: 20,
       
    24     _NODE_FONT_SIZE: 14,
       
    25     _ARROW_LENGTH: 20,
       
    26     _ARROW_WIDTH: 15,
       
    27     _RENDER: 1,
       
    28     _SAVE: 2,
       
    29     _RENDER_AND_SAVE: 3
       
    30 }
       
    31 
       
    32 Rkns.$ = jQuery;
       
    33 
       
    34 Rkns._ = _;
       
    35 
       
    36 Rkns.Serializers = {};
       
    37 
       
    38 Rkns.Serializers._Base = function(_project) {
       
    39     if (typeof _project !== "undefined") {
       
    40         this._project = _project;
       
    41         this._callbackQueue = [];
       
    42         this._loaded = false;
       
    43     }
       
    44 }
       
    45 
       
    46 Rkns.Serializers._Base.prototype.deserialize = function() {}
       
    47 
       
    48 Rkns.Serializers._Base.prototype.deferCallback = function(_callback) {
       
    49     var _this = this;
       
    50     Rkns._.defer(function() {
       
    51         _callback.call(_this);
       
    52     });
       
    53 }
       
    54 
       
    55 Rkns.Serializers._Base.prototype.handleCallbacks = function() {
       
    56     this._loaded = true;
       
    57     while (this._callbackQueue.length) {
       
    58         this.deferCallback(this._callbackQueue.splice(0,1)[0]);
       
    59     }
       
    60 }
       
    61 
       
    62 Rkns.Serializers._Base.prototype.onLoad = function(_callback) {
       
    63     if (this._loaded) {
       
    64         this.deferCallback(_callback);
       
    65     } else {
       
    66         this._callbackQueue.push(_callback);
       
    67     }
       
    68 }
       
    69 
       
    70 Rkns.Serializers._Base.prototype.save = function() {}
       
    71 
       
    72 Rkns.Project = function(_opts) {
       
    73     if (typeof _opts.serializer == "undefined") {
       
    74         _opts.serializer = "BasicJson";
       
    75     }
       
    76     if (typeof _opts.language == "undefined" || typeof Rkns.i18n[_opts.language] == "undefined") {
       
    77         _opts.language = "en";
       
    78     }
       
    79     this.l10n = Rkns.i18n[_opts.language];
       
    80     this._opts = _opts;
       
    81     this.users = new Rkns.Model.List();
       
    82     this.nodes = new Rkns.Model.List();
       
    83     this.edges = new Rkns.Model.List();
       
    84     if (typeof this._opts.user === "object") {
       
    85         this.current_user = this.addUser(this._opts.user)
       
    86     }
       
    87     this.serializer = new Rkns.Serializers[_opts.serializer](this);
       
    88     this.renderer = new Rkns.Renderer.Scene(this);
       
    89     var _this = this;
       
    90     this.serializer.onLoad(function() {
       
    91         if (typeof _this.current_user === "undefined") {
       
    92             _this.current_user = _proj.users[0];
       
    93         }
       
    94         _this.renderer.draw();
       
    95     });
       
    96 }
       
    97 
       
    98 Rkns.Project.prototype.addNode = function(_props, _render_save) {
       
    99     var _node = new Rkns.Model.Node(this, _props);
       
   100     this.nodes.push(_node);
       
   101     if (typeof _render_save !== "undefined" && (_render_save&Rkns._RENDER)) {
       
   102         var _controller = this.renderer.addController("Node", _node);
       
   103         _controller.redraw();
       
   104     }
       
   105     if (typeof _render_save !== "undefined" && (_render_save&Rkns._SAVE)) {
       
   106         this.serializer.save();
       
   107     }
       
   108     return _node;
       
   109 }
       
   110 
       
   111 Rkns.Project.prototype.addEdge = function(_props, _render_save) {
       
   112     var _edge = new Rkns.Model.Edge(this, _props);
       
   113     this.edges.push(_edge);
       
   114     if (typeof _render_save !== "undefined" && (_render_save&Rkns._RENDER)) {
       
   115         var _controller = this.renderer.addController("Edge", _edge);
       
   116         _controller.redraw();
       
   117     }
       
   118     if (typeof _render_save !== "undefined" && (_render_save&Rkns._SAVE)) {
       
   119         this.serializer.save();
       
   120     }
       
   121     return _edge;
       
   122 }
       
   123 
       
   124 Rkns.Project.prototype.addUser = function(_props, _render_save) {
       
   125     var _user = new Rkns.Model.User(this, _props);
       
   126     this.users.push(_user);
       
   127     return _user;
       
   128 }
       
   129 
       
   130 Rkns.Project.prototype.updateElement = function(_element, _props, _render_save) {
       
   131     Rkns._(_props).each(function(_v, _k) {
       
   132         _element[_k] = _v;
       
   133     });
       
   134     if (typeof _render_save !== "undefined" && (_render_save&Rkns._RENDER)) {
       
   135         if (typeof _element.__controller !== "undefined") {
       
   136             _element.__controller.redraw();
       
   137         } else {
       
   138             this._renderer.redraw();
       
   139         }
       
   140     }
       
   141     if (typeof _render_save !== "undefined" && (_render_save&Rkns._SAVE)) {
       
   142         this.serializer.save();
       
   143     }
       
   144 }
       
   145 
       
   146 Rkns.Project.prototype.removeNode = function(_node, _render_save) {
       
   147     this.nodes.removeId(_node.id);
       
   148     if (typeof _node.__controller !== "undefined") {
       
   149         console.log("Controllers", this.renderer.controllers.length);
       
   150         this.renderer.removeController(_node.__controller);
       
   151         console.log("Controllers", this.renderer.controllers.length);
       
   152     }
       
   153     console.log(this.edges.length);
       
   154     var _this = this;
       
   155     this.edges = this.edges.filter(function(_edge) {
       
   156         var _keep = _edge.from !== _node && _edge.to !== _node;
       
   157         if (!_keep && typeof _edge.__controller !== "undefined") {
       
   158             _this.renderer.removeController(_edge.__controller);
       
   159         }
       
   160         return _keep;
       
   161     });
       
   162     if (typeof _render_save !== "undefined" && (_render_save&Rkns._RENDER)) {
       
   163         this.renderer.redraw();
       
   164     }
       
   165     if (typeof _render_save !== "undefined" && (_render_save&Rkns._SAVE)) {
       
   166         this.serializer.save();
       
   167     }
       
   168     return _node;
       
   169 }
       
   170 
       
   171 /* Utility functions */
       
   172 
       
   173 Rkns.Utils = {
       
   174     _ID_AUTO_INCREMENT : 0,
       
   175     _ID_BASE : (function(_d) {
       
   176         function pad(n){return n<10 ? '0'+n : n}
       
   177         function fillrand(n) {
       
   178             var _res = ''
       
   179             for (var i=0; i<n; i++) {
       
   180                 _res += Math.floor(16*Math.random()).toString(16);
       
   181             }
       
   182             return _res;
       
   183         }
       
   184         return _d.getUTCFullYear() + '-'  
       
   185             + pad(_d.getUTCMonth()+1) + '-'  
       
   186             + pad(_d.getUTCDate()) + '-'
       
   187             + fillrand(16);
       
   188     })(new Date()),
       
   189     getUID : function(_base) {
       
   190         var _n = (++this._ID_AUTO_INCREMENT).toString(16),
       
   191             _base = (typeof _base === "undefined" ? "" : _base + "-" );
       
   192         while (_n.length < 4) {
       
   193             _n = '0' + _n
       
   194         }
       
   195         return _base + this._ID_BASE + '-' + _n;
       
   196     },
       
   197     inherit : function(_baseClass, _callbefore) {
       
   198         var _class = function() {
       
   199             if (typeof _callbefore === "function") {
       
   200                 _callbefore.apply(this, Array.prototype.slice.call(arguments, 0));
       
   201             }
       
   202             if (typeof _baseClass.prototype._init !== "function") {
       
   203                 _baseClass.prototype._init = function() {}
       
   204             }
       
   205             _baseClass.apply(this, Array.prototype.slice.call(arguments, 0));
       
   206             this._init.apply(this, Array.prototype.slice.call(arguments, 0));
       
   207         }
       
   208         _class.prototype = new _baseClass();
       
   209         return _class;
       
   210     }
       
   211 }