OSDN Git Service

fix: view
[pettanr/pettanr.git] / app / assets / javascripts / view / layout.js.coffee
diff --git a/app/assets/javascripts/view/layout.js.coffee b/app/assets/javascripts/view/layout.js.coffee
new file mode 100644 (file)
index 0000000..6f58ac4
--- /dev/null
@@ -0,0 +1,181 @@
+class Pettanr.View.Layout\r
+class Pettanr.View.Layout.Base extends Pettanr.View\r
+  \r
+class Pettanr.View.Layout.Sns extends Pettanr.View.Layout.Base\r
+  className: 'layout-sns'\r
+  \r
+  initialize: (options) ->\r
+    @global_router = new Pettanr.GlobalRouter({})\r
+    @listenTo(@global_router, 'go', @body_get)\r
+    @gate = new Pettanr.BeforeUnload()\r
+    @header = new Pettanr.View.Layout.Sns.Header({parent: this})\r
+    @history = new Pettanr.Views.History({parent: this})\r
+    @listenTo(@history, 'http_get', @body_http_get)\r
+    @listenTo(@history, 'get', @body_get)\r
+    @body = new Pettanr.View.Layout.Sns.Body({parent: this})\r
+    @footer = new Pettanr.View.Layout.Sns.Footer({parent: this})\r
+  \r
+  render: () ->\r
+    this.$el.html('')\r
+    this.$el.append(@header.render().el)\r
+    this.$el.append(@history.render().el)\r
+    @rb()\r
+    this.$el.append(@body.render().el)\r
+    this.$el.append(@footer.render().el)\r
+    this\r
+  \r
+  init_operators: (user) ->\r
+    author = new Pettanr.Author(user.get('author')) if user.get('author')\r
+    artist = new Pettanr.Artist(user.get('artist')) if user.get('artist')\r
+    Pettanr.cache.operators.init({\r
+      user: user, \r
+      author: author, \r
+      artist: artist\r
+    })\r
+  \r
+  reset_operators: () ->\r
+    Pettanr.cache.operators.init({\r
+      user: null, \r
+      author: null, \r
+      artist: null\r
+    })\r
+  \r
+  start: () ->\r
+    # check current user signed in?\r
+    proxy = new Pettanr.Proxy({})\r
+    @listenTo(proxy, 'redirect', @test_redirect)\r
+    # @listenTo(proxy, 'success', @test_ok)\r
+    @listenTo(proxy, 'fail', @test_ng)\r
+    @listenTo(proxy, 'sign_in', @sign_in)\r
+    proxy.http_post('users/test')\r
+  \r
+  test_ok: (user, response) ->\r
+    @history.enable()\r
+  \r
+  test_ng: (response) ->\r
+    @history.disable()\r
+    @head_http_get('users/guest')\r
+    @body_http_get('user_sessions/new')\r
+  \r
+  test_redirect: (url) ->\r
+    proxy = @getter_proxy()\r
+    @stopListening(proxy, 'done')  # ignore global routing\r
+    @stopListening(proxy, 'title', @push_history)  # ignore push_history\r
+    proxy.http_get(url, null)\r
+  \r
+  sign_in: (user) ->\r
+    @init_operators(user)\r
+    @head_http_get('users/account')\r
+    @history.enable()\r
+  \r
+  sign_out: () ->\r
+    @reset_operators()\r
+    @head_http_get('users/guest')\r
+    @history.disable()\r
+  \r
+  head_http_get: (url) ->\r
+    proxy = new Pettanr.Proxy({})\r
+    @listenTo(proxy, 'ready', @head_ready)\r
+    proxy.http_get(url, null)\r
+  \r
+  head_ready: (view) ->\r
+    @listenTo(view, 'http_get', @body_http_get)\r
+    @listenTo(view, 'http_post', @body_http_post)\r
+    @header.refresh(view)\r
+  \r
+  getter_proxy: () ->\r
+    proxy = new Pettanr.Proxy({gate: @gate})\r
+    @listenTo(proxy, 'ready', @body_ready)\r
+    @listenTo(proxy, 'title', @body_title)\r
+    @listenTo(proxy, 'title', @push_history)\r
+    @listenTo(proxy, 'done', @body_done)\r
+    @listenTo(proxy, 'deny', @body_deny)\r
+    @listenTo(proxy, 'lock', @body_lock)\r
+    @listenTo(proxy, 'unlock', @body_unlock)\r
+    @listenTo(proxy, 'cancel', @cancel)\r
+    proxy\r
+  \r
+  body_http_get: (url, form) ->\r
+    @getter_proxy().http_get(url, form)\r
+  \r
+  body_get: (params, form) ->\r
+    @getter_proxy().get(params, form)\r
+  \r
+  body_ready: (view) ->\r
+    @listenTo(view, 'http_get', @body_http_get)\r
+    @listenTo(view, 'http_post', @body_http_post)\r
+    @body.refresh(view)\r
+  \r
+  body_done: (params) ->\r
+    url = Pettanr.params_to_url(params)\r
+    @global_router.navigate(url, {trigger: false})\r
+  \r
+  push_history: (params, str = null) ->\r
+    @history.push(params, str)\r
+  \r
+  body_title: (params, str = null) ->\r
+    t = str || I18n.t(params['controller'] + '.' + params['action'] + '.title')\r
+    site_caption = _.escape(Manifest.manifest().magic_numbers.profile.users.caption)\r
+    $(document).attr('title', t + ' - ' + site_caption)\r
+  \r
+  poster_proxy: () ->\r
+    proxy = new Pettanr.Proxy({})\r
+    @listenTo(proxy, 'success', @body_success)\r
+    @listenTo(proxy, 'fail', @body_fail)\r
+    @listenTo(proxy, 'redirect', @body_redirect)\r
+    @listenTo(proxy, 'sign_in', @sign_in)\r
+    @listenTo(proxy, 'sign_out', @sign_out)\r
+    @listenTo(proxy, 'deny', @body_deny)\r
+    @listenTo(proxy, 'unlock', @body_unlock)\r
+    proxy\r
+  \r
+  body_http_post: (url, form) ->\r
+    @poster_proxy().http_post(url, form)\r
+  \r
+  body_post: (params, form) ->\r
+    @poster_proxy().post(params, form)\r
+  \r
+  body_success: (url) ->\r
+    \r
+  \r
+  body_fail: (url) ->\r
+    \r
+  \r
+  body_redirect: (url) ->\r
+    @global_router.navigate(url)\r
+    @getter_proxy().http_get(url)\r
+  \r
+  body_deny: (params, form, safe_url) ->\r
+    @getter_proxy().http_get(safe_url, params, form)\r
+  \r
+  body_lock: (params, form) ->\r
+    @gate.lock(params, form)\r
+  \r
+  body_unlock: (params, form) ->\r
+    @gate.unlock()\r
+  \r
+  cancel: (params, form) ->\r
+  \r
+class Pettanr.View.Layout.Blog extends Pettanr.View.Layout.Base\r
+  className: 'layout-blog'\r
+  \r
+  start: () ->\r
+    # initial page\r
+    @router.navigate('users/sign_in')\r
+    @header.navigate('users/guest')\r
+    @body.navigate('top/demo')\r
+  \r
+  sign_in_fail: (user, response) ->\r
+    view = new Pettanr.Views.UserSeeeion.New({})\r
+    view.render()\r
+    @body.navigate('top/demo')\r
+  \r
+  header_class: () ->\r
+    Pettanr.Views.Layout.Blog.Header\r
+  \r
+  body_class: () ->\r
+    Pettanr.Views.Layout.Blog.Body\r
+  \r
+  footer_class: () ->\r
+    Pettanr.Views.Layout.Blog.Footer\r
+  \r