forked from KRDS/kite.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.js
174 lines (141 loc) · 3.58 KB
/
view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var modules = ['module', 'kite/loader', 'kite/transition', 'jquery'];
define('kite/view', modules, function(Module, Loader, Transition)
{
return {
_currentView: null,
_pushStateOn : false,
//Used for HTML4 browser using History.js to ignore "statechange" event on pushState or replaceState calls
_stateLocked : true,
supports_history_api: function ()
{
return !! (window.history && history.pushState);
},
load: function(next, errorClbk, data)
{
var _this = this,
_transitioner = null,
_loader = _this.loader,
_historize = true,
_historyLoading = false,
_onViewRendered,
url;
var mk = {_as_view : 1};
data = $.extend(data, mk) || mk;
if(typeof next !== 'string')
{
url = next.view;
if(typeof next.historize === 'boolean')
_historize = next.historize;
//Internal stuff
if(typeof next.historyLoading === 'boolean')
{
_historyLoading = next.historyLoading;
if(_historyLoading)
_historize = false;
}
if(next.transition instanceof Transition)
_transitioner = next.transition;
if(typeof next.onRendered === 'function')
_onViewRendered = next.onRendered;
if(typeof next.loader === 'object')
_loader = next.loader;
}
else
url = next;
url = url.indexOf(Env.fb.callbackUrl) === -1 ? Env.fb.callbackUrl + url : url;
if( ! _transitioner)
_transitioner = new Transition();
if( ! _this._currentView)
_this._currentView = document.location.href;
var historyHandler = _this.supports_history_api() ? history : History;
_this._stateLocked = true;
if(_historize)
{
historyHandler.replaceState(null, null, _this._currentView);
historyHandler.pushState(null, null, url);
}
else if( ! _historyLoading)
{
historyHandler.replaceState(null, null, url);
}
if( ! _this._pushStateOn)
{
if(_this.supports_history_api())
{
window.addEventListener('popstate', function(e)
{
_this.load({view: location.href.replace(Env.fb.callbackUrl, ''), historyLoading: true});
}, false);
}
else
{
History.Adapter.bind(window, 'statechange', function()
{
var self = _this;
if(self._stateLocked)
return;
var State = History.getState();
_this.load({view: State.url.replace(Env.fb.callbackUrl, ''), historyLoading: true});
});
}
_this._pushStateOn = true;
}
_this._currentView = url;
if(typeof errorClbk !== 'function')
{
errorClbk = function(jqXHR)
{
if(jqXHR.status == 400 || jqXHR.status == 500)
console.log(jqXHR.responseText);
};
}
if( ! _onViewRendered)
{
if(typeof Module.config().onViewRendered === 'function')
_onViewRendered = Module.config().onViewRendered;
else
_onViewRendered = function(){};
}
_loader.start();
$.ajax(
{
cache : false,
url : url,
type : 'POST',
dataType : 'json',
data : data,
error : errorClbk,
success : function(data)
{
_transitioner
.containerElement('main')
.content(data.content)
.stop(function()
{
_loader.stop();
_onViewRendered();
});
Loader.load(data.assets, null, 0, false, _transitioner);
},
complete: function()
{
_this._stateLocked = false;
}
});
return false;
},
/* Internal loader */
loader: {
start: function()
{
$('#view-loader').show();
require('nprogress').start();
},
stop: function()
{
$('#view-loader').hide();
require('nprogress').done();
}
}
};
});