OSDN Git Service

fix element opacity
[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   @truncate: (str, length = null) ->\r
42     s = Pettanr.to_s(str)\r
43     if length and s.length > 3    # 3 = '...'\r
44       r = s.substr(0, length - 4)\r
45       r = r.concat( '...') if s.length > length - 3\r
46       r\r
47     else\r
48       Pettanr.to_s(str)\r
49   \r
50   @format_date: (date) ->\r
51     y = ('000' + (date.getFullYear())).slice(-4)\r
52     m = ('0' + (date.getMonth() + 1)).slice(-2)\r
53     d = ('0' + date.getDate()).slice(-2)\r
54     h = ('0' + date.getHours()).slice(-2)\r
55     n = ('0' + date.getMinutes()).slice(-2)\r
56     s = ('0' + date.getSeconds()).slice(-2)\r
57     y + '/' + m + '/' + d + ' ' + h + ':' + n + ':' + s\r
58   \r
59   @to_style: (hash) ->\r
60     a = _.map hash, (n, k) ->\r
61       Pettanr.to_s(k) + ': ' + Pettanr.to_s(n)\r
62     a.join('; ')\r
63   \r
64   @url: (controller_name, action_name, params) ->\r
65     controller = Manifest.manifest().controllers[controller_name]\r
66     action = controller.actions[action_name]\r
67     action.url(params)\r
68   \r
69   class Pettanr.AppHelper\r
70     @manifest: () ->\r
71       Manifest.manifest\r
72     \r
73     @full_url: (filename) ->\r
74       request.protocol + request.host_with_port + filename\r
75     \r
76     @t_m: (label) ->\r
77       l = label.split('.')\r
78       if l.length > 2\r
79         label\r
80       else\r
81         if l.length == 1\r
82           I18n.t('activerecord.models.' + Pettanr[label].item_name())\r
83         else\r
84           Pettanr.AppHelper.t_a(Pettanr[l[0]].item_name(), l[1])\r
85     \r
86     @t_a: (item_name, attr_name) ->\r
87       I18n.t('activerecord.attributes.' + item_name + '.' + attr_name)\r
88       \r
89     @t_select_items: (items) ->\r
90       _.map items, (i) ->\r
91         [I18n.t(i[0]), i[1]]\r
92     \r
93     @t_selected_item: (name, index) ->\r
94       I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])\r
95     \r
96     @distance_of_time_in_words_to_now: (datetime) ->\r
97       datetime\r
98   \r
99   @cache = {}\r
100   @credits = {}\r
101   \r
102   class Pettanr.FilerCollection extends Backbone.Collection\r
103     \r
104     initialize: (models, options) ->\r
105       @model = options['model']\r
106       @action = options['action']\r
107       @params = options['params']\r
108       \r
109       @url = '/' + @action.url(@params)\r
110   \r
111   class Pettanr.AppController\r
112     constructor: () ->\r
113       @params = {}\r
114       @operators = window.operators\r
115       \r
116     set_model: () ->\r
117       @my_controller = Manifest.manifest().controllers[@params['controller']]\r
118       @my_action = @my_controller.actions[@params['action']]\r
119       @my_model = Manifest.manifest().models[@my_action.item_name]\r
120       @my_model_class = @my_model.classify()\r
121       \r
122     set_list: () ->\r
123       @set_model()\r
124       @my_list_model = Manifest.manifest().models[@my_action.item_name]\r
125       @my_list_model_class = @my_list_model.classify()\r
126       @list = Locmare.ListGroup.list(@my_action.item_name, @my_action.list_name)\r
127     \r
128     set_show: () ->\r
129       @set_model()\r
130       @item = new @my_model_class({id: @params['id']})\r
131     \r
132     filer_list: () ->\r
133       @set_list()\r
134       _this = this\r
135       @list.open(@operators, @params, @my_action, {}, (page_status) ->\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: _this.my_list_model_class.item_name(), \r
140           items: _this.list.models, \r
141           pager: pager, \r
142           operators: _this.operators\r
143         })\r
144       )\r
145       false\r
146     \r
147     set_play: () ->\r
148       @set_list()\r
149       @binder_controller = Manifest.manifest().controllers[@params['controller']]\r
150       @binder_model = Manifest.manifest().models[@binder_controller.item_name]\r
151       @binder_model_class = @binder_model.classify()\r
152       @item = new @binder_model_class({id: @params['id']})\r
153     \r
154     play_list: () ->\r
155       _this = this\r
156       opt = {\r
157         id: @params['id'], \r
158         offset: @params['offset'], \r
159         count: @params['count'], \r
160         page: @params['page'], \r
161         page_size: @params['page_size']\r
162       }\r
163       @item.fetch({cache: true}).done ->\r
164         _this.list.open(_this.operators, opt, _this.my_action, {}, (page_status) ->\r
165           console.log _this.list.models\r
166         )\r
167         false\r
168     \r
169     show_prof: () ->\r
170       _this = this\r
171       @item.fetch({cache: true}).done ->\r
172         _this.item.boosts 'post'\r
173         profiler = new Locmare.Profiler({\r
174           item_name: _this.item.item_name(), \r
175           item: _this.item, \r
176           operators: _this.operators\r
177         })\r
178         $("#pettanr").html(profiler.render().el)\r
179         _this.redraw_title(_this.params)\r
180     \r
181     set_new: () ->\r
182       @set_model()\r
183       @item = new @my_model_class({id: @params['id']})\r
184       @item.boosts 'post'\r
185       # Backbone.Model has defaults property\r
186       #@item.supply_default()\r
187     \r
188     set_edit: () ->\r
189       @set_model()\r
190       @item = new @my_model_class({id: @params['id']})\r
191       _this = this\r
192       @item.fetch({cache: true}).done ->\r
193         _this.item.boosts 'post'\r
194         _this.render_form()\r
195     \r
196     render_form: () ->\r
197       @form = new Locmare.Form({\r
198         form_name: @item.item_name(), \r
199         item: @item, \r
200         mounted: true, \r
201         submit: 'default', \r
202         operators: @operators,\r
203         action: '/' + @item.table_name() + '/' + Pettanr.to_s(@item.get('id'))\r
204       })\r
205       $("#pettanr").html(@form.render().el)\r
206     \r
207     form_new: () ->\r
208       @set_new()\r
209       @render_form()\r
210     \r
211     form_edit: () ->\r
212       @set_edit()\r
213     \r
214     redraw_title: (params, str = null) ->\r
215       t = str || I18n.t(params['controller'] + '.' + params['action'] + '.title')\r
216       site_caption = Manifest.manifest().magic_numbers.profile.users.caption\r
217       $(document).attr('title', t + ' - ' + site_caption)\r
218     \r
219   class Pettanr.FilerRender\r
220     constructor: (item_name, list_result, pager_type, operators) ->\r
221       @item_name = item_name\r
222       @list_result = list_result\r
223       @pager_type = pager_type\r
224       @operators = operators\r
225       _this = this\r
226       @list_result.fetch({cache: true}).done () ->\r
227         _this.render()\r
228     \r
229     render: () ->\r
230       items = @list_result.models\r
231       f = new Locmare.Filer({\r
232         item_name: @item_name, \r
233         items: items, \r
234         list_result: @list_result, \r
235         pager_type: @pager_type, \r
236         operators: @operators\r
237       })\r
238       $("#pettanr").html(f.render().el)\r
239 \r
240   #>> https://gist.github.com/davidjbeveridge/3813724\r
241   @xeach: (arr, func, index=0) ->\r
242     if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []\r
243   \r
244   @camelize: (input) ->\r
245     pieces = input.split(/[\W_-]/)\r
246     @xeach(pieces, @capitalize).join("")\r
247   \r
248   @capitalize: (input) ->\r
249     input.charAt(0).toUpperCase() + input.slice(1)\r
250   \r
251   @lowercase: (input) ->\r
252     input.toLowerCase()\r
253    \r
254   @underscore: (input) ->\r
255     pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n\r
256     @xeach(pieces, @lowercase ).join("_")\r
257   # <<\r
258 @Pettanr = Pettanr\r