OSDN Git Service

fix escape html
[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       if Pettanr.is_blank(datetime)\r
98         ''\r
99       else\r
100         $.timeago(datetime)\r
101   \r
102   @cache = {}\r
103   @credits = {}\r
104   \r
105   class Pettanr.AppController\r
106     constructor: () ->\r
107       @params = {}\r
108       @operators = window.operators\r
109       \r
110     set_model: () ->\r
111       @my_controller = Manifest.manifest().controllers[@params['controller']]\r
112       @my_action = @my_controller.actions[@params['action']]\r
113       @my_model = Manifest.manifest().models[@my_action.item_name]\r
114       @my_model_class = @my_model.classify()\r
115       \r
116     set_list: () ->\r
117       @set_model()\r
118       @my_list_model = Manifest.manifest().models[@my_action.item_name]\r
119       @my_list_model_class = @my_list_model.classify()\r
120       @list = Locmare.ListGroup.list(\r
121         @my_action.path_name(), @my_action.name, @operators, @params\r
122       )\r
123     \r
124     set_show: () ->\r
125       @set_model()\r
126       @item = new @my_model_class({id: @params['id']})\r
127     \r
128     filer_list: () ->\r
129       @set_list()\r
130       @list.open(() =>\r
131         pager = Locmare.ListGroupModule.LibModule.Pager.factory(@list.page_status, @params)\r
132         f = new Locmare.Filer({\r
133           el: "#pettanr",\r
134           item_name: @my_list_model_class.item_name(), \r
135           items: @list.items(), \r
136           pager: pager, \r
137           operators: @operators\r
138         })\r
139       )\r
140       false\r
141     \r
142     set_play: () ->\r
143       @set_list()\r
144       @binder_action = @my_action.original\r
145       @binder_controller = @binder_action.controller()\r
146       @binder_model = Manifest.manifest().models[@binder_action.item_name]\r
147       @binder_model_class = @binder_model.classify()\r
148       @item = new @binder_model_class({id: @params['id']})\r
149     \r
150     play_list: () ->\r
151       @list.open(() =>\r
152         console.log @list.items()\r
153       )\r
154       false\r
155     \r
156     show_prof: () ->\r
157       @item.fetch({cache: true}).done =>\r
158         @item.boosts 'post'\r
159         profiler = new Locmare.Profiler({\r
160           item_name: @item.item_name(), \r
161           item: @item, \r
162           operators: @operators\r
163         })\r
164         $("#pettanr").html(profiler.render().el)\r
165         @redraw_title(@params)\r
166     \r
167     set_new: () ->\r
168       @set_model()\r
169       @item = new @my_model_class({id: @params['id']})\r
170       @item.boosts 'post'\r
171       # Backbone.Model has defaults property\r
172       #@item.supply_default()\r
173     \r
174     set_edit: () ->\r
175       @set_model()\r
176       @item = new @my_model_class({id: @params['id']})\r
177       @item.fetch({cache: true}).done =>\r
178         @item.boosts 'post'\r
179         @render_form()\r
180     \r
181     render_form: () ->\r
182       @form = Locmare.Form.factory({\r
183         form_name: @item.item_name(), \r
184         item: @item, \r
185         mounted: true, \r
186         submit: 'default', \r
187         operators: @operators,\r
188         action: '/' + @item.table_name() + '/' + Pettanr.to_s(@item.get('id'))\r
189       })\r
190       $("#pettanr").html(@form.render().el)\r
191     \r
192     form_new: () ->\r
193       @set_new()\r
194       @render_form()\r
195     \r
196     form_edit: () ->\r
197       @set_edit()\r
198     \r
199     redraw_title: (params, str = null) ->\r
200       t = str || I18n.t(params['controller'] + '.' + params['action'] + '.title')\r
201       site_caption = _.escape(Manifest.manifest().magic_numbers.profile.users.caption)\r
202       $(document).attr('title', t + ' - ' + site_caption)\r
203     \r
204   #>> https://gist.github.com/davidjbeveridge/3813724\r
205   @xeach: (arr, func, index=0) ->\r
206     if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []\r
207   \r
208   @camelize: (input) ->\r
209     pieces = input.split(/[\W_-]/)\r
210     @xeach(pieces, @capitalize).join("")\r
211   \r
212   @capitalize: (input) ->\r
213     input.charAt(0).toUpperCase() + input.slice(1)\r
214   \r
215   @lowercase: (input) ->\r
216     input.toLowerCase()\r
217    \r
218   @underscore: (input) ->\r
219     pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n\r
220     @xeach(pieces, @lowercase ).join("_")\r
221   # <<\r
222 @Pettanr = Pettanr\r