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