OSDN Git Service

mrg
[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   @to_style: (hash) ->\r
42     a = _.map hash, (n, k) ->\r
43       Pettanr.to_s(k) + ': ' + Pettanr.to_s(n)\r
44     a.join('; ')\r
45   \r
46   @url: (controller_name, action_name, params) ->\r
47     controller = Manifest.manifest().controllers[controller_name]\r
48     action = controller.actions[action_name]\r
49     action.url(params)\r
50   \r
51   class Pettanr.AppHelper\r
52     @manifest: () ->\r
53       Manifest.manifest\r
54     \r
55     @full_url: (filename) ->\r
56       request.protocol + request.host_with_port + filename\r
57     \r
58     @t_m: (label) ->\r
59       l = label.split('.')\r
60       if l.length > 2\r
61         label\r
62       else\r
63         if l.length == 1\r
64           I18n.t('activerecord.models.' + Pettanr[label].item_name())\r
65         else\r
66           Pettanr.AppHelper.t_a(Pettanr[l.first].item_name(), l.last)\r
67     \r
68     @t_a: (item_name, attr_name) ->\r
69       I18n.t('activerecord.attributes.' + item_name + '.' + attr_name)\r
70       \r
71     @t_select_items: (items) ->\r
72       _.map items, (i) ->\r
73         [I18n.t(i[0]), i[1]]\r
74     \r
75     @t_selected_item: (name, index) ->\r
76       I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])\r
77     \r
78     @distance_of_time_in_words_to_now: (datetime) ->\r
79       datetime\r
80   \r
81   class Pettanr.CounterModel extends Backbone.Model\r
82     initialize: (attr, options) ->\r
83       @url = options['url']\r
84     \r
85     count: () ->\r
86       @get('count')\r
87     \r
88   class Pettanr.PageStatus\r
89     \r
90     constructor: (options) ->\r
91       @action = options['action']\r
92       @params = options['params']\r
93       @current_page = parseInt(@params['page']) || 1\r
94       @per_page = @params['page_size'] || 25\r
95       @window_size = 3\r
96       @counter = new Pettanr.CounterModel({}, {url: '/' + @action.counter_url(@params)})\r
97   \r
98   class Pettanr.FilerCollection extends Backbone.Collection\r
99     \r
100     initialize: (models, options) ->\r
101       @model = options['model']\r
102       @action = options['action']\r
103       @params = options['params']\r
104       \r
105       @url = '/' + @action.url(@params)\r
106   \r
107   class Pettanr.AppController\r
108     constructor: () ->\r
109       @params = {}\r
110       @operators = window.operators\r
111       \r
112     set_model: () ->\r
113       @my_controller = Manifest.manifest().controllers[@params['controller']]\r
114       @my_action = @my_controller.actions[@params['action']]\r
115       @my_model = Manifest.manifest().models[@my_action.item_name]\r
116       @my_model_class = @my_model.classify()\r
117       \r
118     set_list: () ->\r
119       @set_model()\r
120       @my_list_model = Manifest.manifest().models[@my_action.item_name]\r
121       @my_list_model_class = @my_list_model.classify()\r
122       @list_options = {\r
123         model: @my_list_model_class, \r
124         action: @my_action, \r
125         params: @params\r
126       }\r
127       @page_status = new Pettanr.PageStatus(@list_options)\r
128       @list = new Pettanr.FilerCollection({}, @list_options)\r
129     \r
130     set_show: () ->\r
131       @set_model()\r
132       @item = new @my_model_class({id: @params['id']})\r
133     \r
134     filer_list: () ->\r
135       @set_list()\r
136       @pager = new Locmare.FilerModule.DefaultPager({page_status: @page_status})\r
137       f = new Locmare.Filer({\r
138         el: "#pettanr",\r
139         item_name: @my_list_model_class.item_name(), \r
140         collection: @list, \r
141         pager: @pager, \r
142         operators: @operators\r
143       })\r
144     \r
145     show_prof: () ->\r
146       profiler = new Locmare.Profiler({\r
147         el: "#pettanr",\r
148         item_name: @item.item_name(), \r
149         item: @item, \r
150         operators: @operators\r
151       })\r
152     \r
153     set_new: () ->\r
154       @set_model()\r
155       @item = new @my_model_class({id: @params['id']})\r
156       @item.boosts 'post'\r
157       @item.supply_default()\r
158     \r
159     set_edit: () ->\r
160       @set_model()\r
161       @item = new @my_model_class({id: @params['id']})\r
162       _this = this\r
163       @item.fetch().done ->\r
164         _this.item.boosts 'post'\r
165         _this.render_form()\r
166     \r
167     render_form: () ->\r
168       @form = new Locmare.Form({\r
169         form_name: @item.item_name(), \r
170         item: @item, \r
171         mounted: true, \r
172         submit: 'default', \r
173         operators: @operators,\r
174         action: '/' + @item.table_name() + '/' + Pettanr.to_s(@item.get('id'))\r
175       })\r
176       $("#pettanr").html(@form.render().el)\r
177     \r
178     form_new: () ->\r
179       @set_new()\r
180       @render_form()\r
181     \r
182     form_edit: () ->\r
183       @set_edit()\r
184     \r
185   class Pettanr.FilerRender\r
186     constructor: (item_name, list_result, pager_type, operators) ->\r
187       @item_name = item_name\r
188       @list_result = list_result\r
189       @pager_type = pager_type\r
190       @operators = operators\r
191       _this = this\r
192       @list_result.fetch({}).done () ->\r
193         _this.render()\r
194     \r
195     render: () ->\r
196       items = @list_result.models\r
197       f = new Locmare.Filer({\r
198         item_name: @item_name, \r
199         items: items, \r
200         list_result: @list_result, \r
201         pager_type: @pager_type, \r
202         operators: @operators\r
203       })\r
204       $("#pettanr").html(f.render().el)\r
205 \r
206   #>> https://gist.github.com/davidjbeveridge/3813724\r
207   @xeach: (arr, func, index=0) ->\r
208     if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []\r
209   \r
210   @camelize: (input) ->\r
211     pieces = input.split(/[\W_-]/)\r
212     @xeach(pieces, @capitalize).join("")\r
213   \r
214   @capitalize: (input) ->\r
215     input.charAt(0).toUpperCase() + input.slice(1)\r
216   \r
217   @lowercase: (input) ->\r
218     input.toLowerCase()\r
219    \r
220   @underscore: (input) ->\r
221     pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n\r
222     @xeach(pieces, @lowercase ).join("_")\r
223   # <<\r
224 @Pettanr = Pettanr\r