mediawiki-extensions-Visual.../demos/trigger/trigger.js
Timo Tijhof 3e9b227a6a Clean up HTML files, Gruntfile and remove index-phantomjs-tmp hack
Consistently:
* Use <!DOCTYPE html>.
* Use lowercase element tags.
* Indent <head> from <html>.
* Use <meta charset="utf-8">.
* Indent <script> and <style> content from open/close tag.
* Put <link> before <script> when in <head> (in ve/test).
* Use .html instead of .php for indexes where PHP is no
  longer used.
* Use the same license header as we use elsewhere (/*! instead
  of /** and no @file)

Gruntfile:
* Include the new .js files in jshint (demos/**/*.js).
* Order buildloader keys in the same order as the directories
  they go to (alphabetically).
* Add missing jshint patterns:
  - .docs/**/*.js
  - build/**/*.js
  - modules/ve-wmf/**/*.js
* Add missing qunit test:
  - qunit.unicodejs
* Add missing watch patterns:
  - .jscs.json
  - qunit.unicodejs

Also:
* Moved relatively large pieces of script into separate files
  so that they are less repeated (though .template) and also
  able to be linted properly.
* Fixed jshint warnings in newly-created trigger.js and demo.js.
* Moved <script> elements already in <body> to bottom of <body>
  (in ve/test and eg-iframe).
* Moved <script> in eg-iframe from <head> to <body>.
* Fixed buildloader grunt task to use a non-\n whitespace match.
  for the start as well, the newline before the placeholder was
  being stripped.
* Removed the (now obsolete) index-phantomjs-tmp hack.

Change-Id: I7c5a371b82f69f367a8e1c11673d2f37868bc931
2013-12-18 07:00:23 +00:00

92 lines
2.3 KiB
JavaScript

/*!
* VisualEditor trigger demo
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
function setTrigger( trigger ) {
var i, len, key,
parts = trigger.split( '+' );
trigger = trigger.toString();
$( '#trigger' ).text( trigger );
for ( i = 0, len = parts.length; i < len; i++ ) {
key = parts[i].replace( '\\', '\\\\' ).replace( '"', '\\"' );
$( '.key[rel="' + key + '"]' ).addClass( 'active' );
}
}
// Initialiation
var i, len, key,
$primary = $( '#primary' ),
$modifiers = $( '#modifiers' ),
$aliases = $( '#aliases' ),
primaryKeys = ve.ui.Trigger.static.primaryKeys,
modifierKeys = ve.ui.Trigger.static.modifierKeys,
keyAliases = ve.ui.Trigger.static.keyAliases;
for ( i = 0, len = modifierKeys.length; i < len; i++ ) {
$modifiers.append(
$( '<li>' ).append(
$( '<span class="key"></span>' )
.text( modifierKeys[i] )
.attr( 'rel', modifierKeys[i] )
)
);
}
for ( i = 0, len = primaryKeys.length; i < len; i++ ) {
$primary.append(
$( '<li>' ).append(
$( '<span class="key"></span>' )
.text( primaryKeys[i] )
.attr( 'rel', primaryKeys[i] )
)
);
}
for ( key in keyAliases ) {
$aliases.append(
$( '<li>' )
.append( $( '<span class="key alias"></span>' ).text( key ) )
.append( '⇢' )
.append( $( '<span class="key"></span>' ).text( keyAliases[key] ) )
);
}
// Events
$( 'body' ).on( {
'keydown': function ( e ) {
$( '.active' ).removeClass( 'active' );
setTrigger( new ve.ui.Trigger( e ) );
e.preventDefault();
}
} );
$( '#primary .key, #modifiers .key' ).on( {
'mousedown': function ( e ) {
var $target = $( e.target );
if ( e.which === 1 ) {
if ( $target.closest( '#primary' ).length ) {
$primary.find( '.active' ).removeClass( 'active' );
}
if ( !$target.hasClass( 'active' ) ) {
$target.addClass( 'active activating' );
}
}
},
'mouseup': function ( e ) {
var parts = [],
$target = $( e.target );
if ( e.which === 1 ) {
if ( $target.hasClass( 'active' ) && !$target.hasClass( 'activating' ) ) {
$target.removeClass( 'active' );
}
$target.removeClass( 'activating' );
$( '.active' ).each( function () {
parts.push( $(this).attr( 'rel' ) );
} );
setTrigger( new ve.ui.Trigger( parts.join( '+' ) ) );
}
}
} );