X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fassets%2Fjavascripts%2Fpettanr.js.coffee;h=544c2502cc2f90066cd2e5c14b3a1c518701dab6;hb=b16d4899d5e7332f572a40ba1ca32250a7952413;hp=d16a41f296373477f7b742f4ca961b4683b626b3;hpb=b404a6f66817a05b366a452fdf5eda4f9ac9c50f;p=pettanr%2Fpettanr.git diff --git a/app/assets/javascripts/pettanr.js.coffee b/app/assets/javascripts/pettanr.js.coffee index d16a41f2..544c2502 100644 --- a/app/assets/javascripts/pettanr.js.coffee +++ b/app/assets/javascripts/pettanr.js.coffee @@ -1,4 +1,6 @@ class Pettanr + # Foo.class -> Pettanr[@constructor.name] + @is_blank: (str) -> !str?.trim() @@ -18,11 +20,62 @@ class Pettanr str isFinite(n) + @to_i: (str) -> + if @is_a_string(str) + parseInt(str) + else + if @is_number(str) + Math.floor(str) + else + 0 + @to_s: (str) -> if @is_a_string(str) str else - '' + if @is_number(str) + str.toString() + else + '' + + @truncate: (str, length = null) -> + s = Pettanr.to_s(str) + if length and s.length > 3 # 3 = '...' + r = s.substr(0, length - 4) + r = r.concat( '...') if s.length > length - 3 + r + else + Pettanr.to_s(str) + + @format_date: (date) -> + y = ('000' + (date.getFullYear())).slice(-4) + m = ('0' + (date.getMonth() + 1)).slice(-2) + d = ('0' + date.getDate()).slice(-2) + h = ('0' + date.getHours()).slice(-2) + n = ('0' + date.getMinutes()).slice(-2) + s = ('0' + date.getSeconds()).slice(-2) + y + '/' + m + '/' + d + ' ' + h + ':' + n + ':' + s + + @to_style: (hash) -> + a = _.map hash, (n, k) -> + Pettanr.to_s(k) + ': ' + Pettanr.to_s(n) + a.join('; ') + + @url: (controller_name, action_name, params) -> + if controller = Manifest.manifest().controllers[controller_name] + # convert from manifest + action = controller.actions[action_name] + action.url(params) + else + # has no manifest + controller_name + '/' + action_name + + @params_to_url: (params) -> + return '' if params['controller'] == 'folders' and params['action'] == 'root' + @url(params['controller'], params['action'], params) + + @is_sns: () -> + Manifest.manifest().magic_numbers.run_mode != 0 class Pettanr.AppHelper @manifest: () -> @@ -39,94 +92,28 @@ 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[0]].item_name(), l[1]) + @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]] + [I18n.t(i[0]), i[1]] @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() - else - name - size = if opt['size'] - parseInt(opt['size']) - else - 35 - "\""" - @distance_of_time_in_words_to_now: (datetime) -> - datetime - - class Pettanr.AppController - - 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() + if Pettanr.is_blank(datetime) + '' + else + $.timeago(datetime) - 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) - + + @cache = null # set by main + @credits = {} + #>> https://gist.github.com/davidjbeveridge/3813724 @xeach: (arr, func, index=0) -> if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []