summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/_router.js
blob: 8d27b9cc8ab414cd2f7c07e5d3e447ca2f8579c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var SiteRouter = Router.extend({

	el: 'body',
	routeByHash: false,

	routes: {
		'/':                  'home',
		'/page/:id':          'pageById',
		'/painting/:id':      'paintingById',
		'/picture/:id':       'paintingById',
		'/paintings':         'list',
		'/pictures':          'list',
	},

	initialize: function(){
	  var fn
	  for (var route in this.routes) {
	    fn = this.routes[route]
	    if (! this[fn]) {
	      this[fn] = this.default_view(fn)
	    }
	  }
	},

  initial_route: null,
	launch: function(){
	  if (this.initial_route) {
      this.parseRoute( this.initial_route )
	  }
	  else {
      this.route()
	  }
	  this.initial_route = null
	},

  go: function(url){
    if (app.view && app.view.hide) {
      app.view.hide()
    }
    this.pushState(url)
    this.parseRoute(url)
  },

  default_view: function(name){
    var fn = function(){
      if (app.view != app.login && app.view != app.signin) {
        app.last_view = app.view
      }
      if (app.view && app.view.hide) {
        app.view.hide()
      }
      if (name.match(/\./)) {
        var n = name.split(".")
        app.view = app.views[n[0]][n[1]]
      }
      else {
        app.view = app.views[name]
      }
      app.view.show()
    }.bind(this)
    return fn
  },

  paintingById: function(id){
    if (app.view && app.view.hide) {
      app.view.hide()
    }
	  app.view = app.views.painting
	  app.views.painting.show()
	  app.views.painting.load(id)
  },
  
  pageById: function(id){
    if (app.view && app.view.hide) {
      app.view.hide()
    }
	  app.view = app.views.page
	  app.views.page.show()
	  app.views.page.load(id)
  }

})