class Pettanr # Foo.class -> Pettanr[@constructor.name] @is_blank: (str) -> !str?.trim() @is_a_string: (str) -> if (str instanceof String) or (typeof str == "string") true else false @is_number: (str) -> if typeof(str) != 'number' && typeof(str) != 'string' return false else n = if typeof(str) == 'string' parseInt(str) else 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 '' @to_style: (hash) -> a = _.map hash, (n, k) -> Pettanr.to_s(k) + ': ' + Pettanr.to_s(n) a.join('; ') @url: (controller_name, action_name, params) -> controller = Manifest.manifest().controllers[controller_name] action = controller.actions[action_name] action.url(params) class Pettanr.AppHelper @manifest: () -> Manifest.manifest @full_url: (filename) -> request.protocol + request.host_with_port + filename @t_m: (label) -> l = label.split('.') if l.length > 2 label else if l.length == 1 I18n.t('activerecord.models.' + Pettanr[label].item_name()) else Pettanr.AppHelper.t_a(Pettanr[l.first].item_name(), l.last) @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]] @t_selected_item: (name, index) -> I18n.t(Manifest.manifest().system_resources.select_items[name][index][0]) @distance_of_time_in_words_to_now: (datetime) -> datetime class Pettanr.CounterModel extends Backbone.Model initialize: (attr, options) -> @url = options['url'] count: () -> @get('count') class Pettanr.PageStatus constructor: (options) -> @action = options['action'] @params = options['params'] @current_page = parseInt(@params['page']) || 1 @per_page = @params['page_size'] || 25 @window_size = 3 @counter = new Pettanr.CounterModel({}, {url: '/' + @action.counter_url(@params)}) class Pettanr.FilerCollection extends Backbone.Collection initialize: (models, options) -> @model = options['model'] @action = options['action'] @params = options['params'] @url = '/' + @action.url(@params) class Pettanr.AppController constructor: () -> @params = {} @operators = window.operators set_model: () -> @my_controller = Manifest.manifest().controllers[@params['controller']] @my_action = @my_controller.actions[@params['action']] @my_model = Manifest.manifest().models[@my_action.item_name] @my_model_class = @my_model.classify() set_list: () -> @set_model() @my_list_model = Manifest.manifest().models[@my_action.item_name] @my_list_model_class = @my_list_model.classify() @list_options = { model: @my_list_model_class, action: @my_action, params: @params } @page_status = new Pettanr.PageStatus(@list_options) @list = new Pettanr.FilerCollection({}, @list_options) set_show: () -> @set_model() @item = new @my_model_class({id: @params['id']}) filer_list: () -> @set_list() @pager = new Locmare.FilerModule.DefaultPager({page_status: @page_status}) f = new Locmare.Filer({ el: "#pettanr", item_name: @my_list_model_class.item_name(), collection: @list, pager: @pager, operators: @operators }) show_prof: () -> profiler = new Locmare.Profiler({ el: "#pettanr", item_name: @item.item_name(), item: @item, operators: @operators }) 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() 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) #>> 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 [] @camelize: (input) -> pieces = input.split(/[\W_-]/) @xeach(pieces, @capitalize).join("") @capitalize: (input) -> input.charAt(0).toUpperCase() + input.slice(1) @lowercase: (input) -> input.toLowerCase() @underscore: (input) -> pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n @xeach(pieces, @lowercase ).join("_") # << @Pettanr = Pettanr