Cicalese has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/387966 )
Change subject: Refactored. ...................................................................... Refactored. Change-Id: If08cc5bfe5b9ecf566e6be8f658d970a666e78e0 --- D JSBreadCrumbs.hooks.php D JSBreadCrumbs.php A extension.json M i18n/en.json M i18n/qqq.json A includes/JSBreadCrumbsHooks.php D js/BreadCrumbs.js R resources/JSBreadCrumbs.css A resources/JSBreadCrumbs.js 9 files changed, 317 insertions(+), 397 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/JSBreadCrumbs refs/changes/66/387966/1 diff --git a/JSBreadCrumbs.hooks.php b/JSBreadCrumbs.hooks.php deleted file mode 100644 index 0e248e5..0000000 --- a/JSBreadCrumbs.hooks.php +++ /dev/null @@ -1,149 +0,0 @@ -<?php -class JSBreadCrumbsHooks { - /** - * BeforePageDisplay hook - */ - public static function addResources( $out ) { - global $wgExtensionAssetsPath; - global $wgUser; - - if ( $wgUser->isAllowed('read') ) { - if ( self::enableBreadCrumbs() ) { - $out->addModules( 'ext.JSBreadCrumbs' ); - } - } - - return true; - } - - /** - * MakeGlobalVariablesScript hook - * - * @param array $vars - * @param OutputPage $outPage - * - * @return bool - */ - public static function addJSVars( $vars, $outPage ) { - global $wgJSBreadCrumbsSeparator, $wgJSBreadCrumbsCookiePath, $wgJSBreadCrumbsCSSSelector, $wgJSBreadCrumbsSkinCSSArray, - $wgJSBreadCrumbsSkinCSSSelector, $wgUser; - - if ( !self::enableBreadCrumbs() ) { - return true; - } - - // Allow localized separator to be overriden - if ( $wgJSBreadCrumbsSeparator !== '' ) { - $separator = $wgJSBreadCrumbsSeparator; - } else { - $separator = wfMessage( "jsbreadcrumbs-separator" )->escaped(); - } - - $variables = array(); - - $skinName = $outPage->getSkin()->getSkinName(); - if(array_key_exists($skinName, $wgJSBreadCrumbsSkinCSSArray)) { - $outPage->addJsConfigVars('wgJSBreadCrumbsCSSSelector', $wgJSBreadCrumbsSkinCSSArray[$skinName]); - } else { - $outPage->addJsConfigVars('wgJSBreadCrumbsCSSSelector', $wgJSBreadCrumbsSkinCSSSelector); - } - - $outPage->addJsConfigVars('wgJSBreadCrumbsMaxCrumbs', $wgUser->getOption( "jsbreadcrumbs-numberofcrumbs" )); - $outPage->addJsConfigVars('wgJSBreadCrumbsShowSidebar', $wgUser->getOption( "jsbreadcrumbs-showcrumbssidebar" )); - $outPage->addJsConfigVars('wgJSBreadCrumbsPervasiveWikiFarm', $wgUser->getOption ("jsbreadcrumbs-pervasivewikifarm" )); - $outPage->addJsConfigVars('wgJSBreadCrumbsSeparator', $separator); - $outPage->addJsConfigVars('wgJSBreadCrumbsCookiePath', $wgJSBreadCrumbsCookiePath); - $outPage->addJsConfigVars('wgJSBreadCrumbsLeadingDescription', $wgUser->getOption( "jsbreadcrumbs-leading-description" )); - $outPage->addJsConfigVars('wgJSBreadCrumbsShowSiteName', $wgUser->getOption( "jsbreadcrumbs-showsite" )); - - - global $wgTitle; - if ( self::getDisplayTitle($wgTitle, $displayTitle) ) { - - if(trim( str_replace( ' ', '', strip_tags( $displayTitle ) ) ) != '' ) { - $outPage->addJsConfigVars('wgJSBreadCrumbsPageName', $displayTitle ); - } else { - $outPage->addJsConfigVars('wgJSBreadCrumbsPageName', $wgTitle->getPrefixedText()); - } - } else { - $outPage->addJsConfigVars('wgJSBreadCrumbsPageName', $wgTitle->getPrefixedText()); - } - - $vars = array_merge( $vars, $variables ); - - return true; - } - - /** - * GetPreferences hook - * - * Add module-releated items to the preferences - */ - public static function addPreferences( $user, $defaultPreferences ) { - $defaultPreferences['jsbreadcrumbs-showcrumbs'] = array( - 'type' => 'toggle', - 'label-message' => 'prefs-jsbreadcrumbs-showcrumbs', - 'section' => 'rendering/jsbreadcrumbs', - ); - - $defaultPreferences['jsbreadcrumbs-showcrumbssidebar'] = array( - 'type' => 'toggle', - 'label-message' => 'prefs-jsbreadcrumbs-showcrumbssidebar', - 'section' => 'rendering/jsbreadcrumbs', - ); - - $defaultPreferences['jsbreadcrumbs-showsite'] = array( - 'type' => 'toggle', - 'label-message' => 'prefs-jsbreadcrumbs-showsite', - 'section' => 'rendering/jsbreadcrumbs', - ); - - $defaultPreferences['jsbreadcrumbs-numberofcrumbs'] = array( - 'type' => 'int', - 'min' => 1, - 'max' => 20, - 'section' => 'rendering/jsbreadcrumbs', - 'help' => wfMessage( 'prefs-jsbreadcrumbs-numberofcrumbs-max' )->text(), - 'label-message' => 'prefs-jsbreadcrumbs-numberofcrumbs', - ); - - return true; - } - - public static function getDisplayTitle( Title $title, &$displayTitle ) { - $id = $title->getArticleID(); - - $dbr = wfGetDB( DB_SLAVE ); - $result = $dbr->select( - 'page_props', - array( 'pp_value' ), - array( - 'pp_page' => $id, - 'pp_propname' => 'displaytitle' - ), - __METHOD__ - ); - - if ( $result->numRows() > 0 ) { - $row = $result->fetchRow(); - $displayTitle = $row['pp_value']; - - if($displayTitle == '') { - return false; - } - return true; - } - - return false; - } - - static function enableBreadCrumbs() { - global $wgUser; - - // Ensure we only enable bread crumbs if we are using vector and - // the user has them enabled - if ( $wgUser->getOption( "jsbreadcrumbs-showcrumbs" ) ) { - return true; - } - } -} diff --git a/JSBreadCrumbs.php b/JSBreadCrumbs.php deleted file mode 100644 index 8bb82d7..0000000 --- a/JSBreadCrumbs.php +++ /dev/null @@ -1,72 +0,0 @@ -<?php -# Copyright (C) 2010 Ryan Lane <http://www.mediawiki.org/wiki/User:Ryan_lane> -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -# http://www.gnu.org/copyleft/gpl.html - -//BreadCrumbsSeparator default set via localization -$wgJSBreadCrumbsSeparator = ''; -$wgJSBreadCrumbsCookiePath = '/'; -$wgJSBreadCrumbsCSSSelector = "#top"; -$wgJSBreadCrumbsSkinCSSArray = array ( - "vector" => "#top", - "foreground" => "#mw-js-message" -); - -$wgDefaultUserOptions['jsbreadcrumbs-showcrumbs'] = true; -$wgDefaultUserOptions['jsbreadcrumbs-showcrumbssidebar'] = false; -$wgDefaultUserOptions['jsbreadcrumbs-showsite'] = false; -$wgDefaultUserOptions['jsbreadcrumbs-pervasivewikifarm'] = false; -$wgDefaultUserOptions['jsbreadcrumbs-numberofcrumbs'] = 5; -$wgDefaultUserOptions['jsbreadcrumbs-leading-description'] = "Last Pages Viewed"; - -// Sets Credits -$wgExtensionCredits['other'][] = array( - 'path' => __FILE__, - 'name' => 'JSBreadCrumbs', - 'author' => array( - 'Ryan Lane', - '...' - ), - 'version' => '0.7.0', - 'url' => 'https://www.mediawiki.org/wiki/Extension:JSBreadCrumbs', - 'descriptionmsg' => 'jsbreadcrumbs-desc', - 'license-name' => 'GPL-2.0+' -); - -// Adds Autoload Classes -//$wgAutoloadClasses['JSBreadCrumbsHooks'] = -// dirname( __FILE__ ) . "/JSBreadCrumbs.hooks.php"; - -$wgResourceModules['ext.JSBreadCrumbs'] = array( - 'localBasePath' => __DIR__, - 'remoteExtPath' => 'JSBreadCrumbs', - 'scripts' => array( 'js/BreadCrumbs.js' ), - 'styles' => array( 'css/BreadCrumbs.css' ), - 'dependencies' => array( - 'jquery.cookie', - ) -); - -$wgAutoloadClasses['JSBreadCrumbsHooks'] = - dirname( __FILE__ ) . "/JSBreadCrumbs.hooks.php"; - -// Adds Internationalized Messages -$wgMessagesDirs['JSBreadCrumbs'] = __DIR__ . '/i18n'; - -// Registers Hooks -$wgHooks['BeforePageDisplay'][] = 'JSBreadCrumbsHooks::addResources'; -$wgHooks['MakeGlobalVariablesScript'][] = 'JSBreadCrumbsHooks::addJSVars'; -$wgHooks['GetPreferences'][] = 'JSBreadCrumbsHooks::addPreferences'; diff --git a/extension.json b/extension.json new file mode 100644 index 0000000..f4a0ffc --- /dev/null +++ b/extension.json @@ -0,0 +1,55 @@ +{ + "name": "JSBreadCrumbs", + "version": "0.7.0", + "author": [ + "Ryan Lane", + "..." + ], + "url": "https://www.mediawiki.org/wiki/Extension:JSBreadCrumbs", + "descriptionmsg": "jsbreadcrumbs-desc", + "license-name": "GPL-2.0+", + "type": "other", + "MessagesDirs": { + "JSBreadCrumbs": [ + "i18n" + ] + }, + "AutoloadClasses": { + "JSBreadCrumbsHooks": "includes/JSBreadCrumbsHooks.php" + }, + "ResourceModules": { + "ext.JSBreadCrumbs": { + "scripts": [ + "JSBreadCrumbs.js" + ], + "styles": [ + "JSBreadCrumbs.css" + ], + "dependencies": [ + "jquery.cookie" + ] + } + }, + "ResourceFileModulePaths": { + "localBasePath": "resources", + "remoteExtPath": "JSBreadCrumbs/resources" + }, + "Hooks": { + "BeforePageDisplay": "JSBreadCrumbsHooks::onBeforePageDisplay", + "GetPreferences": "JSBreadCrumbsHooks::onGetPreferences" + }, + "DefaultUserOptions": { + "jsbreadcrumbs-showcrumbs": true, + "jsbreadcrumbs-showcrumbssidebar": false, + "jsbreadcrumbs-numberofcrumbs": 5 + }, + "config": { + "JSBreadCrumbsSeparator": { + "value": "" + }, + "JSBreadCrumbsGlobalMaxCrumbs": { + "value": 20 + } + }, + "manifest_version": 2 +} diff --git a/i18n/en.json b/i18n/en.json index a21c8af..c2ab4f7 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1,15 +1,16 @@ { "@metadata": { "authors": [ - "Ryan Lane" + "Ryan Lane", + "Kevin Forbes", + "Cindy Cicalese" ] }, "jsbreadcrumbs-desc": "Adds a trail of breadcrumbs below the tab bar", "jsbreadcrumbs-separator": "»", - "jsbreadcrumbs-leading-description": "Navigation trail", + "jsbreadcrumbs-intro": "Last $1 Pages Viewed", "prefs-jsbreadcrumbs": "Bread Crumbs", - "prefs-jsbreadcrumbs-showsite": "Include site name in bread crumbs (useful when used across wikis)", "prefs-jsbreadcrumbs-showcrumbs": "Display bread crumbs", - "prefs-jsbreadcrumbs-numberofcrumbs": "Number of bread crumbs", - "prefs-jsbreadcrumbs-numberofcrumbs-max": "Maximum 20 bread crumbs" -} \ No newline at end of file + "prefs-jsbreadcrumbs-showcrumbssidebar": "Display bread crumbs on sidebar rather than at top of page", + "prefs-jsbreadcrumbs-numberofcrumbs": "Maximum number of bread crumbs to display" +} diff --git a/i18n/qqq.json b/i18n/qqq.json index a38ca6b..b68cfc4 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -4,15 +4,15 @@ "Peter17", "Purodha", "Shirayuki", - "Umherirrender" + "Umherirrender", + "Cindy Cicalese" ] }, "jsbreadcrumbs-desc": "{{desc|name=JS Bread Crumbs|url=https://www.mediawiki.org/wiki/Extension:JSBreadCrumbs}}", "jsbreadcrumbs-separator": "{{optional}}", - "jsbreadcrumbs-leading-description": "Used as bread crumb description.\n\nThis message is followed by \":\" and bread crumbs (\"»\"-separated links).", - "prefs-jsbreadcrumbs": "This is the name of the extension JS Bread Crumbs (https://www.mediawiki.org/wiki/Extension:BreadCrumbs). The expression \"Bread Crumbs\" designates a kind of navigation bar (see https://en.wiktionary.org/wiki/breadcrumbs).", - "prefs-jsbreadcrumbs-showsite": "Used as checkbox label in [[Special:Preferences]].", + "jsbreadcrumbs-intro": "Used as bread crumb description.\n\nThis message includes a parameter for the number of crumbs shown and is followed by \":\" and bread crumbs (\"»\"-separated links).", + "prefs-jsbreadcrumbs": "This is the name of the extension JS Bread Crumbs (https://www.mediawiki.org/wiki/Extension:JSBreadCrumbs). The expression \"Bread Crumbs\" designates a kind of navigation bar (see https://en.wiktionary.org/wiki/breadcrumbs).", "prefs-jsbreadcrumbs-showcrumbs": "Used as checkbox label in [[Special:Preferences]].", - "prefs-jsbreadcrumbs-numberofcrumbs": "Used as label for input box in [[Special:Preferences]].\n\nThe help message for the input box is {{msg-mw|prefs-jsbreadcrumbs-numberofcrumbs-max}}.", - "prefs-jsbreadcrumbs-numberofcrumbs-max": "Used as help message for input box in [[Special:Preferences]].\n\nThe input box follows the label {{msg-mw|prefs-jsbreadcrumbs-numberofcrumbs}}." + "prefs-jsbreadcrumbs-showcrumbssidebar": "Used as checkbox label in [[Special:Preferences]].", + "prefs-jsbreadcrumbs-numberofcrumbs": "Used as label for input box in [[Special:Preferences]]." } diff --git a/includes/JSBreadCrumbsHooks.php b/includes/JSBreadCrumbsHooks.php new file mode 100644 index 0000000..3426360 --- /dev/null +++ b/includes/JSBreadCrumbsHooks.php @@ -0,0 +1,106 @@ +<?php + +class JSBreadCrumbsHooks { + + /** + * Implements BeforePageDisplay hook. + * See https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay + * Initializes variables to be passed to JavaScript. + * + * @param OutputPage $output OutputPage object + * @param Skin $skin Skin object that will be used to generate the page + */ + public static function onBeforePageDisplay( $output, $skin ) { + $user = $output->getUser(); + if ( !$user->isAllowed( 'read' ) || + !$user->getOption( "jsbreadcrumbs-showcrumbs" ) ) { + return; + } + + $vars = []; + + $vars['ShowSidebar'] = + (bool)$user->getOption( "jsbreadcrumbs-showcrumbssidebar" ); + $vars['SiteMaxCrumbs'] = $user->getOption( "jsbreadcrumbs-numberofcrumbs" ); + + // Allow localized separator to be overriden + if ( $GLOBALS['wgJSBreadCrumbsSeparator'] !== '' ) { + $separator = $GLOBALS['wgJSBreadCrumbsSeparator']; + } else { + $separator = wfMessage( "jsbreadcrumbs-separator" )->escaped(); + } + $vars['Separator'] = $separator; + + $vars['GlobalMaxCrumbs'] = $GLOBALS['wgJSBreadCrumbsGlobalMaxCrumbs']; + + $vars['LeadingDescription'] = + wfMessage( "jsbreadcrumbs-intro", $vars['SiteMaxCrumbs'] )->plain(); + + $title = $output->getTitle(); + if ( self::getDisplayTitle( $title, $displayTitle ) ) { + $vars['PageName'] = $displayTitle; + } else { + $vars['PageName'] = $title->getPrefixedText(); + } + + $output->addJSConfigVars( 'JSBreadCrumbs', $vars ); + $output->addModules( 'ext.JSBreadCrumbs' ); + } + + /** + * GetPreferences hook + * + * @param User $user User whose preferences are being modified + * @param array &$preferences Preferences description array + */ + public static function onGetPreferences( User $user, array &$preferences ) { + $preferences['jsbreadcrumbs-showcrumbs'] = [ + 'type' => 'toggle', + 'label-message' => 'prefs-jsbreadcrumbs-showcrumbs', + 'section' => 'rendering/jsbreadcrumbs' + ]; + + $preferences['jsbreadcrumbs-showcrumbssidebar'] = [ + 'type' => 'toggle', + 'label-message' => 'prefs-jsbreadcrumbs-showcrumbssidebar', + 'section' => 'rendering/jsbreadcrumbs' + ]; + + $max = $GLOBALS['wgJSBreadCrumbsGlobalMaxCrumbs']; + $preferences['jsbreadcrumbs-numberofcrumbs'] = [ + 'type' => 'int', + 'min' => 1, + 'max' => $max, + 'label-message' => 'prefs-jsbreadcrumbs-numberofcrumbs', + 'section' => 'rendering/jsbreadcrumbs' + ]; + } + + /** + * Get displaytitle page property text. + * + * @since 1.0 + * @param Title $title the Title object for the page + * @param string &$displaytitle to return the display title, if set + * @return boolean true if the page has a displaytitle page property that is + * different from the prefixed page name, false otherwise + */ + private static function getDisplayTitle( Title $title, &$displaytitle ) { + $pagetitle = $title->getPrefixedText(); + // remove fragment + $title = Title::newFromText( $pagetitle ); + if ( $title instanceof Title ) { + $values = PageProps::getInstance()->getProperties( $title, 'displaytitle' ); + $id = $title->getArticleID(); + if ( array_key_exists( $id, $values ) ) { + $value = $values[$id]; + if ( trim( str_replace( ' ', '', strip_tags( $value ) ) ) !== '' && + $value !== $pagetitle ) { + $displaytitle = $value; + return true; + } + } + } + return false; + } +} diff --git a/js/BreadCrumbs.js b/js/BreadCrumbs.js deleted file mode 100644 index e0c0f22..0000000 --- a/js/BreadCrumbs.js +++ /dev/null @@ -1,164 +0,0 @@ -$(document).ready( function() { - // Set defaults if included as a gadget, otherwise they should - // be defined by the extension. - - var maxCrumbs = mw.config.get('wgJSBreadCrumbsMaxCrumbs'); - var separator = mw.config.get('wgJSBreadCrumbsSeparator'); - var cookiePath = mw.config.get('wgJSBreadCrumbsCookiePath'); - var leadingDescription = mw.config.get('wgJSBreadCrumbsLeadingDescription'); - var cssSelector = mw.config.get('wgJSBreadCrumbsCSSSelector'); - var showSiteName = mw.config.get('wgJSBreadCrumbsShowSiteName'); - var showSidebar = mw.config.get('wgJSBreadCrumbsShowSidebar'); - var specialPageName = mw.config.get('wgCanonicalSpecialPageName'); - var crumbsPageName = mw.config.get('wgJSBreadCrumbsPageName'); - var siteName = mw.config.get('wgSiteName'); - var pervasiveWikiFarm = mw.config.get('wgJSBreadCrumbsPervasiveWikiFarm'); - var cookieNameSuffix = "-" + siteName; - - if(endsWith(crumbsPageName, "Badtitle")) { - } - if ( typeof maxCrumbs === "undefined" ) { - maxCrumbs = 5; - } - if ( typeof separator === "undefined" ) { - separator = "»"; - } - if ( typeof cookiePath === "undefined" ) { - cookiePath = "/"; - } - if ( typeof leadingDescription === "undefined" ) { - leadingDescription = "Navigation trail"; - } - if ( typeof cssSelector === "undefined" ) { - cssSelector = "#top"; - } - if ( typeof showSiteName === "undefined" ) { - showSiteName = false; - } - if ( typeof showSidebar === "undefined" ) { - showSidebar = false; - } - if ( typeof pervasiveWikiFarm === "undefined" ) { - pervasiveWikiFarm = false; - } - - if(pervasiveWikiFarm) { - cookieNameSuffix = ""; - } - - if ( specialPageName === "Userlogout" ) { - $.cookie( 'mwext-bc-title' + cookieNameSuffix, '', { path: cookiePath } ); - $.cookie( 'mwext-bc-url' + cookieNameSuffix, '', { path: cookiePath } ); - $.cookie( 'mwext-bc-site' + cookieNameSuffix, '', { path: cookiePath } ); - } - // Get the breadcrumbs from the cookies - var titleState = ( $.cookie( 'mwext-bc-title' + cookieNameSuffix) || "" ).split( '|' ); - var urlState = ( $.cookie( 'mwext-bc-url' + cookieNameSuffix) || "" ).split( '|' ); - var siteState = ( $.cookie( 'mwext-bc-site' + cookieNameSuffix) || "" ).split( '|' ); - - // Strip the first title/url if it is empty - if ( titleState[0].length === 0 ) { - titleState.splice( 0, 1 ); - urlState.splice( 0, 1 ); - siteState.splice( 0, 1 ); - } - - // Get the full title - var title = crumbsPageName; - - // Remove duplicates - /* var matchTitleIndex = $.inArray( title, titleState ); */ - var matchUrlIndex = $.inArray( location.pathname + location.search, urlState ); - /*if ( matchTitleIndex != -1 && ( matchUrlIndex == matchTitleIndex ) ) { - titleState.splice( matchTitleIndex, 1 ); - urlState.splice( matchTitleIndex, 1 ); - siteState.splice( matchTitleIndex, 1 ); - }*/ - if ( matchUrlIndex !== -1 ) { - titleState.splice( matchUrlIndex, 1 ); - urlState.splice( matchUrlIndex, 1 ); - siteState.splice( matchUrlIndex, 1 ); - } - - // Add the current page - if(!endsWith(crumbsPageName, "Badtitle")) { - titleState.push( title ); - urlState.push( location.pathname + location.search ); - siteState.push( siteName ); - } - // Ensure we only display the maximum breadcrumbs set - if ( titleState.length > maxCrumbs ) { - titleState = titleState.slice( titleState.length - maxCrumbs ); - urlState = urlState.slice( urlState.length - maxCrumbs ); - siteState = siteState.slice( siteState.length - maxCrumbs ); - } - - var skin = mw.config.get( 'skin' ); - - //Insert SideBar List - if( showSidebar === true ) { - if(skin === "vector") { - postVector(maxCrumbs); - } else if(skin === "modern") { - postOther('#mw_portlets', maxCrumbs); - } else if(skin === "monobook") { - postOther('#column-one', maxCrumbs); - } else if(skin === "cologneblue") { - postOther('#quickbar', maxCrumbs); - } - - for ( var i = titleState.length-1; i >= 0; i-- ) { - if ( showSiteName === true ) { - $("#p-rv-list2").append('<li><a href="' + urlState[i]+'">'+ '(' + siteState[i] + ')' + titleState[i] + '</a></li>'); - } else { - $("#p-rv-list2").append('<li><a href="' + urlState[i]+'">'+ titleState[i]+'</a></li>'); - } - } - } else { - // Insert the span we are going to populate - $( cssSelector ).before( '<span id="mwext-bc" class="noprint plainlinks jsbc-breadcrumbs"></span>' ); - - var mwextbc = $( "#mwext-bc" ); - - var urltoappend; - - // Add the bread crumb description - mwextbc.append( leadingDescription + ': ' ); - - // Add the bread crumbs - for ( var k = 0; k < titleState.length; k++ ) { - if ( showSiteName === true ) { - urltoappend = '<a href="' + urlState[k] + '">' + '(' + siteState[k] + ') ' + titleState[k] + '</a> '; - } else { - urltoappend = '<a href="' + urlState[k] + '">' + titleState[k] + '</a> '; - } - - // Only add the separator if this isn't the last title - if ( k < titleState.length - 1 ) { - urltoappend = urltoappend + separator + ' '; - } - mwextbc.append( urltoappend ); - } - } - // Save the bread crumb states to the cookies - $.cookie( 'mwext-bc-title' + cookieNameSuffix, titleState.join( '|' ), { path: cookiePath, expires: 30 } ); - $.cookie( 'mwext-bc-url' + cookieNameSuffix, urlState.join( '|' ), { path: cookiePath, expires: 30 } ); - $.cookie( 'mwext-bc-site' + cookieNameSuffix, siteState.join( '|' ), { path: cookiePath, expires: 30 } ); -}); - -function postVector(maxCrumbs) { - $("#mw-panel").append("<div class='portal persistent' role='navigation' id='p-rv' aria-labelledby='p-rv-label'></div>"); - $("#p-rv").append("<h3 id='p-rv-label' tabindex='3'><a href='#' aria-haspopup='true' aria-controls='p-rv-list2' role='button' aria-pressed='false' aria-expanded='true'>Last "+ maxCrumbs + " Pages Viewed</a></h3>"); - $("#p-rv").append("<div class='body' style='display: block;'><ul id='p-rv-list2'></ul>"); -} - -function postOther(id, maxCrumbs) { - $(id).append("<div class='portlet' id='p-rv' role='navigation'></div>"); - $("#p-rv").append("<h3>Last " + maxCrumbs+ " Pages Viewed</h3><div class='pBody'><ul id='p-rv-list'></ul></div>"); -} - -function endsWith(str, suffix) { - return str.indexOf(suffix, str.length - suffix.length) !== -1; -} - - diff --git a/css/BreadCrumbs.css b/resources/JSBreadCrumbs.css similarity index 100% rename from css/BreadCrumbs.css rename to resources/JSBreadCrumbs.css diff --git a/resources/JSBreadCrumbs.js b/resources/JSBreadCrumbs.js new file mode 100644 index 0000000..fa3637d --- /dev/null +++ b/resources/JSBreadCrumbs.js @@ -0,0 +1,143 @@ +var jsbreadcrumbs_controller = ( function( mw, $ ) { + 'use strict'; + + return { + + initialize: function() { + + var specialPageName = mw.config.get('wgCanonicalSpecialPageName'); + if ( specialPageName === "Userlogout" ) { + $.cookie( 'mwext-jsbreadcrumbs', null, { path: '/' } ); + return; + } + + var config = mw.config.get( 'JSBreadCrumbs' ); + var showSidebar = config.ShowSidebar; + var siteMaxCrumbs = config.SiteMaxCrumbs; + var globalMaxCrumbs = config.GlobalMaxCrumbs; + var separator = config.Separator; + var leadingDescription = config.LeadingDescription; + var pageName = config.PageName; + + // get the breadcrumbs from the cookie + var breadcrumbs = $.cookie( 'mwext-jsbreadcrumbs' ); + if ( breadcrumbs ) { + try { + breadcrumbs = JSON.parse( breadcrumbs ); + } catch ( e ) { + breadcrumbs = []; + } + } else { + breadcrumbs = []; + } + + // remove this URL from the breadcrumb list if it is already in it + var url = location.pathname + location.search; + var index = 0; + while ( index < breadcrumbs.length ) { + if ( breadcrumbs[index].url == url ) { + breadcrumbs.splice( index, 1 ); + } else { + index++; + } + } + + // add the current URL to the breadcrumb list if it points + // to a valid page + var siteName = mw.config.get( 'wgSiteName' ); + if ( !pageName.endsWith( "Badtitle" ) ) { + breadcrumbs.push( { + url: url, + title: pageName, + siteName: siteName + } ); + } + + // get the list of breadcrumbs to display + var visibleCrumbs = []; + for (index = breadcrumbs.length - 1; index >= 0; index--) { + if ( breadcrumbs[index].siteName === siteName ) { + if ( visibleCrumbs.length < siteMaxCrumbs ) { + var breadcrumb = breadcrumbs[index]; + var link = '<a href="' + breadcrumb.url + '">' + breadcrumb.title + + '</a>'; + visibleCrumbs.push( link ); + } else { + breadcrumbs.splice( index, 1 ); + } + } + } + + // truncate the breadcrumb list if necessary + if ( breadcrumbs.length > globalMaxCrumbs ) { + breadcrumbs = breadcrumbs.slice( breadcrumbs.length - globalMaxCrumbs ); + } + + // save the breadcrumbs to the cookie + $.cookie( 'mwext-jsbreadcrumbs', JSON.stringify( breadcrumbs ), + { path: '/', expires: 30 } ); + + var skin = mw.config.get( 'skin' ); + + // insert breadcrumbs in the sidebar + if( showSidebar === true ) { + if ( skin === "vector" ) { + this.postVector( leadingDescription ); + } else if ( skin === "modern" ) { + this.postOther( '#mw_portlets', leadingDescription ); + } else if ( skin === "monobook" ) { + this.postOther( '#column-one', leadingDescription ); + } else if ( skin === "cologneblue" ) { + this.postOther( '#quickbar', leadingDescription ); + } + + for ( index = 0; index < visibleCrumbs.length; index++ ) { + $("#p-rv-list2").append('<li>' + visibleCrumbs[index] + '</li>'); + } + + // insert breadcrumbs elsewhere + } else { + + var cssSelector; + if ( skin === "foreground" ) { + cssSelector = "#mw-js-message"; + } else { + cssSelector = "#top"; + } + + $( cssSelector ).before( + '<span id="mwext-bc" class="noprint plainlinks jsbc-breadcrumbs"></span>' ); + var mwextbc = $( "#mwext-bc" ); + mwextbc.append( leadingDescription + ': ' ); + for ( index = visibleCrumbs.length - 1; index >= 0; index-- ) { + mwextbc.append( visibleCrumbs[index] ); + if ( index > 0 ) { + mwextbc.append( ' ' + separator + ' ' ); + } + } + } + }, + + postVector: function( leadingDescription ) { + $("#mw-panel").append("<div class='portal persistent' role='navigation' id='p-rv' aria-labelledby='p-rv-label'></div>"); + $("#p-rv").append("<h3 id='p-rv-label' tabindex='3'><a href='#' aria-haspopup='true' aria-controls='p-rv-list2' role='button' aria-pressed='false' aria-expanded='true'>" + leadingDescription + "</a></h3>"); + $("#p-rv").append("<div class='body' style='display: block;'><ul id='p-rv-list2'></ul>"); + }, + + postOther: function(id, leadingDescription ) { + $(id).append("<div class='portlet' id='p-rv' role='navigation'></div>"); + $("#p-rv").append("<h3>" + leadingDescription + "</h3><div class='pBody'><ul id='p-rv-list'></ul></div>"); + } + } +}( mediaWiki, jQuery ) ); + +window.JSBreadCrumbsController = jsbreadcrumbs_controller; + +( function( mw, $ ) { + $( document ) + .ready( function() { + if ( mw.config.exists( 'JSBreadCrumbs' ) ) { + window.JSBreadCrumbsController.initialize(); + } + } ); +}( mediaWiki, jQuery ) ); -- To view, visit https://gerrit.wikimedia.org/r/387966 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If08cc5bfe5b9ecf566e6be8f658d970a666e78e0 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/JSBreadCrumbs Gerrit-Branch: master Gerrit-Owner: Cicalese <ccical...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits