1 | define(['dojo/_base/declare','dojo/_base/connect','dojo/_base/xhr','dojo/_base/lang','dojo/_base/Deferred','dojo/hash','dojo/io-query','dijit/registry','dijit','rft/util','rft/ui/Page'], |
---|
2 | function(declare,connect,xhr,lang,Deferred,hash,uriQuery,registry,dijit,util,Page){ |
---|
3 | return new (function() { |
---|
4 | var self = this; |
---|
5 | var current = ""; |
---|
6 | |
---|
7 | var HRI = declare(null,{ |
---|
8 | constructor: function(){ |
---|
9 | this._path = '/'; |
---|
10 | this._args = {}; |
---|
11 | if ( arguments.length == 1 ) { |
---|
12 | this.hash(arguments[0]); |
---|
13 | } else if ( arguments.length == 2 ) { |
---|
14 | this.path(arguments[0]); |
---|
15 | this.args(arguments[1]); |
---|
16 | } |
---|
17 | }, |
---|
18 | path: function(path) { |
---|
19 | if ( path ) |
---|
20 | this._path = this._fixPath(path); |
---|
21 | return this._path; |
---|
22 | }, |
---|
23 | args: function(args) { |
---|
24 | if ( args && lang.isObject(args) ) |
---|
25 | this._args = args; |
---|
26 | return this._args; |
---|
27 | }, |
---|
28 | hash: function(hash) { |
---|
29 | if ( hash && lang.isString(hash) ) { |
---|
30 | var parts = hash.split('!'); |
---|
31 | if ( parts[1] ) |
---|
32 | this._path = this._fixPath(parts[1]); |
---|
33 | if ( parts[2] ) |
---|
34 | this._args = uriQuery.queryToObject(parts[2]); |
---|
35 | } |
---|
36 | return '!'+this._path+'!'+uriQuery.objectToQuery(this._args); |
---|
37 | }, |
---|
38 | _fixPath: function(path) { |
---|
39 | if ( !lang.isString(path) || util.isEmptyString(path) ) { |
---|
40 | path = "/"; |
---|
41 | } |
---|
42 | if ( path[0] != '/' ) { |
---|
43 | path = '/'+path; |
---|
44 | } |
---|
45 | if ( path[path.length-1] == '/' ) { |
---|
46 | path = path + "index"; |
---|
47 | } |
---|
48 | return path; |
---|
49 | } |
---|
50 | }); |
---|
51 | |
---|
52 | function _goTo(hri,replace) { |
---|
53 | var contentPane = dijit.byId('content'); |
---|
54 | var d = new Deferred(); |
---|
55 | var newHash = hri.hash(); |
---|
56 | if ( current == newHash ) { |
---|
57 | d.resolve(); |
---|
58 | return d.promise; |
---|
59 | } |
---|
60 | current = newHash; |
---|
61 | hash(newHash,replace); |
---|
62 | xhr.get({ |
---|
63 | url: 'pages'+hri.path()+'.html' |
---|
64 | }) |
---|
65 | .then(function(html){ |
---|
66 | contentPane.set('content',html); |
---|
67 | var widgets = registry.findWidgets(contentPane.containerNode); |
---|
68 | for(var i in widgets) { |
---|
69 | if ( widgets[i].isInstanceOf(Page) ) { |
---|
70 | widgets[i].init(hri.args()); |
---|
71 | break; |
---|
72 | } |
---|
73 | } |
---|
74 | d.resolve(); |
---|
75 | },function(){ |
---|
76 | contentPane.set('content',"Page "+hri.path()+" not found."); |
---|
77 | d.reject(); |
---|
78 | }); |
---|
79 | return d.promise; |
---|
80 | } |
---|
81 | |
---|
82 | self.initial = function(path,args) { |
---|
83 | if ( current ) { |
---|
84 | return; |
---|
85 | } |
---|
86 | if ( hash() ) { |
---|
87 | var hri = new HRI(hash()); |
---|
88 | _goTo(hri, true); |
---|
89 | } else { |
---|
90 | _goTo(new HRI(path,args)); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | self.goTo = function(path,args) { |
---|
95 | _goTo(new HRI(path,args)); |
---|
96 | } |
---|
97 | |
---|
98 | connect.subscribe('/dojo/hashchange', function(){ |
---|
99 | _goTo(new HRI(hash())); |
---|
100 | }); |
---|
101 | |
---|
102 | })(); |
---|
103 | }); |
---|