5 @is_a_string: (str) ->
\r
6 if (str instanceof String) or (typeof str == "string")
\r
11 @is_number: (str) ->
\r
12 if typeof(str) != 'number' && typeof(str) != 'string'
\r
15 n = if typeof(str) == 'string'
\r
22 if @is_a_string(str)
\r
27 class Pettanr.AppHelper
\r
31 @full_url: (filename) ->
\r
32 request.protocol + request.host_with_port + filename
\r
35 l = label.split('.')
\r
40 I18n.t('activerecord.models.' + Pettanr[label].item_name())
\r
42 I18n.t('activerecord.models.attribtes.' + Pettanr[l.first].item_name() + '.' + l.last)
\r
44 @t_select_items: (items) ->
\r
48 @t_selected_item: (name, index) ->
\r
49 I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])
\r
51 class Pettanr.AppHelperImg extends Backbone.View
\r
54 initialize: (options) ->
\r
56 @size = options.size
\r
59 this.$el.attr('src', @src)
\r
60 this.$el.attr('width', @size)
\r
61 this.$el.attr('height', @size)
\r
64 @image_tag: (src, opt = {}) ->
\r
65 size = if opt['size']
\r
66 parseInt(opt['size'])
\r
69 i = new Pettanr.AppHelperImg({src: src, size: size})
\r
70 i.render().el.outerHTML
\r
72 class Pettanr.AppHelperIcon extends Backbone.View
\r
75 initialize: (options) ->
\r
76 @name = options.name
\r
78 title = if @opt['object']
\r
79 name + ' ID:' + @opt['object'].get(id).toString()
\r
82 size = if @opt['size']
\r
83 parseInt(@opt['size'])
\r
88 this.$el.attr('src', '/images/' + Pettanr.underscore(@name) + '.gif')
\r
89 this.$el.attr('width', @size)
\r
90 this.$el.attr('height', @size)
\r
93 @icon_tag: (name, opt = {}) ->
\r
94 title = if opt['object']
\r
95 name + ' ID:' + opt['object'].get(id).toString()
\r
98 size = if opt['size']
\r
99 parseInt(opt['size'])
\r
102 "<img src=\"" + '/images/' + Pettanr.underscore(name) + '.gif' + "\" width=" + size.toString() + " height=" + size.toString() + " alt=\"" + name + ' Icon' + "title=\"" + title + ">"
\r
104 @distance_of_time_in_words_to_now: (datetime) ->
\r
107 class Pettanr.CounterModel extends Backbone.Model
\r
108 initialize: (attr, options) ->
\r
109 @url = options['url']
\r
116 @decode: (params) ->
\r
118 @encode: (params) ->
\r
120 class Pettanr.Pager
\r
122 initialize: (@counter) ->
\r
123 @counter.fetch().done ->
\r
125 @current_page = @list_result.page
\r
126 @per_page = @list_result.page_size
\r
127 @total_page = (@total / @per_page) + 1
\r
130 @by_params: (params) ->
\r
132 @page = params['page']
\r
133 @page_size = params['page_size']
\r
134 @counter = new Pettanr.CounterModel({}, {url: '/' + params['controller'] + i + 'count_' + a})
\r
135 new Pettanr.Pager()
\r
137 class Pettanr.FilerCollection extends Backbone.Collection
\r
139 initialize: (models, options) ->
\r
140 @model = options['model']
\r
141 params = options['params']
\r
142 @counter = options['counter']
\r
144 i = if params['id']
\r
148 a = if params['action'] != 'index'
\r
153 q.push('page=' + Pettanr.to_s(@page)) if @page
\r
154 q.push('page_size=' + Pettanr.to_s(@page_size)) if @page_size
\r
155 qry = if q.length > 0
\r
159 @url = '/' + params['controller'] + i + a + qry
\r
160 @pager = new Pettanr.Pager(@counter)
\r
162 class Pettanr.AppController
\r
167 @my_controller = Manifest.manifest().controllers[@params['controller']]
\r
168 @my_action = @my_controller.actions[@params['action']]
\r
169 @my_model = Manifest.manifest().models[@my_controller.item_name]
\r
170 @my_model_class = @my_model.classify()
\r
174 @my_list_model = Manifest.manifest().models[@my_action.item_name]
\r
175 @my_list_model_class = @my_list_model.classify()
\r
176 @counter = new Pettanr.CounterModel({}, {url: @my_action.url(i)})
\r
177 @list = new Pettanr.FilerCollection({}, {model: @my_list_model_class, counter: @counter, params: @params})
\r
181 f = new Locmare.Filer({
\r
183 item_name: @my_list_model_class.item_name(),
\r
185 pager: @list.pager,
\r
186 operators: window.operators
\r
189 class Pettanr.FilerRender
\r
190 constructor: (item_name, list_result, pager_type, operators) ->
\r
191 @item_name = item_name
\r
192 @list_result = list_result
\r
193 @pager_type = pager_type
\r
194 @operators = operators
\r
196 @list_result.fetch({}).done () ->
\r
200 items = @list_result.models
\r
201 f = new Locmare.Filer({
\r
202 item_name: @item_name,
\r
204 list_result: @list_result,
\r
205 pager_type: @pager_type,
\r
206 operators: @operators
\r
208 $("#pettanr").html(f.render().el)
\r
210 #>> https://gist.github.com/davidjbeveridge/3813724
\r
211 @xeach: (arr, func, index=0) ->
\r
212 if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []
\r
214 @camelize: (input) ->
\r
215 pieces = input.split(/[\W_-]/)
\r
216 @xeach(pieces, @capitalize).join("")
\r
218 @capitalize: (input) ->
\r
219 input.charAt(0).toUpperCase() + input.slice(1)
\r
221 @lowercase: (input) ->
\r
222 input.toLowerCase()
\r
224 @underscore: (input) ->
\r
225 pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n
\r
226 @xeach(pieces, @lowercase ).join("_")
\r