pjax-base-debug.js revision dff8d056ecfb52c863b7ad28e8ecea3a3789ad92
23c56a064638d10a6057e991c7dc113cc15c379aEric FerraiuoloYUI.add('pjax-base', function(Y) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolovar win = Y.config.win,
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo originalRoot = getRoot(),
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo CLASS_PJAX = Y.ClassNameManager.getClassName('pjax'),
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo EVT_NAVIGATE = 'navigate';
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolofunction getRoot() {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo var segments = (win && win.location.pathname.split('/')) || [];
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo segments.pop();
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo return segments.join('/');
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo}
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo// PjaxBase is a mixin for Controller.
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolofunction PjaxBase() {}
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric FerraiuoloPjaxBase.prototype = {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // -- Properties -----------------------------------------------------------
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo _resolved: {},
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo _regexUrl: /^((?:([^:]+):(?:\/\/)?|\/\/)[^\/]*)?([^?#]*)(.*)$/i,
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // -- Lifecycle Methods ----------------------------------------------------
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo initializer: function () {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo this._pjaxRoot = originalRoot;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo this.publish(EVT_NAVIGATE, {defaultFn: this._defNavigateFn});
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo this._pjaxBindUI();
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo },
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo destructor: function () {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo this._pjaxEvents && this._pjaxEvents.detach();
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo },
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // -- Public Prototype Methods ---------------------------------------------
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo load: function (url) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo this.save(this._resolveUrl(url));
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (this.get('scrollToTop') && Y.config.win) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // Scroll to the top of the page. The timeout ensures that the
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // scroll happens after navigation begins, so that the current
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // scroll position will be restored if the user clicks the back
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // button.
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo setTimeout(function () {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo Y.config.win.scroll(0, 0);
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }, 1);
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo },
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // -- Protected Prototype Methods ------------------------------------------
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo _getRoot: getRoot,
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo _normalizePath: function (path) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo var dots = '..',
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo slash = '/',
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo i, len, normalized, parts, part, stack;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (!path) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo return path;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo parts = path.split(slash);
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo stack = [];
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo for (i = 0, len = parts.length; i < len; ++i) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo part = parts[i];
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (part === dots) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo stack.pop();
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo } else if (part) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo stack.push(part);
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo normalized = stack.join(slash);
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // Append a slash if necessary.
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (path.charAt(path.length - 1) === slash) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo normalized += slash;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo return normalized;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo },
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo _pjaxBindUI: function () {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (this.html5) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo this._pjaxEvents = Y.one('body').delegate('click',
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo this._onLinkClick, this.get('linkSelector'), this);
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo },
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo _resolvePath: function (path) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo var root = this._pjaxRoot;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (!path) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo return root;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // Path is host relative.
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (path.charAt(0) === '/') {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo return path;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo return this._normalizePath(root + '/' + path);
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo },
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo _resolveUrl: function (url) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo var self = this,
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo resolved = self._resolved,
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo resolvedUrl = resolved[url];
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (resolvedUrl) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo return resolvedUrl;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo function resolve(match, prefix, scheme, path, suffix) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (scheme && scheme.toLowerCase().indexOf('http') !== 0) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo return match;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo return (prefix || '') + self._resolvePath(path) + (suffix || '');
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // Cache resolved URL.
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo resolvedUrl = resolved[url] = url.replace(self._regexUrl, resolve);
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo return resolvedUrl;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo },
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // -- Protected Event Handlers ---------------------------------------------
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo _defNavigateFn: function (e) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo this.load(e.url);
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo },
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo _onLinkClick: function (e) {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo var url = this._resolveUrl(e.currentTarget.get('href'));
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // Allow the native behavior on middle/right-click, or when Ctrl or
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // Command are pressed.
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (e.button !== 1 || e.ctrlKey || e.metaKey) { return; }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo // Do nothing if there's no matching route for this URL.
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo if (!this.hasRoute(url)) { return; }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo e.preventDefault();
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo this.fire(EVT_NAVIGATE, {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo originEvent: e,
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo url : url
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo });
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo};
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric FerraiuoloPjaxBase.ATTRS = {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo linkSelector: {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo value : 'a.' + CLASS_PJAX,
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo writeOnce: 'initOnly'
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo },
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo scrollToTop: {
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo value: true
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo }
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo};
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric FerraiuoloY.PjaxBase = PjaxBase;
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo}, '@VERSION@' ,{requires:['classnamemanager', 'controller', 'node-event-delegate']});
23c56a064638d10a6057e991c7dc113cc15c379aEric Ferraiuolo