OSDN Git Service

fix js
[pettanr/pettanr.git] / app / assets / javascripts / pettanr.js.coffee
1 class Pettanr\r
2   # Foo.class -> Pettanr[@constructor.name]\r
3 \r
4   @is_blank: (str) ->\r
5     !str?.trim()\r
6   \r
7   @is_a_string: (str) ->\r
8     if (str instanceof String) or (typeof str == "string")\r
9       true\r
10     else\r
11       false\r
12   \r
13   @is_number: (str) ->\r
14     if typeof(str) != 'number' && typeof(str) != 'string'\r
15       return false\r
16     else\r
17       n = if typeof(str) == 'string'\r
18         parseInt(str)\r
19       else\r
20         str\r
21       isFinite(n)\r
22   \r
23   @to_i: (str) ->\r
24     if @is_a_string(str)\r
25       parseInt(str)\r
26     else\r
27       if @is_number(str)\r
28         Math.floor(str)\r
29       else\r
30         0\r
31   \r
32   @to_s: (str) ->\r
33     if @is_a_string(str)\r
34       str\r
35     else\r
36       if @is_number(str)\r
37         str.toString()\r
38       else\r
39         ''\r
40   \r
41   @url: (controller_name, action_name, params) ->\r
42     controller = Manifest.manifest().controllers[controller_name]\r
43     action = controller.actions[action_name]\r
44     action.url(params)\r
45   \r
46   class Pettanr.AppHelper\r
47     @manifest: () ->\r
48       Manifest.manifest\r
49     \r
50     @full_url: (filename) ->\r
51       request.protocol + request.host_with_port + filename\r
52     \r
53     @t_m: (label) ->\r
54       l = label.split('.')\r
55       if l.length > 2\r
56         label\r
57       else\r
58         if l.length == 1\r
59           I18n.t('activerecord.models.' + Pettanr[label].item_name())\r
60         else\r
61           Pettanr.AppHelper.t_a(Pettanr[l.first].item_name(), l.last)\r
62     \r
63     @t_a: (item_name, attr_name) ->\r
64       I18n.t('activerecord.attributes.' + item_name + '.' + attr_name)\r
65       \r
66     @t_select_items: (items) ->\r
67       _.map items, (i) ->\r
68         [t(i[0]), i[1]]\r
69     \r
70     @t_selected_item: (name, index) ->\r
71       I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])\r
72     \r
73     @distance_of_time_in_words_to_now: (datetime) ->\r
74       datetime\r
75   \r
76   class Pettanr.CounterModel extends Backbone.Model\r
77     initialize: (attr, options) ->\r
78       @url = options['url']\r
79     \r
80     count: () ->\r
81       @get('count')\r
82     \r
83   class Pettanr.PageStatus\r
84     \r
85     constructor: (options) ->\r
86       @action = options['action']\r
87       @params = options['params']\r
88       @current_page = @params['page'] || 1\r
89       @per_page = @params['page_size'] || 25\r
90       @window_size = 3\r
91       @counter = new Pettanr.CounterModel({}, {url: '/' + @action.counter_url(@params)})\r
92   \r
93   class Pettanr.FilerCollection extends Backbone.Collection\r
94     \r
95     initialize: (models, options) ->\r
96       @model = options['model']\r
97       @action = options['action']\r
98       @params = options['params']\r
99       \r
100       @url = '/' + @action.url(@params)\r
101   \r
102   class Pettanr.AppController\r
103     constructor: () ->\r
104       @params = {}\r
105       @operators = window.operators\r
106       \r
107     set_model: () ->\r
108       @my_controller = Manifest.manifest().controllers[@params['controller']]\r
109       @my_action = @my_controller.actions[@params['action']]\r
110       @my_model = Manifest.manifest().models[@my_action.item_name]\r
111       @my_model_class = @my_model.classify()\r
112       \r
113     set_list: () ->\r
114       @set_model()\r
115       @my_list_model = Manifest.manifest().models[@my_action.item_name]\r
116       @my_list_model_class = @my_list_model.classify()\r
117       @list_options = {\r
118         model: @my_list_model_class, \r
119         action: @my_action, \r
120         params: @params\r
121       }\r
122       @page_status = new Pettanr.PageStatus(@list_options)\r
123       @list = new Pettanr.FilerCollection({}, @list_options)\r
124     \r
125     set_show: () ->\r
126       @set_model()\r
127       @item = new @my_model_class({id: @params['id']})\r
128     \r
129     filer_list: () ->\r
130       @set_list()\r
131       @pager = new Locmare.FilerModule.DefaultPager({page_status: @page_status})\r
132       f = new Locmare.Filer({\r
133         el: "#pettanr",\r
134         item_name: @my_list_model_class.item_name(), \r
135         collection: @list, \r
136         pager: @pager, \r
137         operators: @operators\r
138       })\r
139     \r
140     show_prof: () ->\r
141       profiler = new Locmare.Profiler({\r
142         el: "#pettanr",\r
143         item_name: @item.item_name(), \r
144         item: @item, \r
145         operators: @operators\r
146       })\r
147   \r
148   class Pettanr.FilerRender\r
149     constructor: (item_name, list_result, pager_type, operators) ->\r
150       @item_name = item_name\r
151       @list_result = list_result\r
152       @pager_type = pager_type\r
153       @operators = operators\r
154       _this = this\r
155       @list_result.fetch({}).done () ->\r
156         _this.render()\r
157     \r
158     render: () ->\r
159       items = @list_result.models\r
160       f = new Locmare.Filer({\r
161         item_name: @item_name, \r
162         items: items, \r
163         list_result: @list_result, \r
164         pager_type: @pager_type, \r
165         operators: @operators\r
166       })\r
167       $("#pettanr").html(f.render().el)\r
168 \r
169   #>> https://gist.github.com/davidjbeveridge/3813724\r
170   @xeach: (arr, func, index=0) ->\r
171     if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []\r
172   \r
173   @camelize: (input) ->\r
174     pieces = input.split(/[\W_-]/)\r
175     @xeach(pieces, @capitalize).join("")\r
176   \r
177   @capitalize: (input) ->\r
178     input.charAt(0).toUpperCase() + input.slice(1)\r
179   \r
180   @lowercase: (input) ->\r
181     input.toLowerCase()\r
182    \r
183   @underscore: (input) ->\r
184     pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n\r
185     @xeach(pieces, @lowercase ).join("_")\r
186   # <<\r
187 @Pettanr = Pettanr\r