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 '' @truncate: (str, length = null) -> s = Pettanr.to_s(str) if length and s.length > 3 # 3 = '...' r = s.substr(0, length - 4) r = r.concat( '...') if s.length > length - 3 r else Pettanr.to_s(str) @format_date: (date) -> y = ('000' + (date.getFullYear())).slice(-4) m = ('0' + (date.getMonth() + 1)).slice(-2) d = ('0' + date.getDate()).slice(-2) h = ('0' + date.getHours()).slice(-2) n = ('0' + date.getMinutes()).slice(-2) s = ('0' + date.getSeconds()).slice(-2) y + '/' + m + '/' + d + ' ' + h + ':' + n + ':' + s @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[0]].item_name(), l[1]) @t_a: (item_name, attr_name) -> I18n.t('activerecord.attributes.' + item_name + '.' + attr_name) @t_select_items: (items) -> _.map items, (i) -> [I18n.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) -> if Pettanr.is_blank(datetime) '' else $.timeago(datetime) @cache = {} @credits = {} class Pettanr.AppController constructor: () -> @params = {} @operators = window.operators _.extend(this, Backbone.Events) 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 = Locmare.ListGroup.list( @my_action.path_name(), @my_action.name, @operators, @params ) set_show: () -> @set_model() @item = new @my_model_class({id: @params['id']}) filer_list: () -> @set_list() @list.open(() => pager = Locmare.ListGroupModule.LibModule.Pager.factory(@list.page_status, @params) filer = new Locmare.Filer({ item_name: @list.item_name, items: @list.items(), pager: pager, operators: @operators }) @trigger('ready', filer) ) false set_play: () -> @set_list() @binder_action = @my_action.original @binder_controller = @binder_action.controller() @binder_model = Manifest.manifest().models[@binder_action.item_name] @binder_model_class = @binder_model.classify() @item = new @binder_model_class({id: @params['id']}) play_list: () -> @list.open(() => console.log @list.items() ) false show_prof: (el) -> @item.fetch({cache: true}).done => @item.boosts 'post' profiler = new Locmare.Profiler({ item_name: @item.item_name(), item: @item, operators: @operators }) el ||= $("#pettanr") el.html(profiler.render().el) @trigger('retitle', @params) set_new: () -> @set_model() @item = new @my_model_class({id: @params['id']}) @item.boosts 'post' # Backbone.Model has defaults property #@item.supply_default() set_edit: () -> @set_model() @item = new @my_model_class({id: @params['id']}) @item.fetch({cache: true}).done => @item.boosts 'post' @render_form() render_form: () -> @form = Locmare.Form.factory({ form_name: @item.item_name(), item: @item, mounted: true, submit: 'default', operators: @operators, action: '/' + @item.table_name() + '/' + Pettanr.to_s(@item.get('id')) }) $("#pettanr").html(@form.render().el) form_new: () -> @set_new() @render_form() form_edit: () -> @set_edit() class Pettanr.AppBody extends Backbone.View el: "#pettanr" initialize: (options) -> super(options) render: () -> this.$el.html('') this refresh: (body) -> @listenTo(body, 'navigate', @navigate) this.$el.html(body.el) fire: (params) -> c = Pettanr[Pettanr.camelize(params['controller']) + 'Controller'] controller = new c controller.params = params @listenTo(controller, 'ready', @refresh) @listenTo(controller, 'retitle', @retitle) controller[params['action']]() navigate: (url) -> window.router.navigate(url, true) retitle: (params, str = null) -> t = str || I18n.t(params['controller'] + '.' + params['action'] + '.title') site_caption = _.escape(Manifest.manifest().magic_numbers.profile.users.caption) $(document).attr('title', t + ' - ' + site_caption) class Pettanr.Dialog extends Backbone.View initialize: (options) -> super(options) render: () -> this.$el.html('') this refresh: (body) -> @listenTo(body, 'navigate', @navigate) this.$el.html(body.render().el) fire: (params) -> c = Pettanr[Pettanr.camelize(params['controller']) + 'Controller'] controller = new c controller.params = params @listenTo(controller, 'ready', @refresh) # ignore retitle message # @listenTo(controller, 'retitle', @retitle) controller[params['action']]() local_navigate: (url) -> window.router.local_navigate(url, true) #>> 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