/** * @class * @extends {OO.ui.Element} * * @constructor */ OO.ui.Demo = function OoUiDemo() { // Parent OO.ui.Demo.super.call( this ); // Normalization this.normalizeHash(); // Properties this.stylesheetLinks = this.getStylesheetLinks(); this.mode = this.getCurrentMode(); this.$menu = $( '
' ).text( 'Demo styles failed to load.' ) ); } ); }; /** * Handle mode change events. * * Will load a new page. */ OO.ui.Demo.prototype.onModeChange = function () { var page = this.pageMenu.getSelectedItem().getData(), theme = this.themeSelect.getSelectedItem().getData(), direction = this.directionSelect.getSelectedItem().getData(), graphics = this.graphicsSelect.getSelectedItem().getData(); location.hash = '#' + [ page, theme, graphics, direction ].join( '-' ); }; /** * Get a list of mode factors. * * Factors are a mapping between symbolic names used in the URL hash and internal information used * to act on those symbolic names. * * Factor lists are in URL order: page, theme, graphics, direction. Page contains the symbolic * page name, others contain file suffixes. * * @return {Object[]} List of mode factors, keyed by symbolic name */ OO.ui.Demo.prototype.getFactors = function () { var key, factors = [ {}, {}, {}, {} ]; for ( key in this.constructor.static.pages ) { factors[ 0 ][ key ] = key; } for ( key in this.constructor.static.themes ) { factors[ 1 ][ key ] = this.constructor.static.themes[ key ].fileSuffix; } for ( key in this.constructor.static.graphics ) { factors[ 2 ][ key ] = this.constructor.static.graphics[ key ].fileSuffix; } for ( key in this.constructor.static.directions ) { factors[ 3 ][ key ] = this.constructor.static.directions[ key ].fileSuffix; } return factors; }; /** * Get a list of default factors. * * Factor defaults are in URL order: page, theme, graphics, direction. Each contains a symbolic * factor name which should be used as a fallback when the URL hash is missing or invalid. * * @return {Object[]} List of default factors */ OO.ui.Demo.prototype.getDefaultFactorValues = function () { return [ this.constructor.static.defaultPage, this.constructor.static.defaultTheme, this.constructor.static.defaultGraphics, this.constructor.static.defaultDirection ]; }; /** * Parse the current URL hash into factor values. * * @return {string[]} Factor values in URL order: page, theme, graphics, direction */ OO.ui.Demo.prototype.getCurrentFactorValues = function () { return location.hash.slice( 1 ).split( '-' ); }; /** * Get the current mode. * * Generated from parsed URL hash values. * * @return {Object} List of factor values keyed by factor name */ OO.ui.Demo.prototype.getCurrentMode = function () { var factorValues = this.getCurrentFactorValues(); return { page: factorValues[ 0 ], theme: factorValues[ 1 ], graphics: factorValues[ 2 ], direction: factorValues[ 3 ] }; }; /** * Get link elements for the current mode. * * @return {HTMLElement[]} List of link elements */ OO.ui.Demo.prototype.getStylesheetLinks = function () { var i, len, links, fragments, factors = this.getFactors(), theme = this.getCurrentFactorValues()[ 1 ], suffixes = this.constructor.static.themes[ theme ].additionalSuffixes || [], urls = []; // Translate modes to filename fragments fragments = this.getCurrentFactorValues().map( function ( val, index ) { return factors[ index ][ val ]; } ); // Theme styles urls.push( '../dist/oojs-ui' + fragments.slice( 1 ).join( '' ) + '.css' ); for ( i = 0, len = suffixes.length; i < len; i++ ) { urls.push( '../dist/oojs-ui' + fragments[1] + suffixes[i] + fragments.slice( 2 ).join( '' ) + '.css' ); } // Demo styles urls.push( 'styles/demo' + fragments[ 3 ] + '.css' ); // Add link tags links = urls.map( function ( url ) { var link = document.createElement( 'link' ), $link = $( link ), deferred = $.Deferred(); $link.data( 'load-promise', deferred.promise() ); $link.on( { load: deferred.resolve, error: deferred.reject } ); link.rel = 'stylesheet'; link.href = url; return link; } ); return links; }; /** * Normalize the URL hash. */ OO.ui.Demo.prototype.normalizeHash = function () { var i, len, factorValues, modes = [], factors = this.getFactors(), defaults = this.getDefaultFactorValues(); factorValues = this.getCurrentFactorValues(); for ( i = 0, len = factors.length; i < len; i++ ) { modes[ i ] = factors[ i ][ factorValues[ i ] ] !== undefined ? factorValues[ i ] : defaults[ i ]; } // Update hash location.hash = modes.join( '-' ); }; /** * Destroy demo. */ OO.ui.Demo.prototype.destroy = function () { $( 'body' ).removeClass( 'oo-ui-ltr oo-ui-rtl' ); $( this.stylesheetLinks ).remove(); this.$element.remove(); }; /** * Build a console for interacting with an element. * * @param {OO.ui.Element} item * @param {string} key Variable name for item * @param {string} [item.label=""] * @return {jQuery} Console interface element */ OO.ui.Demo.prototype.buildConsole = function ( item, key ) { var $toggle, $log, $label, $input, $submit, $console, $form, console = window.console; function exec( str ) { var func, ret; /*jshint evil:true */ if ( str.indexOf( 'return' ) !== 0 ) { str = 'return ' + str; } try { func = new Function( key, 'item', str ); ret = { value: func( item, item ) }; } catch ( error ) { ret = { value: undefined, error: error }; } return ret; } function submit() { var val, result, logval; val = $input.val(); $input.val( '' ); $input[ 0 ].focus(); result = exec( val ); logval = String( result.value ); if ( logval === '' ) { logval = '""'; } $log.append( $( '