OSDN Git Service

6f58ac4d39d184bad30e0d380b9f13629d532eb3
[pettanr/pettanr.git] / app / assets / javascripts / view / layout.js.coffee
1 class Pettanr.View.Layout\r
2 class Pettanr.View.Layout.Base extends Pettanr.View\r
3   \r
4 class Pettanr.View.Layout.Sns extends Pettanr.View.Layout.Base\r
5   className: 'layout-sns'\r
6   \r
7   initialize: (options) ->\r
8     @global_router = new Pettanr.GlobalRouter({})\r
9     @listenTo(@global_router, 'go', @body_get)\r
10     @gate = new Pettanr.BeforeUnload()\r
11     @header = new Pettanr.View.Layout.Sns.Header({parent: this})\r
12     @history = new Pettanr.Views.History({parent: this})\r
13     @listenTo(@history, 'http_get', @body_http_get)\r
14     @listenTo(@history, 'get', @body_get)\r
15     @body = new Pettanr.View.Layout.Sns.Body({parent: this})\r
16     @footer = new Pettanr.View.Layout.Sns.Footer({parent: this})\r
17   \r
18   render: () ->\r
19     this.$el.html('')\r
20     this.$el.append(@header.render().el)\r
21     this.$el.append(@history.render().el)\r
22     @rb()\r
23     this.$el.append(@body.render().el)\r
24     this.$el.append(@footer.render().el)\r
25     this\r
26   \r
27   init_operators: (user) ->\r
28     author = new Pettanr.Author(user.get('author')) if user.get('author')\r
29     artist = new Pettanr.Artist(user.get('artist')) if user.get('artist')\r
30     Pettanr.cache.operators.init({\r
31       user: user, \r
32       author: author, \r
33       artist: artist\r
34     })\r
35   \r
36   reset_operators: () ->\r
37     Pettanr.cache.operators.init({\r
38       user: null, \r
39       author: null, \r
40       artist: null\r
41     })\r
42   \r
43   start: () ->\r
44     # check current user signed in?\r
45     proxy = new Pettanr.Proxy({})\r
46     @listenTo(proxy, 'redirect', @test_redirect)\r
47     # @listenTo(proxy, 'success', @test_ok)\r
48     @listenTo(proxy, 'fail', @test_ng)\r
49     @listenTo(proxy, 'sign_in', @sign_in)\r
50     proxy.http_post('users/test')\r
51   \r
52   test_ok: (user, response) ->\r
53     @history.enable()\r
54   \r
55   test_ng: (response) ->\r
56     @history.disable()\r
57     @head_http_get('users/guest')\r
58     @body_http_get('user_sessions/new')\r
59   \r
60   test_redirect: (url) ->\r
61     proxy = @getter_proxy()\r
62     @stopListening(proxy, 'done')  # ignore global routing\r
63     @stopListening(proxy, 'title', @push_history)  # ignore push_history\r
64     proxy.http_get(url, null)\r
65   \r
66   sign_in: (user) ->\r
67     @init_operators(user)\r
68     @head_http_get('users/account')\r
69     @history.enable()\r
70   \r
71   sign_out: () ->\r
72     @reset_operators()\r
73     @head_http_get('users/guest')\r
74     @history.disable()\r
75   \r
76   head_http_get: (url) ->\r
77     proxy = new Pettanr.Proxy({})\r
78     @listenTo(proxy, 'ready', @head_ready)\r
79     proxy.http_get(url, null)\r
80   \r
81   head_ready: (view) ->\r
82     @listenTo(view, 'http_get', @body_http_get)\r
83     @listenTo(view, 'http_post', @body_http_post)\r
84     @header.refresh(view)\r
85   \r
86   getter_proxy: () ->\r
87     proxy = new Pettanr.Proxy({gate: @gate})\r
88     @listenTo(proxy, 'ready', @body_ready)\r
89     @listenTo(proxy, 'title', @body_title)\r
90     @listenTo(proxy, 'title', @push_history)\r
91     @listenTo(proxy, 'done', @body_done)\r
92     @listenTo(proxy, 'deny', @body_deny)\r
93     @listenTo(proxy, 'lock', @body_lock)\r
94     @listenTo(proxy, 'unlock', @body_unlock)\r
95     @listenTo(proxy, 'cancel', @cancel)\r
96     proxy\r
97   \r
98   body_http_get: (url, form) ->\r
99     @getter_proxy().http_get(url, form)\r
100   \r
101   body_get: (params, form) ->\r
102     @getter_proxy().get(params, form)\r
103   \r
104   body_ready: (view) ->\r
105     @listenTo(view, 'http_get', @body_http_get)\r
106     @listenTo(view, 'http_post', @body_http_post)\r
107     @body.refresh(view)\r
108   \r
109   body_done: (params) ->\r
110     url = Pettanr.params_to_url(params)\r
111     @global_router.navigate(url, {trigger: false})\r
112   \r
113   push_history: (params, str = null) ->\r
114     @history.push(params, str)\r
115   \r
116   body_title: (params, str = null) ->\r
117     t = str || I18n.t(params['controller'] + '.' + params['action'] + '.title')\r
118     site_caption = _.escape(Manifest.manifest().magic_numbers.profile.users.caption)\r
119     $(document).attr('title', t + ' - ' + site_caption)\r
120   \r
121   poster_proxy: () ->\r
122     proxy = new Pettanr.Proxy({})\r
123     @listenTo(proxy, 'success', @body_success)\r
124     @listenTo(proxy, 'fail', @body_fail)\r
125     @listenTo(proxy, 'redirect', @body_redirect)\r
126     @listenTo(proxy, 'sign_in', @sign_in)\r
127     @listenTo(proxy, 'sign_out', @sign_out)\r
128     @listenTo(proxy, 'deny', @body_deny)\r
129     @listenTo(proxy, 'unlock', @body_unlock)\r
130     proxy\r
131   \r
132   body_http_post: (url, form) ->\r
133     @poster_proxy().http_post(url, form)\r
134   \r
135   body_post: (params, form) ->\r
136     @poster_proxy().post(params, form)\r
137   \r
138   body_success: (url) ->\r
139     \r
140   \r
141   body_fail: (url) ->\r
142     \r
143   \r
144   body_redirect: (url) ->\r
145     @global_router.navigate(url)\r
146     @getter_proxy().http_get(url)\r
147   \r
148   body_deny: (params, form, safe_url) ->\r
149     @getter_proxy().http_get(safe_url, params, form)\r
150   \r
151   body_lock: (params, form) ->\r
152     @gate.lock(params, form)\r
153   \r
154   body_unlock: (params, form) ->\r
155     @gate.unlock()\r
156   \r
157   cancel: (params, form) ->\r
158   \r
159 class Pettanr.View.Layout.Blog extends Pettanr.View.Layout.Base\r
160   className: 'layout-blog'\r
161   \r
162   start: () ->\r
163     # initial page\r
164     @router.navigate('users/sign_in')\r
165     @header.navigate('users/guest')\r
166     @body.navigate('top/demo')\r
167   \r
168   sign_in_fail: (user, response) ->\r
169     view = new Pettanr.Views.UserSeeeion.New({})\r
170     view.render()\r
171     @body.navigate('top/demo')\r
172   \r
173   header_class: () ->\r
174     Pettanr.Views.Layout.Blog.Header\r
175   \r
176   body_class: () ->\r
177     Pettanr.Views.Layout.Blog.Body\r
178   \r
179   footer_class: () ->\r
180     Pettanr.Views.Layout.Blog.Footer\r
181   \r