OSDN Git Service

818e15720a71fc2e2a007d8c20a3b86282d45dd2
[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     if controller = Manifest.manifest().controllers[controller_name]\r
66       # convert from manifest\r
67       action = controller.actions[action_name]\r
68       action.url(params)\r
69     else\r
70       # has no manifest\r
71       controller_name + '/' + action_name\r
72   \r
73   @params_to_url: (params) ->\r
74     return '' if params['controller'] == 'folders' and params['action'] == 'root'\r
75     @url(params['controller'], params['action'], params)\r
76   \r
77   @is_sns: () ->\r
78     Manifest.manifest().magic_numbers.run_mode != 0\r
79   \r
80   @before_filter: (action_name, filter_action_names) ->\r
81     _.contains(filter_action_names, action_name)\r
82   \r
83   class Pettanr.AppHelper\r
84     @manifest: () ->\r
85       Manifest.manifest\r
86     \r
87     @full_url: (filename) ->\r
88       request.protocol + request.host_with_port + filename\r
89     \r
90     @t_m: (label) ->\r
91       l = label.split('.')\r
92       if l.length > 2\r
93         label\r
94       else\r
95         if l.length == 1\r
96           I18n.t('activerecord.models.' + Pettanr[label].item_name())\r
97         else\r
98           Pettanr.AppHelper.t_a(Pettanr[l[0]].item_name(), l[1])\r
99     \r
100     @t_a: (item_name, attr_name) ->\r
101       I18n.t('activerecord.attributes.' + item_name + '.' + attr_name)\r
102       \r
103     @t_select_items: (items) ->\r
104       _.map items, (i) ->\r
105         [I18n.t(i[0]), i[1]]\r
106     \r
107     @t_selected_item: (name, index) ->\r
108       I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])\r
109     \r
110     @distance_of_time_in_words_to_now: (datetime) ->\r
111       if Pettanr.is_blank(datetime)\r
112         ''\r
113       else\r
114         $.timeago(datetime)\r
115     \r
116   \r
117   @cache = null  # set by main\r
118   @credits = {}\r
119   \r
120   #>> https://gist.github.com/davidjbeveridge/3813724\r
121   @xeach: (arr, func, index=0) ->\r
122     if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []\r
123   \r
124   @camelize: (input) ->\r
125     pieces = input.split(/[\W_-]/)\r
126     @xeach(pieces, @capitalize).join("")\r
127   \r
128   @capitalize: (input) ->\r
129     input.charAt(0).toUpperCase() + input.slice(1)\r
130   \r
131   @lowercase: (input) ->\r
132     input.toLowerCase()\r
133    \r
134   @underscore: (input) ->\r
135     pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n\r
136     @xeach(pieces, @lowercase ).join("_")\r
137   # <<\r
138 @Pettanr = Pettanr\r