2 # Foo.class -> Pettanr[@constructor.name]
\r
7 @is_a_string: (str) ->
\r
8 if (str instanceof String) or (typeof str == "string")
\r
13 @is_number: (str) ->
\r
14 if typeof(str) != 'number' && typeof(str) != 'string'
\r
17 n = if typeof(str) == 'string'
\r
24 if @is_a_string(str)
\r
29 class Pettanr.AppHelper
\r
33 @full_url: (filename) ->
\r
34 request.protocol + request.host_with_port + filename
\r
37 l = label.split('.')
\r
42 I18n.t('activerecord.models.' + Pettanr[label].item_name())
\r
44 Pettanr.AppHelper.t_a(Pettanr[l.first].item_name(), l.last)
\r
46 @t_a: (item_name, attr_name) ->
\r
47 I18n.t('activerecord.attributes.' + item_name + '.' + attr_name)
\r
49 @t_select_items: (items) ->
\r
53 @t_selected_item: (name, index) ->
\r
54 I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])
\r
56 class Pettanr.AppHelperImg extends Backbone.View
\r
59 initialize: (options) ->
\r
61 @size = options.size
\r
64 this.$el.attr('src', @src)
\r
65 this.$el.attr('width', @size)
\r
66 this.$el.attr('height', @size)
\r
69 @image_tag: (src, opt = {}) ->
\r
70 size = if opt['size']
\r
71 parseInt(opt['size'])
\r
74 i = new Pettanr.AppHelperImg({src: src, size: size})
\r
75 i.render().el.outerHTML
\r
77 class Pettanr.AppHelperIcon extends Backbone.View
\r
80 initialize: (options) ->
\r
81 @name = options.name
\r
83 title = if @opt['object']
\r
84 name + ' ID:' + @opt['object'].get(id).toString()
\r
87 size = if @opt['size']
\r
88 parseInt(@opt['size'])
\r
93 this.$el.attr('src', '/images/' + Pettanr.underscore(@name) + '.gif')
\r
94 this.$el.attr('width', @size)
\r
95 this.$el.attr('height', @size)
\r
98 @icon_tag: (name, opt = {}) ->
\r
99 title = if opt['object']
\r
100 name + ' ID:' + opt['object'].get(id).toString()
\r
103 size = if opt['size']
\r
104 parseInt(opt['size'])
\r
107 "<img src=\"" + '/images/' + Pettanr.underscore(name) + '.gif' + "\" width=" + size.toString() + " height=" + size.toString() + " alt=\"" + name + ' Icon' + "title=\"" + title + ">"
\r
109 @distance_of_time_in_words_to_now: (datetime) ->
\r
112 class Pettanr.CounterModel extends Backbone.Model
\r
113 initialize: (attr, options) ->
\r
114 @url = options['url']
\r
119 class Pettanr.Pager extends Backbone.View
\r
121 initialize: (options) ->
\r
122 @action = options['action']
\r
123 @params = options['params']
\r
124 @current_page = @params['page']
\r
125 @per_page = @params['page_size']
\r
127 @counter = new Pettanr.CounterModel({}, {url: '/' + @action.counter_url(@params)})
\r
129 @counter.fetch().done ->
\r
130 _this.total_page = (@total / _this.per_page) + 1
\r
132 class Pettanr.FilerCollection extends Backbone.Collection
\r
134 initialize: (models, options) ->
\r
135 @model = options['model']
\r
136 @action = options['action']
\r
137 @params = options['params']
\r
139 @url = '/' + @action.url(@params)
\r
141 class Pettanr.AppController
\r
146 @my_controller = Manifest.manifest().controllers[@params['controller']]
\r
147 @my_action = @my_controller.actions[@params['action']]
\r
148 @my_model = Manifest.manifest().models[@my_controller.item_name]
\r
149 @my_model_class = @my_model.classify()
\r
153 @my_list_model = Manifest.manifest().models[@my_action.item_name]
\r
154 @my_list_model_class = @my_list_model.classify()
\r
155 @pager = new Pettanr.Pager({action: @my_action, params: @params})
\r
156 @list = new Pettanr.FilerCollection({}, {model: @my_list_model_class, action: @my_action, params: @params})
\r
160 f = new Locmare.Filer({
\r
162 item_name: @my_list_model_class.item_name(),
\r
163 collection: @list,
\r
165 operators: window.operators
\r
168 class Pettanr.FilerRender
\r
169 constructor: (item_name, list_result, pager_type, operators) ->
\r
170 @item_name = item_name
\r
171 @list_result = list_result
\r
172 @pager_type = pager_type
\r
173 @operators = operators
\r
175 @list_result.fetch({}).done () ->
\r
179 items = @list_result.models
\r
180 f = new Locmare.Filer({
\r
181 item_name: @item_name,
\r
183 list_result: @list_result,
\r
184 pager_type: @pager_type,
\r
185 operators: @operators
\r
187 $("#pettanr").html(f.render().el)
\r
189 #>> https://gist.github.com/davidjbeveridge/3813724
\r
190 @xeach: (arr, func, index=0) ->
\r
191 if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []
\r
193 @camelize: (input) ->
\r
194 pieces = input.split(/[\W_-]/)
\r
195 @xeach(pieces, @capitalize).join("")
\r
197 @capitalize: (input) ->
\r
198 input.charAt(0).toUpperCase() + input.slice(1)
\r
200 @lowercase: (input) ->
\r
201 input.toLowerCase()
\r
203 @underscore: (input) ->
\r
204 pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n
\r
205 @xeach(pieces, @lowercase ).join("_")
\r