1 | define(['dojo/_base/declare','dojo/_base/connect','dojo/_base/xhr', |
---|
2 | 'dojo/_base/lang','dojo/_base/Deferred','dojo/hash', |
---|
3 | 'dojo/io-query','dijit','rft/util','rft/ui/_Page'], |
---|
4 | function(declare,connect,xhr,lang,Deferred,hash,uriQuery,dijit,util,_Page){ |
---|
5 | return new (function() { |
---|
6 | var self = this; |
---|
7 | var currentHash = ""; |
---|
8 | var currentPage = null; |
---|
9 | |
---|
10 | var HRI = declare(null,{ |
---|
11 | constructor: function() { |
---|
12 | this._path = this._fixPath('/'); |
---|
13 | this._args = {}; |
---|
14 | if ( arguments.length == 1 ) { |
---|
15 | this.hash(arguments[0]); |
---|
16 | } else if ( arguments.length == 2 ) { |
---|
17 | this.path(arguments[0]); |
---|
18 | this.args(arguments[1]); |
---|
19 | } |
---|
20 | }, |
---|
21 | path: function(path) { |
---|
22 | if ( path ) |
---|
23 | this._path = this._fixPath(path); |
---|
24 | return this._path; |
---|
25 | }, |
---|
26 | args: function(args) { |
---|
27 | if ( args && lang.isObject(args) ) |
---|
28 | this._args = args; |
---|
29 | return this._args; |
---|
30 | }, |
---|
31 | hash: function(hash) { |
---|
32 | if ( hash && lang.isString(hash) ) { |
---|
33 | var parts = hash.split('!'); |
---|
34 | if ( parts[1] ) |
---|
35 | this._path = this._fixPath(parts[1]); |
---|
36 | if ( parts[2] ) |
---|
37 | this._args = uriQuery.queryToObject(parts[2]); |
---|
38 | } |
---|
39 | return '!'+this._path+'!'+uriQuery.objectToQuery(this._args); |
---|
40 | }, |
---|
41 | _fixPath: function(path) { |
---|
42 | if ( !lang.isString(path) || util.isEmptyString(path) ) { |
---|
43 | path = "/"; |
---|
44 | } |
---|
45 | if ( path[0] != '/' ) { |
---|
46 | path = '/'+path; |
---|
47 | } |
---|
48 | if ( path[path.length-1] == '/' ) { |
---|
49 | path = path + "index"; |
---|
50 | } |
---|
51 | return path; |
---|
52 | } |
---|
53 | }); |
---|
54 | |
---|
55 | function _goTo(hri,replace) { |
---|
56 | var contentPane = dijit.byId('content'); |
---|
57 | var dfd = new Deferred(); |
---|
58 | var newHash = hri.hash(); |
---|
59 | |
---|
60 | // if already there, return |
---|
61 | if ( currentHash === newHash ) { |
---|
62 | dfd.resolve(); |
---|
63 | return dfd.promise; |
---|
64 | } |
---|
65 | |
---|
66 | // check if we can leave current page |
---|
67 | if ( currentPage ) { |
---|
68 | if ( currentPage.onLeave() === false ) { |
---|
69 | dfd.reject(); |
---|
70 | return dfd.promise; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | // update hash |
---|
75 | currentHash = newHash; |
---|
76 | hash(newHash,replace); |
---|
77 | |
---|
78 | // load html |
---|
79 | var pageUrl = 'pages'+hri.path()+'.html'; |
---|
80 | xhr.get({ |
---|
81 | url: pageUrl, |
---|
82 | failOk: true |
---|
83 | }) |
---|
84 | |
---|
85 | // initialize page or create error message |
---|
86 | .then(function(html){ |
---|
87 | currentPage = new _Page({ |
---|
88 | templateString: html, |
---|
89 | pageQuery: hri.args() |
---|
90 | }); |
---|
91 | contentPane.set('content',currentPage); |
---|
92 | dfd.resolve(); |
---|
93 | },function(){ |
---|
94 | currentPage = new _Page({ |
---|
95 | templateString: "<div>Page "+hri.path()+" not found.</div>" |
---|
96 | }); |
---|
97 | contentPane.set('content',currentPage); |
---|
98 | dfd.reject(); |
---|
99 | }); |
---|
100 | return dfd.promise; |
---|
101 | } |
---|
102 | |
---|
103 | self.initial = function(path,args) { |
---|
104 | if ( currentHash ) { |
---|
105 | var dfd = new Deferred(); |
---|
106 | dfd.resolve(); |
---|
107 | return dfd.promise; |
---|
108 | } |
---|
109 | if ( hash() ) { |
---|
110 | var hri = new HRI(hash()); |
---|
111 | return _goTo(hri, true); |
---|
112 | } else { |
---|
113 | return _goTo(new HRI(path,args)); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | self.goTo = function(path,args) { |
---|
118 | return _goTo(new HRI(path,args)); |
---|
119 | } |
---|
120 | |
---|
121 | connect.subscribe('/dojo/hashchange', function(){ |
---|
122 | _goTo(new HRI(hash())); |
---|
123 | }); |
---|
124 | |
---|
125 | })(); |
---|
126 | }); |
---|