X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fassets%2Fjavascripts%2Fpettanr.js.coffee;h=c5ed2df332bc53c9c2350cb1b4fe19e729734940;hb=a4838064801c37fddc101f493c654cf0b2c78db4;hp=3704d7ec0c4b8575f408543f1b3a1b04f39f82a8;hpb=72d848d01fbd2e6ab6abc0794e55df1cae42a79b;p=pettanr%2Fpettanr.git diff --git a/app/assets/javascripts/pettanr.js.coffee b/app/assets/javascripts/pettanr.js.coffee index 3704d7ec..c5ed2df3 100644 --- a/app/assets/javascripts/pettanr.js.coffee +++ b/app/assets/javascripts/pettanr.js.coffee @@ -1,4 +1,31 @@ class Pettanr + # Foo.class -> Pettanr[@constructor.name] + + @is_blank: (str) -> + !str?.trim() + + @is_a_string: (str) -> + if (str instanceof String) or (typeof str == "string") + true + else + false + + @is_number: (str) -> + if typeof(str) != 'number' && typeof(str) != 'string' + return false + else + n = if typeof(str) == 'string' + parseInt(str) + else + str + isFinite(n) + + @to_s: (str) -> + if @is_a_string(str) + str + else + '' + class Pettanr.AppHelper @manifest: () -> Manifest.manifest @@ -14,8 +41,11 @@ class Pettanr if l.length == 1 I18n.t('activerecord.models.' + Pettanr[label].item_name()) else - I18n.t('activerecord.models.attribtes.' + Pettanr[l.first].item_name() + '.' + l.last) + Pettanr.AppHelper.t_a(Pettanr[l.first].item_name(), l.last) + @t_a: (item_name, attr_name) -> + I18n.t('activerecord.attributes.' + item_name + '.' + attr_name) + @t_select_items: (items) -> _.map items, (i) -> [t(i[0]), i[1]] @@ -23,6 +53,48 @@ class Pettanr @t_selected_item: (name, index) -> I18n.t(Manifest.manifest().system_resources.select_items[name][index][0]) + class Pettanr.AppHelperImg extends Backbone.View + tagName: 'img' + + initialize: (options) -> + @src = options.src + @size = options.size + + render: () -> + this.$el.attr('src', @src) + this.$el.attr('width', @size) + this.$el.attr('height', @size) + this + + @image_tag: (src, opt = {}) -> + size = if opt['size'] + parseInt(opt['size']) + else + 35 + i = new Pettanr.AppHelperImg({src: src, size: size}) + i.render().el.outerHTML + + class Pettanr.AppHelperIcon extends Backbone.View + tagName: 'img' + + initialize: (options) -> + @name = options.name + @opt = options.opt + title = if @opt['object'] + name + ' ID:' + @opt['object'].get(id).toString() + else + name + size = if @opt['size'] + parseInt(@opt['size']) + else + 35 + + render: () -> + this.$el.attr('src', '/images/' + Pettanr.underscore(@name) + '.gif') + this.$el.attr('width', @size) + this.$el.attr('height', @size) + this + @icon_tag: (name, opt = {}) -> title = if opt['object'] name + ' ID:' + opt['object'].get(id).toString() @@ -33,8 +105,87 @@ class Pettanr else 35 "\""" - + + @distance_of_time_in_words_to_now: (datetime) -> + datetime + + class Pettanr.CounterModel extends Backbone.Model + initialize: (attr, options) -> + @url = options['url'] + + class Pettanr.FilerCollection extends Backbone.Collection + + initialize: (models, options) -> + params = options['params'] + i = if params['id'] + params['id'] + '/' + else + '' + a = if params['action'] != 'index' + params['action'] + else + '' + + @page = params['page'] + @page_size = params['page_size'] + q = [] + q.push('page=' + Pettanr.to_s(@page)) if @page + q.push('page_size=' + Pettanr.to_s(@page_size)) if @page_size + qry = if q.length > 0 + '?' + q.join('&') + else + '' + @url = '/' + params['controller'] + '/' + i + a + qry + @total = new Pettanr.CounterModel({}, {url: '/' + params['controller'] + '/' + i + 'count_' + a}) + @model = options['model'] + class Pettanr.AppController + constructor: () -> + @params = {} + + set_model: () -> + @my_controller = Manifest.manifest().controllers[@params['controller']] + @my_action = @my_controller.actions[@params['action']] + @my_model = Manifest.manifest().models[@my_controller.item_name] + @my_model_class = @my_model.classify() + + set_list: () -> + @set_model() + @my_list_model = Manifest.manifest().models[@my_action.item_name] + @my_list_model_class = @my_list_model.classify() + @list = new Pettanr.FilerCollection({}, {model: @my_list_model_class, params: @params}) + + filer_list: () -> + @set_list() + f = new Locmare.Filer({ + el: "#pettanr", + item_name: @my_list_model_class.item_name(), + items: [], + list_result: @list, + pager_type: 'default', + operators: window.operators + }) + + class Pettanr.FilerRender + constructor: (item_name, list_result, pager_type, operators) -> + @item_name = item_name + @list_result = list_result + @pager_type = pager_type + @operators = operators + _this = this + @list_result.fetch({}).done () -> + _this.render() + + render: () -> + items = @list_result.models + f = new Locmare.Filer({ + item_name: @item_name, + items: items, + list_result: @list_result, + pager_type: @pager_type, + operators: @operators + }) + $("#pettanr").html(f.render().el) #>> https://gist.github.com/davidjbeveridge/3813724 @xeach: (arr, func, index=0) -> @@ -54,10 +205,4 @@ class Pettanr pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n @xeach(pieces, @lowercase ).join("_") # << - @is_a_string: (str) -> - if (str instanceof String) or (typeof str == "string") - true - else - false - @Pettanr = Pettanr