1 | define([ |
---|
2 | 'dojo/_base/declare', |
---|
3 | 'dojo/_base/Deferred', |
---|
4 | 'dojo/_base/lang', |
---|
5 | 'dojo/hash', |
---|
6 | 'dojo/topic', |
---|
7 | 'dojo/io-query', |
---|
8 | './content', |
---|
9 | './util', |
---|
10 | ],function(declare,Deferred,lang,hash,topic,uriQuery,content,util){ |
---|
11 | return new (function() { |
---|
12 | |
---|
13 | var HRI = declare(null,{ |
---|
14 | constructor: function() { |
---|
15 | this._path = this._fixPath('/'); |
---|
16 | this._args = {}; |
---|
17 | if ( arguments.length == 1 ) { |
---|
18 | this.hash(arguments[0]); |
---|
19 | } else if ( arguments.length == 2 ) { |
---|
20 | this.path(arguments[0]); |
---|
21 | this.args(arguments[1]); |
---|
22 | } |
---|
23 | }, |
---|
24 | path: function(path) { |
---|
25 | if ( path ) |
---|
26 | this._path = this._fixPath(path); |
---|
27 | return this._path; |
---|
28 | }, |
---|
29 | args: function(args) { |
---|
30 | if ( args && lang.isObject(args) ) |
---|
31 | this._args = args; |
---|
32 | return this._args; |
---|
33 | }, |
---|
34 | hash: function(hash) { |
---|
35 | if ( hash && lang.isString(hash) ) { |
---|
36 | var parts = hash.split('!'); |
---|
37 | if ( parts[1] ) |
---|
38 | this._path = this._fixPath(parts[1]); |
---|
39 | if ( parts[2] ) |
---|
40 | this._args = uriQuery.queryToObject(parts[2]); |
---|
41 | } |
---|
42 | return '!'+this._path+'!'+uriQuery.objectToQuery(this._args); |
---|
43 | }, |
---|
44 | _fixPath: function(path) { |
---|
45 | if ( !lang.isString(path) || util.isEmptyString(path) ) { |
---|
46 | path = "/"; |
---|
47 | } |
---|
48 | if ( path[0] != '/' ) { |
---|
49 | path = '/'+path; |
---|
50 | } |
---|
51 | if ( path[path.length-1] == '/' ) { |
---|
52 | path = path + "index"; |
---|
53 | } |
---|
54 | return path; |
---|
55 | } |
---|
56 | }); |
---|
57 | |
---|
58 | var currentHri = null; |
---|
59 | var currentPage = null; |
---|
60 | |
---|
61 | function _goTo(hri,replace) { |
---|
62 | var dfd = new Deferred(); |
---|
63 | |
---|
64 | // if already there, return |
---|
65 | if ( currentHri && currentHri.hash() === hri.hash() ) { |
---|
66 | dfd.resolve(); |
---|
67 | return dfd.promise; |
---|
68 | } |
---|
69 | |
---|
70 | // check if we can leave current page |
---|
71 | if ( currentPage ) { |
---|
72 | if ( currentPage.onLeave() === false ) { |
---|
73 | // restore hash if changed by hand or back button |
---|
74 | hash(currentHri.hash()); |
---|
75 | dfd.reject(); |
---|
76 | return dfd.promise; |
---|
77 | } |
---|
78 | currentPage = null; |
---|
79 | } |
---|
80 | |
---|
81 | // update hash |
---|
82 | currentHri = hri; |
---|
83 | hash(hri.hash(),replace); |
---|
84 | |
---|
85 | content._loadPage(hri.path(),hri.args()) |
---|
86 | .then(function(page){ |
---|
87 | currentPage = page; |
---|
88 | dfd.resolve(); |
---|
89 | }); |
---|
90 | |
---|
91 | return dfd.promise; |
---|
92 | } |
---|
93 | |
---|
94 | content.initialImpl = function(path,args) { |
---|
95 | if ( currentHri ) { |
---|
96 | var dfd = new Deferred(); |
---|
97 | dfd.resolve(); |
---|
98 | return dfd.promise; |
---|
99 | } |
---|
100 | if ( hash() ) { |
---|
101 | var hri = new HRI(hash()); |
---|
102 | return _goTo(hri, true); |
---|
103 | } else { |
---|
104 | return _goTo(new HRI(path,args)); |
---|
105 | } |
---|
106 | }; |
---|
107 | |
---|
108 | content.goToImpl = function(path,args) { |
---|
109 | return _goTo(new HRI(path,args)); |
---|
110 | }; |
---|
111 | |
---|
112 | topic.subscribe('/dojo/hashchange', function(){ |
---|
113 | _goTo(new HRI(hash())); |
---|
114 | }); |
---|
115 | |
---|
116 | })(); |
---|
117 | }); |
---|