OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[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         [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   class Pettanr.FilerRender\r
154     constructor: (item_name, list_result, pager_type, operators) ->\r
155       @item_name = item_name\r
156       @list_result = list_result\r
157       @pager_type = pager_type\r
158       @operators = operators\r
159       _this = this\r
160       @list_result.fetch({}).done () ->\r
161         _this.render()\r
162     \r
163     render: () ->\r
164       items = @list_result.models\r
165       f = new Locmare.Filer({\r
166         item_name: @item_name, \r
167         items: items, \r
168         list_result: @list_result, \r
169         pager_type: @pager_type, \r
170         operators: @operators\r
171       })\r
172       $("#pettanr").html(f.render().el)\r
173 \r
174   #>> https://gist.github.com/davidjbeveridge/3813724\r
175   @xeach: (arr, func, index=0) ->\r
176     if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []\r
177   \r
178   @camelize: (input) ->\r
179     pieces = input.split(/[\W_-]/)\r
180     @xeach(pieces, @capitalize).join("")\r
181   \r
182   @capitalize: (input) ->\r
183     input.charAt(0).toUpperCase() + input.slice(1)\r
184   \r
185   @lowercase: (input) ->\r
186     input.toLowerCase()\r
187    \r
188   @underscore: (input) ->\r
189     pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n\r
190     @xeach(pieces, @lowercase ).join("_")\r
191   # <<\r
192 @Pettanr = Pettanr\r