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
97 if Pettanr.is_blank(datetime)
\r
100 $.timeago(datetime)
\r
105 class Pettanr.AppController
\r
108 @operators = window.operators
\r
109 _.extend(this, Backbone.Events)
\r
112 @my_controller = Manifest.manifest().controllers[@params['controller']]
\r
113 @my_action = @my_controller.actions[@params['action']]
\r
114 @my_model = Manifest.manifest().models[@my_action.item_name]
\r
115 @my_model_class = @my_model.classify()
\r
119 @my_list_model = Manifest.manifest().models[@my_action.item_name]
\r
120 @my_list_model_class = @my_list_model.classify()
\r
121 @list = Locmare.ListGroup.list(
\r
122 @my_action.path_name(), @my_action.name, @operators, @params
\r
127 @item = new @my_model_class({id: @params['id']})
\r
132 pager = Locmare.ListGroupModule.LibModule.Pager.factory(@list.page_status, @params)
\r
133 filer = new Locmare.Filer({
\r
134 item_name: @list.item_name,
\r
135 items: @list.items(),
\r
137 operators: @operators
\r
139 @trigger('ready', filer)
\r
145 @binder_action = @my_action.original
\r
146 @binder_controller = @binder_action.controller()
\r
147 @binder_model = Manifest.manifest().models[@binder_action.item_name]
\r
148 @binder_model_class = @binder_model.classify()
\r
149 @item = new @binder_model_class({id: @params['id']})
\r
153 console.log @list.items()
\r
158 @item.fetch({cache: true}).done =>
\r
159 @item.boosts 'post'
\r
160 profiler = new Locmare.Profiler({
\r
161 item_name: @item.item_name(),
\r
163 operators: @operators
\r
165 el ||= $("#pettanr")
\r
166 el.html(profiler.render().el)
\r
167 @trigger('retitle', @params)
\r
171 @item = new @my_model_class({id: @params['id']})
\r
172 @item.boosts 'post'
\r
173 # Backbone.Model has defaults property
\r
174 #@item.supply_default()
\r
178 @item = new @my_model_class({id: @params['id']})
\r
179 @item.fetch({cache: true}).done =>
\r
180 @item.boosts 'post'
\r
184 @form = Locmare.Form.factory({
\r
185 form_name: @item.item_name(),
\r
188 submit: 'default',
\r
189 operators: @operators,
\r
190 action: '/' + @item.table_name() + '/' + Pettanr.to_s(@item.get('id'))
\r
192 $("#pettanr").html(@form.render().el)
\r
201 class Pettanr.AppBody extends Backbone.View
\r
204 initialize: (options) ->
\r
212 @listenTo(body, 'navigate', @navigate)
\r
213 this.$el.html(body.el)
\r
216 c = Pettanr[Pettanr.camelize(params['controller']) + 'Controller']
\r
218 controller.params = params
\r
219 @listenTo(controller, 'ready', @refresh)
\r
220 @listenTo(controller, 'retitle', @retitle)
\r
221 controller[params['action']]()
\r
224 window.router.navigate(url, true)
\r
226 retitle: (params, str = null) ->
\r
227 t = str || I18n.t(params['controller'] + '.' + params['action'] + '.title')
\r
228 site_caption = _.escape(Manifest.manifest().magic_numbers.profile.users.caption)
\r
229 $(document).attr('title', t + ' - ' + site_caption)
\r
231 class Pettanr.Dialog extends Backbone.View
\r
233 initialize: (options) ->
\r
241 @listenTo(body, 'navigate', @navigate)
\r
242 this.$el.html(body.render().el)
\r
245 c = Pettanr[Pettanr.camelize(params['controller']) + 'Controller']
\r
247 controller.params = params
\r
248 @listenTo(controller, 'ready', @refresh)
\r
249 # ignore retitle message
\r
250 # @listenTo(controller, 'retitle', @retitle)
\r
251 controller[params['action']]()
\r
253 local_navigate: (url) ->
\r
254 window.router.local_navigate(url, true)
\r
257 #>> https://gist.github.com/davidjbeveridge/3813724
\r
258 @xeach: (arr, func, index=0) ->
\r
259 if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []
\r
261 @camelize: (input) ->
\r
262 pieces = input.split(/[\W_-]/)
\r
263 @xeach(pieces, @capitalize).join("")
\r
265 @capitalize: (input) ->
\r
266 input.charAt(0).toUpperCase() + input.slice(1)
\r
268 @lowercase: (input) ->
\r
269 input.toLowerCase()
\r
271 @underscore: (input) ->
\r
272 pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n
\r
273 @xeach(pieces, @lowercase ).join("_")
\r