|
525
|
1 |
/* |
|
|
2 |
YUI 3.10.3 (build 2fb5187) |
|
|
3 |
Copyright 2013 Yahoo! Inc. All rights reserved. |
|
|
4 |
Licensed under the BSD License. |
|
|
5 |
http://yuilibrary.com/license/ |
|
|
6 |
*/ |
|
|
7 |
|
|
|
8 |
YUI.add('pjax', function (Y, NAME) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
Provides seamless, gracefully degrading Pjax (pushState + Ajax) functionality, |
|
|
12 |
which makes it easy to progressively enhance standard links on the page so that |
|
|
13 |
they can be loaded normally in old browsers, or via Ajax (with HTML5 history |
|
|
14 |
support) in newer browsers. |
|
|
15 |
|
|
|
16 |
@module pjax |
|
|
17 |
@main |
|
|
18 |
@since 3.5.0 |
|
|
19 |
**/ |
|
|
20 |
|
|
|
21 |
/** |
|
|
22 |
A stack of middleware which forms the default Pjax route. |
|
|
23 |
|
|
|
24 |
@property defaultRoute |
|
|
25 |
@type Array |
|
|
26 |
@static |
|
|
27 |
@since 3.7.0 |
|
|
28 |
**/ |
|
|
29 |
var defaultRoute = ['loadContent', '_defaultRoute'], |
|
|
30 |
|
|
|
31 |
/** |
|
|
32 |
Fired when an error occurs while attempting to load a URL via Ajax. |
|
|
33 |
|
|
|
34 |
@event error |
|
|
35 |
@param {Object} content Content extracted from the response, if any. |
|
|
36 |
@param {Node} content.node A `Y.Node` instance for a document fragment |
|
|
37 |
containing the extracted HTML content. |
|
|
38 |
@param {String} [content.title] The title of the HTML page, if any, |
|
|
39 |
extracted using the `titleSelector` attribute. If `titleSelector` is |
|
|
40 |
not set or if a title could not be found, this property will be |
|
|
41 |
`undefined`. |
|
|
42 |
@param {String} responseText Raw Ajax response text. |
|
|
43 |
@param {Number} status HTTP status code for the Ajax response. |
|
|
44 |
@param {String} url The absolute URL that failed to load. |
|
|
45 |
@since 3.5.0 |
|
|
46 |
**/ |
|
|
47 |
EVT_ERROR = 'error', |
|
|
48 |
|
|
|
49 |
/** |
|
|
50 |
Fired when a URL is successfully loaded via Ajax. |
|
|
51 |
|
|
|
52 |
@event load |
|
|
53 |
@param {Object} content Content extracted from the response, if any. |
|
|
54 |
@param {Node} content.node A `Y.Node` instance for a document fragment |
|
|
55 |
containing the extracted HTML content. |
|
|
56 |
@param {String} [content.title] The title of the HTML page, if any, |
|
|
57 |
extracted using the `titleSelector` attribute. If `titleSelector` is |
|
|
58 |
not set or if a title could not be found, this property will be |
|
|
59 |
`undefined`. |
|
|
60 |
@param {String} responseText Raw Ajax response text. |
|
|
61 |
@param {Number} status HTTP status code for the Ajax response. |
|
|
62 |
@param {String} url The absolute URL that was loaded. |
|
|
63 |
@since 3.5.0 |
|
|
64 |
**/ |
|
|
65 |
EVT_LOAD = 'load'; |
|
|
66 |
|
|
|
67 |
/** |
|
|
68 |
Provides seamless, gracefully degrading Pjax (pushState + Ajax) functionality, |
|
|
69 |
which makes it easy to progressively enhance standard links on the page so that |
|
|
70 |
they can be loaded normally in old browsers, or via Ajax (with HTML5 history |
|
|
71 |
support) in newer browsers. |
|
|
72 |
|
|
|
73 |
@class Pjax |
|
|
74 |
@extends Router |
|
|
75 |
@uses PjaxBase |
|
|
76 |
@uses PjaxContent |
|
|
77 |
@constructor |
|
|
78 |
@param {Object} [config] Config attributes. |
|
|
79 |
@since 3.5.0 |
|
|
80 |
**/ |
|
|
81 |
Y.Pjax = Y.Base.create('pjax', Y.Router, [Y.PjaxBase, Y.PjaxContent], { |
|
|
82 |
// -- Lifecycle Methods ---------------------------------------------------- |
|
|
83 |
initializer: function () { |
|
|
84 |
this.publish(EVT_ERROR, {defaultFn: this._defCompleteFn}); |
|
|
85 |
this.publish(EVT_LOAD, {defaultFn: this._defCompleteFn}); |
|
|
86 |
}, |
|
|
87 |
|
|
|
88 |
// -- Protected Methods ---------------------------------------------------- |
|
|
89 |
|
|
|
90 |
/** |
|
|
91 |
Default Pjax route callback. Fires either the `load` or `error` event based |
|
|
92 |
on the status of the `Y.io` request made by the `loadContent()` middleware. |
|
|
93 |
|
|
|
94 |
**Note:** This route callback assumes that it's called after the |
|
|
95 |
`loadContent()` middleware. |
|
|
96 |
|
|
|
97 |
@method _defaultRoute |
|
|
98 |
@param {Object} req Request object. |
|
|
99 |
@param {Object} res Response Object. |
|
|
100 |
@param {Function} next Function to pass control to the next route callback. |
|
|
101 |
@protected |
|
|
102 |
@since 3.5.0 |
|
|
103 |
@see Y.Pjax.defaultRoute |
|
|
104 |
**/ |
|
|
105 |
_defaultRoute: function (req, res, next) { |
|
|
106 |
var ioResponse = res.ioResponse, |
|
|
107 |
status = ioResponse.status, |
|
|
108 |
event = status >= 200 && status < 300 ? EVT_LOAD : EVT_ERROR; |
|
|
109 |
|
|
|
110 |
this.fire(event, { |
|
|
111 |
content : res.content, |
|
|
112 |
responseText: ioResponse.responseText, |
|
|
113 |
status : status, |
|
|
114 |
url : req.ioURL |
|
|
115 |
}); |
|
|
116 |
|
|
|
117 |
next(); |
|
|
118 |
}, |
|
|
119 |
|
|
|
120 |
// -- Event Handlers ------------------------------------------------------- |
|
|
121 |
|
|
|
122 |
/** |
|
|
123 |
Default event handler for both the `error` and `load` events. Attempts to |
|
|
124 |
insert the loaded content into the `container` node and update the page's |
|
|
125 |
title. |
|
|
126 |
|
|
|
127 |
@method _defCompleteFn |
|
|
128 |
@param {EventFacade} e |
|
|
129 |
@protected |
|
|
130 |
@since 3.5.0 |
|
|
131 |
**/ |
|
|
132 |
_defCompleteFn: function (e) { |
|
|
133 |
var container = this.get('container'), |
|
|
134 |
content = e.content; |
|
|
135 |
|
|
|
136 |
if (container && content.node) { |
|
|
137 |
container.setHTML(content.node); |
|
|
138 |
} |
|
|
139 |
|
|
|
140 |
if (content.title && Y.config.doc) { |
|
|
141 |
Y.config.doc.title = content.title; |
|
|
142 |
} |
|
|
143 |
} |
|
|
144 |
}, { |
|
|
145 |
ATTRS: { |
|
|
146 |
/** |
|
|
147 |
Node into which content should be inserted when a page is loaded via |
|
|
148 |
Pjax. This node's existing contents will be removed to make way for the |
|
|
149 |
new content. |
|
|
150 |
|
|
|
151 |
If not set, loaded content will not be automatically inserted into the |
|
|
152 |
page. |
|
|
153 |
|
|
|
154 |
@attribute container |
|
|
155 |
@type Node |
|
|
156 |
@default null |
|
|
157 |
@since 3.5.0 |
|
|
158 |
**/ |
|
|
159 |
container: { |
|
|
160 |
value : null, |
|
|
161 |
setter: Y.one |
|
|
162 |
}, |
|
|
163 |
|
|
|
164 |
// Inherited from Router and already documented there. |
|
|
165 |
routes: { |
|
|
166 |
value: [ |
|
|
167 |
{path: '*', callbacks: defaultRoute} |
|
|
168 |
] |
|
|
169 |
} |
|
|
170 |
}, |
|
|
171 |
|
|
|
172 |
// Documented towards the top of this file. |
|
|
173 |
defaultRoute: defaultRoute |
|
|
174 |
}); |
|
|
175 |
|
|
|
176 |
|
|
|
177 |
}, '3.10.3', {"requires": ["pjax-base", "pjax-content"]}); |