OSDN Git Service

fix js
[pettanr/pettanr.git] / app / assets / javascripts / pettanr.js.coffee
index 3704d7e..e644594 100644 (file)
@@ -1,4 +1,48 @@
 class Pettanr\r
+  # Foo.class -> Pettanr[@constructor.name]\r
+\r
+  @is_blank: (str) ->\r
+    !str?.trim()\r
+  \r
+  @is_a_string: (str) ->\r
+    if (str instanceof String) or (typeof str == "string")\r
+      true\r
+    else\r
+      false\r
+  \r
+  @is_number: (str) ->\r
+    if typeof(str) != 'number' && typeof(str) != 'string'\r
+      return false\r
+    else\r
+      n = if typeof(str) == 'string'\r
+        parseInt(str)\r
+      else\r
+        str\r
+      isFinite(n)\r
+  \r
+  @to_i: (str) ->\r
+    if @is_a_string(str)\r
+      parseInt(str)\r
+    else\r
+      if @is_number(str)\r
+        Math.floor(str)\r
+      else\r
+        0\r
+  \r
+  @to_s: (str) ->\r
+    if @is_a_string(str)\r
+      str\r
+    else\r
+      if @is_number(str)\r
+        str.toString()\r
+      else\r
+        ''\r
+  \r
+  @url: (controller_name, action_name, params) ->\r
+    controller = Manifest.manifest().controllers[controller_name]\r
+    action = controller.actions[action_name]\r
+    action.url(params)\r
+  \r
   class Pettanr.AppHelper\r
     @manifest: () ->\r
       Manifest.manifest\r
@@ -14,8 +58,11 @@ class Pettanr
         if l.length == 1\r
           I18n.t('activerecord.models.' + Pettanr[label].item_name())\r
         else\r
-          I18n.t('activerecord.models.attribtes.' + Pettanr[l.first].item_name() + '.' + l.last)\r
+          Pettanr.AppHelper.t_a(Pettanr[l.first].item_name(), l.last)\r
     \r
+    @t_a: (item_name, attr_name) ->\r
+      I18n.t('activerecord.attributes.' + item_name + '.' + attr_name)\r
+      \r
     @t_select_items: (items) ->\r
       _.map items, (i) ->\r
         [t(i[0]), i[1]]\r
@@ -23,18 +70,101 @@ class Pettanr
     @t_selected_item: (name, index) ->\r
       I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])\r
     \r
-    @icon_tag: (name, opt = {}) ->\r
-      title = if opt['object']\r
-        name + ' ID:' + opt['object'].get(id).toString()\r
-      else\r
-        name\r
-      size = if opt['size']\r
-        parseInt(opt['size'])\r
-      else\r
-        35\r
-      "<img src=\"" + '/images/' + Pettanr.underscore(name) + '.gif' + "\" width=" + size.toString() + " height=" + size.toString() + " alt=\"" + name + ' Icon' + "title=\"" + title + ">"\r
-\r
+    @distance_of_time_in_words_to_now: (datetime) ->\r
+      datetime\r
+  \r
+  class Pettanr.CounterModel extends Backbone.Model\r
+    initialize: (attr, options) ->\r
+      @url = options['url']\r
+    \r
+    count: () ->\r
+      @get('count')\r
+    \r
+  class Pettanr.PageStatus\r
+    \r
+    constructor: (options) ->\r
+      @action = options['action']\r
+      @params = options['params']\r
+      @current_page = @params['page'] || 1\r
+      @per_page = @params['page_size'] || 25\r
+      @window_size = 3\r
+      @counter = new Pettanr.CounterModel({}, {url: '/' + @action.counter_url(@params)})\r
+  \r
+  class Pettanr.FilerCollection extends Backbone.Collection\r
+    \r
+    initialize: (models, options) ->\r
+      @model = options['model']\r
+      @action = options['action']\r
+      @params = options['params']\r
+      \r
+      @url = '/' + @action.url(@params)\r
+  \r
   class Pettanr.AppController\r
+    constructor: () ->\r
+      @params = {}\r
+      @operators = window.operators\r
+      \r
+    set_model: () ->\r
+      @my_controller = Manifest.manifest().controllers[@params['controller']]\r
+      @my_action = @my_controller.actions[@params['action']]\r
+      @my_model = Manifest.manifest().models[@my_action.item_name]\r
+      @my_model_class = @my_model.classify()\r
+      \r
+    set_list: () ->\r
+      @set_model()\r
+      @my_list_model = Manifest.manifest().models[@my_action.item_name]\r
+      @my_list_model_class = @my_list_model.classify()\r
+      @list_options = {\r
+        model: @my_list_model_class, \r
+        action: @my_action, \r
+        params: @params\r
+      }\r
+      @page_status = new Pettanr.PageStatus(@list_options)\r
+      @list = new Pettanr.FilerCollection({}, @list_options)\r
+    \r
+    set_show: () ->\r
+      @set_model()\r
+      @item = new @my_model_class({id: @params['id']})\r
+    \r
+    filer_list: () ->\r
+      @set_list()\r
+      @pager = new Locmare.FilerModule.DefaultPager({page_status: @page_status})\r
+      f = new Locmare.Filer({\r
+        el: "#pettanr",\r
+        item_name: @my_list_model_class.item_name(), \r
+        collection: @list, \r
+        pager: @pager, \r
+        operators: @operators\r
+      })\r
+    \r
+    show_prof: () ->\r
+      profiler = new Locmare.Profiler({\r
+        el: "#pettanr",\r
+        item_name: @item.item_name(), \r
+        item: @item, \r
+        operators: @operators\r
+      })\r
+  \r
+  class Pettanr.FilerRender\r
+    constructor: (item_name, list_result, pager_type, operators) ->\r
+      @item_name = item_name\r
+      @list_result = list_result\r
+      @pager_type = pager_type\r
+      @operators = operators\r
+      _this = this\r
+      @list_result.fetch({}).done () ->\r
+        _this.render()\r
+    \r
+    render: () ->\r
+      items = @list_result.models\r
+      f = new Locmare.Filer({\r
+        item_name: @item_name, \r
+        items: items, \r
+        list_result: @list_result, \r
+        pager_type: @pager_type, \r
+        operators: @operators\r
+      })\r
+      $("#pettanr").html(f.render().el)\r
 \r
   #>> https://gist.github.com/davidjbeveridge/3813724\r
   @xeach: (arr, func, index=0) ->\r
@@ -54,10 +184,4 @@ class Pettanr
     pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n\r
     @xeach(pieces, @lowercase ).join("_")\r
   # <<\r
-  @is_a_string: (str) ->\r
-    if (str instanceof String) or (typeof str == "string")\r
-      true\r
-    else\r
-      false\r
-  \r
 @Pettanr = Pettanr\r