X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=blobdiff_plain;f=app%2Fassets%2Fjavascripts%2Fpeta%2Fitem.js.coffee;h=f5fa53bf40ce40db4646889a7abc8face366760a;hp=294ee7aa1b2021203ad9287e83f816cfd5b50de5;hb=e05f18d1db38e531e7bca45d4ba8db71b082402f;hpb=9529441f04dd2c502077a051b866eac838e5849e diff --git a/app/assets/javascripts/peta/item.js.coffee b/app/assets/javascripts/peta/item.js.coffee index 294ee7aa..f5fa53bf 100644 --- a/app/assets/javascripts/peta/item.js.coffee +++ b/app/assets/javascripts/peta/item.js.coffee @@ -1,4 +1,12 @@ class Peta.Item extends Backbone.Model + + initialize: (attr = {}, options = {}) -> + @url = @default_url() + super(attr, options) + @expire_time = options.expire_time + @boosters = {} + @templates = {} + @child_models: () -> @my_manifest().child_models() @@ -7,8 +15,14 @@ class Peta.Item extends Backbone.Model # ClassMethods + @default_url: () -> + '/' + @table_name() + '/' + + @my_class: () -> + this + my_class: () -> - Pettanr[@constructor.name] + @constructor @my_peta: () -> return null if not Manifest.manifest().items @@ -22,7 +36,8 @@ class Peta.Item extends Backbone.Model 'Name' @plural: () -> - 'Names' + table_name = Manifest.manifest().pluralize @item_name() + Pettanr.camelize(table_name) @class_name: () -> @singular() @@ -51,20 +66,17 @@ class Peta.Item extends Backbone.Model @parent_model: () -> null + @has_picture: () -> + false + @path_name: (with_engine = false) -> @table_name() - @pickup_item_name: () -> - @item_name() - - @pickup_column_name: () -> - 'id' - @find_boost_name: (column_name) -> - my_peta().find_boost_name column_name - + @my_peta().find_boost_name column_name + @is_extend_column: (column_name) -> - my_peta().is_extend_column column_name + @my_peta().is_extend_column column_name @fold_extend_settings: (attr) -> _.each my_peta().boost, (name, manifest) -> @@ -72,13 +84,27 @@ class Peta.Item extends Backbone.Model if my_settings attr[manifest.settings_column_name] = my_settings.to_json + @retrieve: (id, context, options = {}) -> + item = new this({id: id}) + item.retrieve(context, options) + + @trace_routes: () -> + {} + + @default_label_shorten_length = 13 + #InstanceMethods + default_url: () -> + r = @my_class().default_url() + r = r.concat( @id ) if @id + r + singular: () -> - Pettanr[@constructor.name].singular() + @my_class().singular() plural: () -> - Pettanr[@constructor.name].plural() + @my_class().plural() item_name: () -> @my_class().item_name() @@ -92,15 +118,6 @@ class Peta.Item extends Backbone.Model path_name: (with_engine = false) -> @my_class().path_name(with_engine) - pickup_item_name: () -> - @my_class().pickup_item_name() - - pickup_column_name: () -> - @my_class().pickup_column_name() - - pickup_id: () -> - @get(pickup_column_name()) - form_template: (with_engine = false) -> @path_name(with_engine) + '/form' @@ -110,31 +127,324 @@ class Peta.Item extends Backbone.Model else @item_name() + _label: (label_column_name, options) -> + shorten = options.shorten + l = if _.isBoolean(shorten) + if shorten + # supply default shorten_length + @constructor.default_label_shorten_length + else + # False be no shorten + null + else + # numeric + shorten + Pettanr.truncate(@get(label_column_name), l) + + label: (options) -> + '' + + get_association: (routes, context, options) -> + routes = [routes] if _.isString(routes) + route = routes.shift() + if _.isEmpty(routes) + # fetching terminate association. callback + cxt = options.context || context + @fetch_association(route, cxt, { + success: (association_item, options) => + options.success.call(context, association_item) + context: context, + options: options + }) + else + # fetching through associations + @fetch_association(route, this, { + success: (association_item, options) => + association_item.get_association(routes, this, options) + context: context, + options: options + }) + + fetch_association: (name, context, options) => + a = @my_class().my_manifest().associations + fetch_options = { + success: (association_item) => + options.success.call(context, association_item, options.options) + } + if a.belongs_to[name] + @get_parent(name, context, fetch_options) + else if a.has_many[name] + @get_children(name, context, fetch_options) + else if a.has_one[name] + @get_child(name, context, fetch_options) + else + console.error('association does not exist in model manifest') + + get_parent: (belongs_to_name, context, options = null) -> + m = Manifest.item_name_to_model(belongs_to_name) + retriever = new Pettanr.Cache.Retriever(m, @get(belongs_to_name + '_id')) + return retriever if !options + @listenTo(retriever, 'retrieve', (item) => + options.success.call(context, item) + ) + @listenTo(retriever, 'fail', (response, opt) => + options.fail.call(context, response, opt) + ) + retriever.retrieve() + + get_child: (has_one_name, context, options = null) -> + list = @has_one(has_one_name) + list.open(context, { + success: (items) => + callback = options.success + item = items[0] + callback.call(context, item) + }) + + get_children: (has_many_name, context, options = null) -> + list = @has_many(has_many_name) + list.open(context, { + success: (items) => + callback = options.success + callback.call(context, items) + }) + + has_many: (has_many_name) -> + has_many_manifest = @my_class().my_manifest().associations.has_many[has_many_name] + action_name = has_many_manifest.list_action_name + Locmare.ListGroup.list( + has_many_name, action_name, {id: @get('id')} + ) + + has_one: (has_one_name) -> + has_one_manifest = @my_class().my_manifest().associations.has_one[has_one_name] + controller_name = has_one_manifest.model().table_name() + action_name = has_one_manifest.list_action_name + Locmare.ListGroup.list( + controller_name, action_name, {id: @get('id')} + ) + + trace_to: (trace_name, context, options) -> + routes = @my_class().trace_routes()[trace_name] + if !routes + console.error('no trace route') + return + @get_association(routes, this, { + success: (association) => + options.success.call(context, association) + }) + boosts: (level) -> - _.each @my_class().my_peta().boost, (boost_name, boost_manifest) -> - next if level == 'read' and boost_manifest.level == 'post' + c = @my_class().my_peta().boost + _.each c, (boost_manifest, boost_name) => + return if level == 'read' and boost_manifest.level == 'post' @boost boost_manifest boost: (boost_manifest) -> - @boosters ||= {} - @boosters[boost_manifest.name] ||= new Locmare.Booster(boost_manifest, this) - - boosters: () -> - @boosters ||= {} + @boosters[boost_manifest.name] = new Locmare.Booster(boost_manifest, this) + # fetched item is not cleared . force write booster + #@boosters[boost_manifest.name] ||= new Locmare.Booster(boost_manifest, this) is_extend_column: (column_name) -> @is_extend_column column_name - is_user_visible: (operators) -> - if Manifest.manifest.magic_numbers['run_mode'] == 0 - return false if not operators.is_guest + is_visible: (operators = Pettanr.cache.operators) -> + if Manifest.manifest().magic_numbers['run_mode'] == 0 + return false if not operators.is_guest() else - return false if not operators.is_resource_reader + return false if not operators.is_resource_reader() true - is_visible: (operators) -> - return false if not @is_user_visible(operators) - true + retriever: () -> + new Pettanr.Cache.Retriever(@my_class(), @get('id')) + + retrieve: (context, options = {}) -> + retriever = new Pettanr.Cache.Retriever(@my_class(), @get('id')) + @listenTo(retriever, 'retrieve', (item) => + callback = options.success + callback.call(context, item) + ) + @listenTo(retriever, 'fail', (response, opt) => + callback = options.fail + callback.call(context, response, opt) + ) + retriever.retrieve(options.force) + + @pick_item_name: () -> + null + + @pick_model: () -> + Manifest.item_name_to_model(@pick_item_name()) + + @traceable_item_names: () -> + [] + + @is_traceable: (item_name) -> + _.contains(@traceable_item_names(), item_name) + + @is_picker_inspire: (item_name) -> + item_name == @item_name() + + @is_picker_trace: (item_name) -> + @is_traceable(item_name) + + @is_picker_pick: (item_name) -> + item_name == @pick_item_name() + + @pick_type: (item_name) -> + if @is_picker_inspire(item_name) + 'inspire' + else if @is_picker_trace(item_name) + 'trace' + else if @is_picker_pick(item_name) + 'pick' + else + 'none' + + symbol_option: (context, options) -> + @trace_to('symbol', this, { + success: (symbol_item) => + options.success.call(context, symbol_item.symbol_file()) + }) + + @face_file: () -> + new Pettanr.ImageFile('/images/' + @item_name() + '.gif') + + face_file: () -> + @my_class().face_file() + + # thumbnail size picture + symbol_file: (subdir) -> + new Pettanr.PictureFile(this, subdir) + + # real size picture + picture_file: (subdir = null) -> + new Pettanr.PictureFile(this, subdir) + + real_picture: (subdir = null) -> + new Pettanr.View.RealIcon(@picture_file(subdir)) + + # item.face_button({ + # context: this, + # click: () -> + # # ... + # }) + face_button: (options) -> + Pettanr.View.face_button(this, @face_file(), options) + + mini_face_button: (options) -> + Pettanr.View.mini_face_button(this, @face_file(), options) + + prof_button: (options) -> + Pettanr.View.prof_button(@prof_url(), options) + + mini_prof_button: (options) -> + Pettanr.View.mini_prof_button(@prof_url(), options) + + symbol_button: (options) -> + Pettanr.View.face_button(this, @symbol_file(), options) + + mini_symbol_button: (options) -> + Pettanr.View.mini_face_button(this, @symbol_file(), options) + + # pencil button + edit_button: (options) -> + icon = new Pettanr.View.Icon(Pettanr.View.Image.icon_edit_file()) + Pettanr.View.any_button(this, @edit_url(), icon, options) + + mini_edit_button: (options) -> + icon = new Pettanr.View.Minicon(Pettanr.View.Image.icon_edit_file()) + Pettanr.View.mini_any_button(this, @edit_url(), icon, options) + + # x button + destroy_button: (options) -> + icon = new Pettanr.View.Icon(Pettanr.View.Image.icon_destroy_file()) + Pettanr.View.any_button(this, @destroy_url(), icon, options) + + mini_destroy_button: (options) -> + icon = new Pettanr.View.Minicon(Pettanr.View.Image.icon_destroy_file()) + Pettanr.View.mini_any_button(this, @destroy_url(), icon, options) + + label_button: (label_options, button_options) -> + url = button_options.url || @show_url() + new Pettanr.View.Button(url, _.escape(@label(label_options)), button_options) + + # faced_label_button({ + # shorten: true + # }, { + # url: @show_url(), + # context: this, + # click: () => + # # ... + # }) + faced_label_button: (label_options, button_options) -> + new Pettanr.View.FacedLabelButton(this, label_options, button_options) + + mini_faced_label_button: (label_options, button_options) -> + new Pettanr.View.MiniFacedLabelButton(this, label_options, button_options) + + summary: (context, options) -> + klass = Pettanr.Views[@singular()].Summary + new klass(this, context, options) + + @index_url: () -> + Pettanr.url(@table_name(), 'index', {id: null}) + + index_url: () -> + @my_class().index_url() + + list_url: (action_name = 'index') -> + Pettanr.url(@table_name(), action_name, {id: @get('id')}) + + show_url: () -> + Pettanr.url(@table_name(), 'show', {id: @get('id')}) + + prof_url: () -> + Pettanr.url(@table_name(), 'show', {id: @get('id'), format: 'prof'}) + + new_url: () -> + Pettanr.url(@table_name(), 'new', {}) + + create_url: () -> + Pettanr.url(@table_name(), 'create', {}) + + edit_url: () -> + Pettanr.url(@table_name(), 'edit', {id: @get('id')}) + + update_url: () -> + Pettanr.url(@table_name(), 'update', {id: @get('id')}) + + destroy_url: () -> + Pettanr.url(@table_name(), 'destroy', {id: @get('id')}) + + hold: () -> + Pettanr.cache.hold(this) + + fix: () -> + Pettanr.cache.fix(this) + + release: () -> + Pettanr.cache.release(this) + + free: () -> + Pettanr.cache.free(this) + + save: (model_attr) -> + super(model_attr || @attributes, { + success: (model, response, options) => + @trigger('save:success', model, response) + error: (model, response, options) => + @trigger('save:fail', model, response) + }) + + destroy: () -> + super({ + success: (model, response, options) => + @free() + @trigger('destroy:success', model, response) + error: (model, response, options) => + @trigger('destroy:fail', model, response) + }) is_editize: () -> @editor @@ -142,6 +452,9 @@ class Peta.Item extends Backbone.Model dom_id: () -> (@get('id') || '').toString() + cache_key: () -> + @table_name() + '-' + @dom_id() + dom_pool_type: () -> @new_record ? 'stored' : 'new'