X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fassets%2Fjavascripts%2Fmodels%2Fpanel.js.coffee;h=22102c56c9d389f970cd6aed718049fb4dd4e1a9;hb=dbe1cc622a8fda2e7f24294b97dbd064c1cb197e;hp=2d900854155a74451601cffc59b934350d13b3c4;hpb=c7e2ff2c0ca44029e4241a488afe897c2dfa24d0;p=pettanr%2Fpettanr.git diff --git a/app/assets/javascripts/models/panel.js.coffee b/app/assets/javascripts/models/panel.js.coffee index 2d900854..22102c56 100644 --- a/app/assets/javascripts/models/panel.js.coffee +++ b/app/assets/javascripts/models/panel.js.coffee @@ -1,17 +1,4 @@ class Pettanr.Panel extends Peta.Root - url: '/panels/' - relations: [ - { - type: Backbone.HasMany, - key: 'panel_pictures', - relatedModel: 'Pettanr.PanelPicture', - collectionType: 'Pettanr.PanelPictures', - reverseRelation: { - key: 'panel', - includeInJSON: 'id' - } - }, - ] @singular: () -> 'Panel' @@ -21,14 +8,21 @@ class Pettanr.Panel extends Peta.Root defaults: { id: null, + width: 500, + height: 300, border: 2, publish: 0 } - pp: () -> - new Pettanr.PanelPicture({id: @get('pp_id')}) + with_elements: () -> + new Pettanr.Panel.WithElements({id: @get('id')}) - is_visible: (operators) -> + overwrite: (options) -> + operators = Pettanr.cache.operators + return false if not operators.author + @set('author_id', operators.author.id) + + is_visible: (operators = Pettanr.cache.operators) -> switch super(operators) when null # super return return true @@ -38,33 +32,60 @@ class Pettanr.Panel extends Peta.Root return true if @isNew() @is_publish() - is_usable: (operators) -> + overwrite: (options) -> + @set('author_id', Pettanr.cache.operators.author.id) + + is_usable: (operators = Pettanr.cache.operators) -> @is_visible(operators) is_publish: () -> @get('publish') > 0 + @licensed_pictures: (panels) -> + r = {} + _.each panels, (panel) -> + _.extend r, panel.licensed_pictures() + r + + licensed_pictures: () -> + r = {} + _.each @scenario_elements(), (elm) -> + return if not elm.my_class().has_picture() + pid = elm.get('picture_id') + r[pid] = true if not r[pid] + r + + label: (shorten_length) -> + @_label('caption', shorten_length) + initialize: (attr = {}, options = {}) -> + super(attr, options) @set(attr) - if @get('id') - @url = @url + @get('id') - if options.with_elements - @url = @url + '?with_elements=true' - -class Pettanr.Panels extends Backbone.Collection - model: Pettanr.Panel - url: '/panels/' -class Pettanr.RelationPanel extends Pettanr.Panel - relations: [ - { - type: Backbone.HasMany, - key: 'panel_pictures', - relatedModel: 'Pettanr.PanelPicture', - collectionType: 'Pettanr.PanelPictures', - reverseRelation: { - key: 'panel', - includeInJSON: 'id' - } - }, - ] + +class Pettanr.Panel.WithElements extends Pettanr.Panel + + fetch: (options) -> + @url = @default_url() + '?with_elements=true' + super(options) + + save: (attrs) -> + @url = @default_url() + method = if @isNew() + 'create' + else + 'update' + _this = this + xhr = @sync(method, this, { + attrs: {panel: attrs}, + success: (model, response, options) => + _this.set(model, {silent: true}) + _this.trigger('save:success', _this, response) + error: (model, response, options) => + _this.trigger('save:fail', _this, response) + }) + xhr + + destroy: (options) -> + @url = @my_url() + super(options)