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