[263] | 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){ |
---|
[257] | 5 | return new (function() { |
---|
| 6 | var self = this; |
---|
[263] | 7 | var currentHash = ""; |
---|
| 8 | var currentPage = null; |
---|
[256] | 9 | |
---|
[257] | 10 | var HRI = declare(null,{ |
---|
[260] | 11 | constructor: function() { |
---|
[263] | 12 | this._path = this._fixPath('/'); |
---|
[257] | 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 | } |
---|
[256] | 53 | }); |
---|
| 54 | |
---|
[257] | 55 | function _goTo(hri,replace) { |
---|
| 56 | var contentPane = dijit.byId('content'); |
---|
[263] | 57 | var dfd = new Deferred(); |
---|
[257] | 58 | var newHash = hri.hash(); |
---|
[263] | 59 | |
---|
| 60 | // if already there, return |
---|
| 61 | if ( currentHash === newHash ) { |
---|
| 62 | dfd.resolve(); |
---|
| 63 | return dfd.promise; |
---|
[257] | 64 | } |
---|
[263] | 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; |
---|
[257] | 76 | hash(newHash,replace); |
---|
[263] | 77 | |
---|
| 78 | // load html |
---|
| 79 | var pageUrl = 'pages'+hri.path()+'.html'; |
---|
[257] | 80 | xhr.get({ |
---|
[263] | 81 | url: pageUrl, |
---|
[260] | 82 | failOk: true |
---|
[257] | 83 | }) |
---|
[263] | 84 | |
---|
| 85 | // initialize page or create error message |
---|
[257] | 86 | .then(function(html){ |
---|
[263] | 87 | currentPage = new _Page({ |
---|
| 88 | templateString: html, |
---|
| 89 | pageQuery: hri.args() |
---|
| 90 | }); |
---|
| 91 | contentPane.set('content',currentPage); |
---|
| 92 | dfd.resolve(); |
---|
[257] | 93 | },function(){ |
---|
[263] | 94 | currentPage = new _Page({ |
---|
| 95 | templateString: "<div>Page "+hri.path()+" not found.</div>" |
---|
| 96 | }); |
---|
| 97 | contentPane.set('content',currentPage); |
---|
| 98 | dfd.reject(); |
---|
[257] | 99 | }); |
---|
[263] | 100 | return dfd.promise; |
---|
[256] | 101 | } |
---|
| 102 | |
---|
[257] | 103 | self.initial = function(path,args) { |
---|
[263] | 104 | if ( currentHash ) { |
---|
[260] | 105 | var dfd = new Deferred(); |
---|
[263] | 106 | dfd.resolve(); |
---|
[260] | 107 | return dfd.promise; |
---|
[257] | 108 | } |
---|
| 109 | if ( hash() ) { |
---|
| 110 | var hri = new HRI(hash()); |
---|
[260] | 111 | return _goTo(hri, true); |
---|
[257] | 112 | } else { |
---|
[260] | 113 | return _goTo(new HRI(path,args)); |
---|
[257] | 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | self.goTo = function(path,args) { |
---|
[260] | 118 | return _goTo(new HRI(path,args)); |
---|
[257] | 119 | } |
---|
| 120 | |
---|
| 121 | connect.subscribe('/dojo/hashchange', function(){ |
---|
| 122 | _goTo(new HRI(hash())); |
---|
[256] | 123 | }); |
---|
[257] | 124 | |
---|
| 125 | })(); |
---|
| 126 | }); |
---|