OSDN Git Service

add profiler
[pettanr/pettanr.git] / app / assets / javascripts / pettanr.js.coffee
index 67c3c27..c5ed2df 100644 (file)
@@ -1,7 +1,31 @@
 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_s: (str) ->\r
+    if @is_a_string(str)\r
+      str\r
+    else\r
+      ''\r
+  \r
   class Pettanr.AppHelper\r
     @manifest: () ->\r
       Manifest.manifest\r
@@ -17,8 +41,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
@@ -81,8 +108,63 @@ class Pettanr
     \r
     @distance_of_time_in_words_to_now: (datetime) ->\r
       datetime\r
-\r
+  \r
+  class Pettanr.CounterModel extends Backbone.Model\r
+    initialize: (attr, options) ->\r
+      @url = options['url']\r
+    \r
+  class Pettanr.FilerCollection extends Backbone.Collection\r
+    \r
+    initialize: (models, options) ->\r
+      params = options['params']\r
+      i = if params['id']\r
+        params['id'] + '/'\r
+      else\r
+        ''\r
+      a = if params['action'] != 'index'\r
+        params['action']\r
+      else\r
+        ''\r
+      \r
+      @page = params['page']\r
+      @page_size = params['page_size']\r
+      q = []\r
+      q.push('page=' + Pettanr.to_s(@page)) if @page\r
+      q.push('page_size=' + Pettanr.to_s(@page_size)) if @page_size\r
+      qry = if q.length > 0\r
+        '?' + q.join('&')\r
+      else\r
+        ''\r
+      @url = '/' + params['controller'] + '/' + i + a + qry\r
+      @total = new Pettanr.CounterModel({}, {url: '/' + params['controller'] + '/' + i + 'count_' + a})\r
+      @model = options['model']\r
+  \r
   class Pettanr.AppController\r
+    constructor: () ->\r
+      @params = {}\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_controller.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 = new Pettanr.FilerCollection({}, {model: @my_list_model_class, params: @params})\r
+    \r
+    filer_list: () ->\r
+      @set_list()\r
+      f = new Locmare.Filer({\r
+        el: "#pettanr",\r
+        item_name: @my_list_model_class.item_name(), \r
+        items: [], \r
+        list_result: @list, \r
+        pager_type: 'default', \r
+        operators: window.operators\r
+      })\r
   \r
   class Pettanr.FilerRender\r
     constructor: (item_name, list_result, pager_type, operators) ->\r
@@ -123,16 +205,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
-  @to_s: (str) ->\r
-    if @is_a_string(str)\r
-      str\r
-    else\r
-      ''\r
-  \r
 @Pettanr = Pettanr\r