OSDN Git Service

1dc751005c2f147d7678da90efd640cd06bfbdcf
[pettanr/pettanr.git] / app / assets / javascripts / pettanr.js.coffee
1 class Pettanr\r
2   @is_blank: (str) ->\r
3     !str?.trim()\r
4   \r
5   class Pettanr.AppHelper\r
6     @manifest: () ->\r
7       Manifest.manifest\r
8     \r
9     @full_url: (filename) ->\r
10       request.protocol + request.host_with_port + filename\r
11     \r
12     @t_m: (label) ->\r
13       l = label.split('.')\r
14       if l.length > 2\r
15         label\r
16       else\r
17         if l.length == 1\r
18           I18n.t('activerecord.models.' + Pettanr[label].item_name())\r
19         else\r
20           I18n.t('activerecord.models.attribtes.' + Pettanr[l.first].item_name() + '.' + l.last)\r
21     \r
22     @t_select_items: (items) ->\r
23       _.map items, (i) ->\r
24         [t(i[0]), i[1]]\r
25     \r
26     @t_selected_item: (name, index) ->\r
27       I18n.t(Manifest.manifest().system_resources.select_items[name][index][0])\r
28     \r
29     @image_tag: (src, opt = {}) ->\r
30       size = if opt['size']\r
31         parseInt(opt['size'])\r
32       else\r
33         35\r
34       "<img src=\"" + src + "\" width=" + size.toString() + " height=" + size.toString() + ">"\r
35     \r
36     @icon_tag: (name, opt = {}) ->\r
37       title = if opt['object']\r
38         name + ' ID:' + opt['object'].get(id).toString()\r
39       else\r
40         name\r
41       size = if opt['size']\r
42         parseInt(opt['size'])\r
43       else\r
44         35\r
45       "<img src=\"" + '/images/' + Pettanr.underscore(name) + '.gif' + "\" width=" + size.toString() + " height=" + size.toString() + " alt=\"" + name + ' Icon' + "title=\"" + title + ">"\r
46     \r
47     @distance_of_time_in_words_to_now: (datetime) ->\r
48       datetime\r
49 \r
50   class Pettanr.AppController\r
51 \r
52   #>> https://gist.github.com/davidjbeveridge/3813724\r
53   @xeach: (arr, func, index=0) ->\r
54     if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else []\r
55   \r
56   @camelize: (input) ->\r
57     pieces = input.split(/[\W_-]/)\r
58     @xeach(pieces, @capitalize).join("")\r
59   \r
60   @capitalize: (input) ->\r
61     input.charAt(0).toUpperCase() + input.slice(1)\r
62   \r
63   @lowercase: (input) ->\r
64     input.toLowerCase()\r
65    \r
66   @underscore: (input) ->\r
67     pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n\r
68     @xeach(pieces, @lowercase ).join("_")\r
69   # <<\r
70   @is_a_string: (str) ->\r
71     if (str instanceof String) or (typeof str == "string")\r
72       true\r
73     else\r
74       false\r
75   \r
76 @Pettanr = Pettanr\r