client/js/renderer/baserepresentation.js
changeset 284 fa8035885814
child 293 fba23fde14ba
equal deleted inserted replaced
283:67f3a24a7c01 284:fa8035885814
       
     1 /* paper-renderer.js */
       
     2 "use strict";
       
     3 define(['jquery', 'underscore'], function ($, _) {
       
     4 
       
     5     /* Rkns.Renderer._BaseRepresentation Class */
       
     6 
       
     7     /* In Renkan, a "Representation" is a sort of ViewModel (in the MVVM paradigm) and bridges the gap between
       
     8      * models (written with Backbone.js) and the view (written with Paper.js)
       
     9      * Renkan's representations all inherit from Rkns.Renderer._BaseRepresentation '*/
       
    10 
       
    11     var _BaseRepresentation = function(_renderer, _model) {
       
    12         if (typeof _renderer !== "undefined") {
       
    13             this.renderer = _renderer;
       
    14             this.renkan = _renderer.renkan;
       
    15             this.project = _renderer.renkan.project;
       
    16             this.options = _renderer.renkan.options;
       
    17             this.model = _model;
       
    18             if (this.model) {
       
    19                 var _this = this;
       
    20                 this._changeBinding = function() {
       
    21                     _this.redraw();
       
    22                 };
       
    23                 this._removeBinding = function() {
       
    24                     _renderer.removeRepresentation(_this);
       
    25                     _(function() {
       
    26                         _renderer.redraw();
       
    27                     }).defer();
       
    28                 };
       
    29                 this._selectBinding = function() {
       
    30                     _this.select();
       
    31                 };
       
    32                 this._unselectBinding = function() {
       
    33                     _this.unselect();
       
    34                 };
       
    35                 this.model.on("change", this._changeBinding );
       
    36                 this.model.on("remove", this._removeBinding );
       
    37                 this.model.on("select", this._selectBinding );
       
    38                 this.model.on("unselect", this._unselectBinding );
       
    39             }
       
    40         }
       
    41     };
       
    42 
       
    43     /* Rkns.Renderer._BaseRepresentation Methods */
       
    44 
       
    45     _(_BaseRepresentation.prototype).extend({
       
    46         _super: function(_func) {
       
    47             return _BaseRepresentation.prototype[_func].apply(this, Array.prototype.slice.call(arguments, 1));
       
    48         },
       
    49         redraw: function() {},
       
    50         moveTo: function() {},
       
    51         show: function() { return "chaud cacao"; },
       
    52         hide: function() {},
       
    53         select: function() {
       
    54             if (this.model) {
       
    55                 this.model.trigger("selected");
       
    56             }
       
    57         },
       
    58         unselect: function() {
       
    59             if (this.model) {
       
    60                 this.model.trigger("unselected");
       
    61             }
       
    62         },
       
    63         highlight: function() {},
       
    64         unhighlight: function() {},
       
    65         mousedown: function() {},
       
    66         mouseup: function() {
       
    67             if (this.model) {
       
    68                 this.model.trigger("clicked");
       
    69             }
       
    70         },
       
    71         destroy: function() {
       
    72             if (this.model) {
       
    73                 this.model.off("change", this._changeBinding );
       
    74                 this.model.off("remove", this._removeBinding );
       
    75                 this.model.off("select", this._selectBinding );
       
    76                 this.model.off("unselect", this._unselectBinding );
       
    77             }
       
    78         }
       
    79     });
       
    80 
       
    81     /* End of Rkns.Renderer._BaseRepresentation Class */
       
    82     
       
    83     return _BaseRepresentation;
       
    84 
       
    85 });
       
    86