From 44ac703d19c7c8c2deebd766ebb670981896d533 Mon Sep 17 00:00:00 2001 From: "James D. Forrester" Date: Wed, 6 Feb 2019 11:52:46 -0800 Subject: [PATCH] resources: Update OOjs-Router from 48302a572 to v0.1.0 Add to foreign resource validation tool. Change-Id: I06f5b2c258d1e72f2eb6268a6d24824bbe5d19eb --- maintenance/resources/foreign-resources.yaml | 8 + .../lib/oojs-router/{LICENSE-MIT => LICENSE} | 0 resources/lib/oojs-router/oojs-router.js | 195 +++++++++--------- 3 files changed, 109 insertions(+), 94 deletions(-) rename resources/lib/oojs-router/{LICENSE-MIT => LICENSE} (100%) diff --git a/maintenance/resources/foreign-resources.yaml b/maintenance/resources/foreign-resources.yaml index 7760f1d133..cf8ed78b24 100644 --- a/maintenance/resources/foreign-resources.yaml +++ b/maintenance/resources/foreign-resources.yaml @@ -110,6 +110,14 @@ oojs: package/LICENSE-MIT: package/README.md: +oojs-router: + type: tar + src: https://registry.npmjs.org/oojs-router/-/oojs-router-0.1.0.tgz + integrity: sha384-h5D3oeG4TeizHhgXY8Z9ghnas3BcNQI7keiUrwS06Zu4zSCiU6ht+Tu8eRbWuqhs + dest: + package/dist/oojs-router.js: + package/LICENSE: + ooui: type: tar src: https://registry.npmjs.org/oojs-ui/-/oojs-ui-0.30.2.tgz diff --git a/resources/lib/oojs-router/LICENSE-MIT b/resources/lib/oojs-router/LICENSE similarity index 100% rename from resources/lib/oojs-router/LICENSE-MIT rename to resources/lib/oojs-router/LICENSE diff --git a/resources/lib/oojs-router/oojs-router.js b/resources/lib/oojs-router/oojs-router.js index b136923e9a..03aeeb2040 100644 --- a/resources/lib/oojs-router/oojs-router.js +++ b/resources/lib/oojs-router/oojs-router.js @@ -1,131 +1,150 @@ /*! * OOjs Router v0.1.0 - * https://www.mediawiki.org/wiki/OOjs + * https://www.mediawiki.org/wiki/OOjs_Router * - * Copyright 2011-2016 OOjs Team and other contributors. + * Copyright 2011-2019 OOjs Team and other contributors. * Released under the MIT license - * http://oojs-router.mit-license.org + * http://oojs.mit-license.org * - * Date: 2016-05-05T19:27:58Z + * Date: 2019-01-11T03:40:55Z */ ( function ( $ ) { 'use strict'; -/** - * Does hash match entry.path? If it does apply the - * callback for the Entry object. - * - * @method - * @private - * @ignore - * @param {string} hash string to match - * @param {Object} entry Entry object - * @return {boolean} Whether hash matches entry.path - */ -function matchRoute( hash, entry ) { - var match = hash.match( entry.path ); - if ( match ) { - entry.callback.apply( this, match.slice( 1 ) ); - return true; - } - return false; -} - /** * Provides navigation routing and location information * - * @class Router - * @mixins OO.EventEmitter + * @class OO.Router + * @extends OO.Registry */ -function Router() { - var self = this; - OO.EventEmitter.call( this ); - // use an object instead of an array for routes so that we don't - // duplicate entries that already exist - this.routes = {}; +OO.Router = function OoRouter() { + var router = this; + + // Parent constructor + OO.Router.parent.call( this ); + this.enabled = true; this.oldHash = this.getPath(); $( window ).on( 'popstate', function () { - self.emit( 'popstate' ); + router.emit( 'popstate' ); } ); $( window ).on( 'hashchange', function () { - self.emit( 'hashchange' ); + router.emit( 'hashchange' ); } ); this.on( 'hashchange', function () { - // ev.originalEvent.newURL is undefined on Android 2.x - var routeEv; + // event.originalEvent.newURL is undefined on Android 2.x + var routeEvent; - if ( self.enabled ) { - routeEv = $.Event( 'route', { - path: self.getPath() + if ( router.enabled ) { + routeEvent = $.Event( 'route', { + path: router.getPath() } ); - self.emit( 'route', routeEv ); + router.emit( 'route', routeEvent ); - if ( !routeEv.isDefaultPrevented() ) { - self.checkRoute(); + if ( !routeEvent.isDefaultPrevented() ) { + router.checkRoute(); } else { // if route was prevented, ignore the next hash change and revert the // hash to its old value - self.enabled = false; - self.navigate( self.oldHash ); + router.enabled = false; + router.navigate( router.oldHash ); } } else { - self.enabled = true; + router.enabled = true; } - self.oldHash = self.getPath(); + router.oldHash = router.getPath(); } ); -} -OO.mixinClass( Router, OO.EventEmitter ); +}; + +/* Inheritance */ + +OO.inheritClass( OO.Router, OO.Registry ); + +/* Events */ /** - * Check the current route and run appropriate callback if it matches. + * @event popstate + */ + +/** + * @event hashchange + */ + +/** + * @event route + * @param {jQuery.Event} routeEvent + */ + +/* Static Methods */ + +/** + * Determine if current browser supports this router * - * @method + * @return {boolean} The browser is supported */ -Router.prototype.checkRoute = function () { - var hash = this.getPath(); +OO.Router.static.isSupported = function () { + return 'onhashchange' in window; +}; - $.each( this.routes, function ( id, entry ) { - return !matchRoute( hash, entry ); - } ); +/* Methods */ + +/** + * Check the current route and run appropriate callback if it matches. + */ +OO.Router.prototype.checkRoute = function () { + var id, entry, match, + hash = this.getPath(); + + for ( id in this.registry ) { + entry = this.registry[ id ]; + match = hash.match( entry.path ); + if ( match ) { + entry.callback.apply( this, match.slice( 1 ) ); + return; + } + } }; /** * Bind a specific callback to a hash-based route, e.g. * * @example - * route( 'alert', function () { alert( 'something' ); } ); - * route( /hi-(.*)/, function ( name ) { alert( 'Hi ' + name ) } ); + * addRoute( 'alert', function () { alert( 'something' ); } ); + * addRoute( /hi-(.*)/, function ( name ) { alert( 'Hi ' + name ) } ); + * * Note that after defining all available routes it is up to the caller * to check the existing route via the checkRoute method. * - * @method - * @param {Object} path string or RegExp to match. + * @param {string|RegExp} path Path to match, string or regular expression * @param {Function} callback Callback to be run when hash changes to one * that matches. */ -Router.prototype.route = function ( path, callback ) { +OO.Router.prototype.addRoute = function ( path, callback ) { var entry = { path: typeof path === 'string' ? - new RegExp( '^' + path.replace( /[\\^$*+?.()|[\]{}]/g, '\\$&' ) + '$' ) - : path, + new RegExp( '^' + path.replace( /[\\^$*+?.()|[\]{}]/g, '\\$&' ) + '$' ) : + path, callback: callback }; - this.routes[ entry.path ] = entry; + this.register( entry.path.toString(), entry ); }; +/** + * @deprecated Use #addRoute + */ +OO.Router.prototype.route = OO.Router.prototype.addRoute; + /** * Navigate to a specific route. * - * @method - * @param {string} path string with a route (hash without #). + * @param {string} path String with a route (hash without #). */ -Router.prototype.navigate = function ( path ) { +OO.Router.prototype.navigate = function ( path ) { var history = window.history; // Take advantage of `pushState` when available, to clear the hash and // not leave `#` in the history. An entry with `#` in the history has @@ -141,30 +160,22 @@ Router.prototype.navigate = function ( path ) { } }; -/** - * Triggers back on the window - */ -Router.prototype.goBack = function () { - window.history.back(); -}; - /** * Navigate to the previous route. This is a wrapper for window.history.back * - * @method - * @return {jQuery.Deferred} + * @return {jQuery.Promise} Promise which resolves when the back navigation is complete */ -Router.prototype.back = function () { - var deferredRequest = $.Deferred(), - self = this, - timeoutID; +OO.Router.prototype.back = function () { + var timeoutID, + router = this, + deferred = $.Deferred(); this.once( 'popstate', function () { clearTimeout( timeoutID ); - deferredRequest.resolve(); + deferred.resolve(); } ); - this.goBack(); + window.history.back(); // If for some reason (old browser, bug in IE/windows 8.1, etc) popstate doesn't fire, // resolve manually. Since we don't know for sure which browsers besides IE10/11 have @@ -173,33 +184,29 @@ Router.prototype.back = function () { // See https://connect.microsoft.com/IE/feedback/details/793618/history-back-popstate-not-working-as-expected-in-webview-control // Give browser a few ms to update its history. timeoutID = setTimeout( function () { - self.off( 'popstate' ); - deferredRequest.resolve(); + router.off( 'popstate' ); + deferred.resolve(); }, 50 ); - return deferredRequest; + return deferred.promise(); }; /** * Get current path (hash). * - * @method * @return {string} Current path. */ -Router.prototype.getPath = function () { +OO.Router.prototype.getPath = function () { return window.location.hash.slice( 1 ); }; /** - * Determine if current browser supports onhashchange event - * - * @method - * @return {boolean} + * @deprecated Use static method */ -Router.prototype.isSupported = function () { - return 'onhashchange' in window; -}; +OO.Router.prototype.isSupported = OO.Router.static.isSupported; -module.exports = Router; +if ( typeof module !== 'undefined' && module.exports ) { + module.exports = OO.Router; +} }( jQuery ) ); -- 2.20.1