OSDN Git Service

merge:
[pettanr/pettanr.git] / app / assets / javascripts / models / panel.js.coffee
index 0683896..d652d92 100644 (file)
@@ -1,5 +1,6 @@
 class Pettanr.Panel extends Peta.Root\r
   url: '/panels/'\r
+  \r
   @singular: () ->\r
     'Panel'\r
   \r
@@ -8,14 +9,88 @@ class Pettanr.Panel extends Peta.Root
   \r
   defaults: {\r
     id: null,\r
+    width: 500,\r
+    height: 300,\r
     border: 2,\r
     publish: 0\r
   } \r
   \r
-  initialize: () ->\r
-    if @id\r
-      @url = @url + @id\r
+  author: () ->\r
+    new Pettanr.Cache.Retriever(Pettanr.Author, @get('author_id'))\r
+  \r
+  with_elements: () ->\r
+    new Pettanr.Panel.WithElements({id: @get('id')})\r
+  \r
+  overwrite: (options) ->\r
+    operators = options.operators\r
+    return false if not operators.author\r
+    @set('author_id', operators.author.id)\r
+  \r
+  is_visible: (operators) ->\r
+    switch super(operators)\r
+      when null # super return\r
+        return true\r
+      when false\r
+        return false\r
+      else\r
+        return true if @isNew()\r
+        @is_publish()\r
+  \r
+  overwrite: (options) ->\r
+    @set('author_id', options.operators.author.id)\r
+  \r
+  is_usable: (operators) ->\r
+    @is_visible(operators)\r
+  \r
+  is_publish: () ->\r
+    @get('publish') > 0\r
+  \r
+  @licensed_pictures: (panels) ->\r
+    r = {}\r
+    _.each panels, (panel) ->\r
+      _.extend r, panel.licensed_pictures()\r
+    r\r
+  \r
+  licensed_pictures: () ->\r
+    r = {}\r
+    _.each @scenario_elements(), (elm) ->\r
+      return if not elm.my_class().has_picture()\r
+      pid = elm.get('picture_id')\r
+      r[pid] = elm.picture() if not r[pid]\r
+    r\r
+  \r
+  my_url: () ->\r
+    '/' + @path_name() + '/' + @get('id')\r
+  \r
+  initialize: (attr = {}, options = {}) ->\r
+    super(attr, options)\r
+    @set(attr)\r
+    if @get('id')\r
+      @url = @my_url()\r
+  \r
+class Pettanr.Panel.WithElements extends Pettanr.Panel\r
+  \r
+  fetch: (options) ->\r
+    @url = @my_url() + '?with_elements=true'\r
+    super(options)\r
+  \r
+  save: (attrs) ->\r
+    @url = @my_url()\r
+    method = if @isNew()\r
+      'create'\r
+    else\r
+      'update'\r
+    _this = this\r
+    xhr = @sync(method, this, {\r
+      attrs: {panel: attrs},\r
+      success: (model, response, options) =>\r
+        _this.trigger('save:success', _this, response)\r
+      error: (model, response, options) =>\r
+        _this.trigger('save:fail', _this, response)\r
+    })\r
+    xhr\r
+  \r
+  destroy: (options) ->\r
+    @url = @my_url()\r
+    super(options)\r
   \r
-class Pettanr.Panel.Collection extends Backbone.Collection\r
-  model: Pettanr.Panel\r
-  url: '/panels'\r