client/js/renderer/baserepresentation.js
changeset 284 fa8035885814
child 293 fba23fde14ba
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/js/renderer/baserepresentation.js	Mon May 05 17:43:37 2014 +0200
@@ -0,0 +1,86 @@
+/* paper-renderer.js */
+"use strict";
+define(['jquery', 'underscore'], function ($, _) {
+
+    /* Rkns.Renderer._BaseRepresentation Class */
+
+    /* In Renkan, a "Representation" is a sort of ViewModel (in the MVVM paradigm) and bridges the gap between
+     * models (written with Backbone.js) and the view (written with Paper.js)
+     * Renkan's representations all inherit from Rkns.Renderer._BaseRepresentation '*/
+
+    var _BaseRepresentation = function(_renderer, _model) {
+        if (typeof _renderer !== "undefined") {
+            this.renderer = _renderer;
+            this.renkan = _renderer.renkan;
+            this.project = _renderer.renkan.project;
+            this.options = _renderer.renkan.options;
+            this.model = _model;
+            if (this.model) {
+                var _this = this;
+                this._changeBinding = function() {
+                    _this.redraw();
+                };
+                this._removeBinding = function() {
+                    _renderer.removeRepresentation(_this);
+                    _(function() {
+                        _renderer.redraw();
+                    }).defer();
+                };
+                this._selectBinding = function() {
+                    _this.select();
+                };
+                this._unselectBinding = function() {
+                    _this.unselect();
+                };
+                this.model.on("change", this._changeBinding );
+                this.model.on("remove", this._removeBinding );
+                this.model.on("select", this._selectBinding );
+                this.model.on("unselect", this._unselectBinding );
+            }
+        }
+    };
+
+    /* Rkns.Renderer._BaseRepresentation Methods */
+
+    _(_BaseRepresentation.prototype).extend({
+        _super: function(_func) {
+            return _BaseRepresentation.prototype[_func].apply(this, Array.prototype.slice.call(arguments, 1));
+        },
+        redraw: function() {},
+        moveTo: function() {},
+        show: function() { return "chaud cacao"; },
+        hide: function() {},
+        select: function() {
+            if (this.model) {
+                this.model.trigger("selected");
+            }
+        },
+        unselect: function() {
+            if (this.model) {
+                this.model.trigger("unselected");
+            }
+        },
+        highlight: function() {},
+        unhighlight: function() {},
+        mousedown: function() {},
+        mouseup: function() {
+            if (this.model) {
+                this.model.trigger("clicked");
+            }
+        },
+        destroy: function() {
+            if (this.model) {
+                this.model.off("change", this._changeBinding );
+                this.model.off("remove", this._removeBinding );
+                this.model.off("select", this._selectBinding );
+                this.model.off("unselect", this._unselectBinding );
+            }
+        }
+    });
+
+    /* End of Rkns.Renderer._BaseRepresentation Class */
+    
+    return _BaseRepresentation;
+
+});
+