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
33 if @is_a_string(str)
\r
41 @truncate: (str, length = null) ->
\r
42 s = Pettanr.to_s(str)
\r
43 if length and s.length > 3 # 3 = '...'
\r
44 r = s.substr(0, length - 4)
\r
45 r = r.concat( '...') if s.length > length - 3
\r
50 @format_date: (date) ->
\r
51 y = ('000' + (date.getFullYear())).slice(-4)
\r
52 m = ('0' + (date.getMonth() + 1)).slice(-2)
\r
53 d = ('0' + date.getDate()).slice(-2)
\r
54 h = ('0' + date.getHours()).slice(-2)
\r
55 n = ('0' + date.getMinutes()).slice(-2)
\r
56 s = ('0' + date.getSeconds()).slice(-2)
\r
57 y + '/' + m + '/' + d + ' ' + h + ':' + n + ':' + s
\r
59 @to_style: (hash) ->
\r
60 a = _.map hash, (n, k) ->
\r
61 Pettanr.to_s(k) + ': ' + Pettanr.to_s(n)
\r
64 @url: (controller_name, action_name, params) ->
\r
65 controller = Manifest.manifest().controllers[controller_name]
\r
66 action = controller.actions[action_name]
\r
69 class Pettanr.AppHelper
\r
73 @full_url: (filename) ->
\r
74 request.protocol + request.host_with_port + filename
\r
77 l = label.split('.')
\r
82 I18n.t('activerecord.models.' + Pettanr[label].item_name())
\r
84 Pettanr.AppHelper.t_a(Pettanr[l[0]].item_name(), l[1])
\r
86 @t_a: (item_name, attr_name) ->
\r
87 I18n.t('activerecord.attributes.' + item_name + '.' + attr_name)
\r
89 @t_select_items: (items) ->
\r
91 [I18n.t(i[0]), i[1]]
\r
93 @t_selected_item: (name, index) ->
\r
94 I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])
\r
96 @distance_of_time_in_words_to_now: (datetime) ->
\r
101 class Pettanr.FilerCollection extends Backbone.Collection
\r
103 initialize: (models, options) ->
\r
104 @model = options['model']
\r
105 @action = options['action']
\r
106 @params = options['params']
\r
108 @url = '/' + @action.url(@params)
\r
110 class Pettanr.AppController
\r
113 @operators = window.operators
\r
116 @my_controller = Manifest.manifest().controllers[@params['controller']]
\r
117 @my_action = @my_controller.actions[@params['action']]
\r
118 @my_model = Manifest.manifest().models[@my_action.item_name]
\r
119 @my_model_class = @my_model.classify()
\r
123 @my_list_model = Manifest.manifest().models[@my_action.item_name]
\r
124 @my_list_model_class = @my_list_model.classify()
\r
125 @list = Locmare.ListGroup.list(@my_action.item_name, @my_action.list_name)
\r
129 @item = new @my_model_class({id: @params['id']})
\r
134 @list.open(@operators, @params, @my_action, {}, (page_status) ->
\r
135 pager = new Locmare.FilerModule.DefaultPager({page_status: page_status})
\r
136 f = new Locmare.Filer({
\r
138 item_name: _this.my_list_model_class.item_name(),
\r
139 items: _this.list.models,
\r
141 operators: _this.operators
\r
148 @binder_controller = Manifest.manifest().controllers[@params['controller']]
\r
149 @binder_model = Manifest.manifest().models[@binder_controller.item_name]
\r
150 @binder_model_class = @binder_model.classify()
\r
151 @item = new @binder_model_class({id: @params['id']})
\r
156 id: @params['id'],
\r
157 offset: @params['offset'],
\r
158 count: @params['count'],
\r
159 page: @params['page'],
\r
160 page_size: @params['page_size']
\r
162 @item.fetch({cache: true}).done ->
\r
163 _this.list.open(_this.operators, opt, _this.my_action, {}, (page_status) ->
\r
164 console.log _this.list.models
\r
170 @item.fetch({cache: true}).done ->
\r
171 _this.item.boosts 'post'
\r
172 profiler = new Locmare.Profiler({
\r
173 item_name: _this.item.item_name(),
\r
175 operators: _this.operators
\r
177 $("#pettanr").html(profiler.render().el)
\r
178 _this.redraw_title(_this.params)
\r
182 @item = new @my_model_class({id: @params['id']})
\r
183 @item.boosts 'post'
\r
184 # Backbone.Model has defaults property
\r
185 #@item.supply_default()
\r
189 @item = new @my_model_class({id: @params['id']})
\r
191 @item.fetch({cache: true}).done ->
\r
192 _this.item.boosts 'post'
\r
193 _this.render_form()
\r
196 @form = new Locmare.Form({
\r
197 form_name: @item.item_name(),
\r
200 submit: 'default',
\r
201 operators: @operators,
\r
202 action: '/' + @item.table_name() + '/' + Pettanr.to_s(@item.get('id'))
\r
204 $("#pettanr").html(@form.render().el)
\r
213 redraw_title: (params, str = null) ->
\r
214 t = str || I18n.t(params['controller'] + '.' + params['action'] + '.title')
\r
215 site_caption = Manifest.manifest().magic_numbers.profile.users.caption
\r
216 $(document).attr('title', t + ' - ' + site_caption)
\r
218 class Pettanr.FilerRender
\r
219 constructor: (item_name, list_result, pager_type, operators) ->
\r
220 @item_name = item_name
\r
221 @list_result = list_result
\r
222 @pager_type = pager_type
\r
223 @operators = operators
\r
225 @list_result.fetch({cache: true}).done () ->
\r
229 items = @list_result.models
\r
230 f = new Locmare.Filer({
\r
231 item_name: @item_name,
\r
233 list_result: @list_result,
\r
234 pager_type: @pager_type,
\r
235 operators: @operators
\r
237 $("#pettanr").html(f.render().el)
\r
239 #>> https://gist.github.com/davidjbeveridge/3813724
\r
240 @xeach: (arr, func, index=0) ->
\r
241 if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []
\r
243 @camelize: (input) ->
\r
244 pieces = input.split(/[\W_-]/)
\r
245 @xeach(pieces, @capitalize).join("")
\r
247 @capitalize: (input) ->
\r
248 input.charAt(0).toUpperCase() + input.slice(1)
\r
250 @lowercase: (input) ->
\r
251 input.toLowerCase()
\r
253 @underscore: (input) ->
\r
254 pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n
\r
255 @xeach(pieces, @lowercase ).join("_")
\r