pjax.js revision fe23a9535206b8c78aff2e73f8d0d0106a148a41
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncYUI.add('pjax', function(Y) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncvar EVT_ERROR = 'error',
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync EVT_LOAD = 'load';
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsyncY.Pjax = Y.Base.create('pjax', Y.Router, [Y.PjaxBase], {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync // -- Lifecycle Methods ----------------------------------------------------
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync initializer: function () {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync this.publish(EVT_ERROR, {defaultFn: this._defCompleteFn});
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync this.publish(EVT_LOAD, {defaultFn: this._defCompleteFn});
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync // -- Public Methods -------------------------------------------------------
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync getContent: function (responseText) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync var content = {},
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync contentSelector = this.get('contentSelector'),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync frag = Y.Node.create(responseText || ''),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync titleSelector = this.get('titleSelector'),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync titleNode;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (contentSelector) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync content.node = Y.one(frag.all(contentSelector).toFrag());
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync } else {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync content.node = frag;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (titleSelector) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync titleNode = frag.one(titleSelector);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (titleNode) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync content.title = titleNode.get('text');
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return content;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync // -- Private Methods ------------------------------------------------------
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync _defaultRoute: function (req) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync // If there's an outstanding request, abort it.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync this._request && this._request.abort();
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync // Send a request.
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync this._request = Y.io(req.url, {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync context: this,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync headers: {'X-PJAX': 'true'},
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync timeout: this.get('timeout'),
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync on: {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync end : this._onPjaxIOEnd,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync failure: this._onPjaxIOFailure,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync success: this._onPjaxIOSuccess
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync });
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync },
dbec828311ed2a5cf6fbc68fe4391d516ba4f92fvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync // -- Event Handlers -------------------------------------------------------
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync _defCompleteFn: function (e) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync var container = this.get('container'),
5eb36887f6970e0033f63fa135f3bb8fbfd6059bvboxsync content = e.content;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (container && content.node) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync container.setContent(content.node);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync if (content.title && Y.config.doc) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync Y.config.doc.title = content.title;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync _onPjaxIOEnd: function () {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync this._request = null;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync _onPjaxIOFailure: function (id, res) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync var content = this.getContent(res.responseText);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync this.fire(EVT_ERROR, {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync content : content,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync responseText: res.responseText,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync status : res.status
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync });
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync },
be9960565d2df0031f0e6c8a4610f5f0ae8c1845vboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync _onPjaxIOSuccess: function (id, res, args) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync var content = this.getContent(res.responseText);
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync this.fire(EVT_LOAD, {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync content : content,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync responseText: res.responseText,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync status : res.status
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync });
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}, {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ATTRS: {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync container: {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync value: null,
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync setter: function (node) {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync return node ? Y.one(node) : null;
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync contentSelector: {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync value: null
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync routes: {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync value: [
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync {path: '*', callback: '_defaultRoute'}
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync ]
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync titleSelector: {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync value: 'title'
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync },
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync timeout: {
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync value: 30000
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync }
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync});
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync}, '@VERSION@' ,{requires:['pjax-base', 'io-base']});
30a23dfc653298a09d77d3045cf873b1bd6ddecfvboxsync