2013-06-27 23:17:59 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWReferenceSearchWidget class.
|
|
|
|
*
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2013-06-27 23:17:59 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an ve.ui.MWReferenceSearchWidget object.
|
|
|
|
*
|
|
|
|
* @class
|
2013-10-09 20:09:59 +00:00
|
|
|
* @extends OO.ui.SearchWidget
|
2013-06-27 23:17:59 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.ui.Surface} [varname] [description]
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-06-27 23:17:59 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceSearchWidget = function VeUiMWReferenceSearchWidget( surface, config ) {
|
|
|
|
// Configuration intialization
|
2013-09-20 07:09:06 +00:00
|
|
|
config = ve.extendObject( {
|
2013-06-27 23:17:59 +00:00
|
|
|
'placeholder': ve.msg( 'visualeditor-reference-input-placeholder' )
|
2013-09-20 07:09:06 +00:00
|
|
|
}, config );
|
2013-06-27 23:17:59 +00:00
|
|
|
|
|
|
|
// Parent constructor
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.SearchWidget.call( this, config );
|
2013-06-27 23:17:59 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.surface = surface;
|
|
|
|
this.index = [];
|
|
|
|
|
|
|
|
// Initialization
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.addClass( 've-ui-mwReferenceSearchWidget' );
|
2013-06-27 23:17:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.inheritClass( ve.ui.MWReferenceSearchWidget, OO.ui.SearchWidget );
|
2013-06-27 23:17:59 +00:00
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event select
|
|
|
|
* @param {Object|string|null} data Reference node attributes, command string (e.g. 'create') or
|
2013-07-25 02:02:50 +00:00
|
|
|
* null if no item is selected
|
2013-06-27 23:17:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2013-07-17 19:09:30 +00:00
|
|
|
* Handle query change events.
|
2013-06-27 23:17:59 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} value New value
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceSearchWidget.prototype.onQueryChange = function () {
|
|
|
|
// Parent method
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.SearchWidget.prototype.onQueryChange.call( this );
|
2013-06-27 23:17:59 +00:00
|
|
|
|
|
|
|
// Populate
|
|
|
|
this.addResults();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle select widget select events.
|
|
|
|
*
|
|
|
|
* @method
|
2013-10-09 20:09:59 +00:00
|
|
|
* @param {OO.ui.OptionWidget} item Selected item
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires select
|
2013-06-27 23:17:59 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceSearchWidget.prototype.onResultsSelect = function ( item ) {
|
|
|
|
var data;
|
|
|
|
|
|
|
|
if ( item ) {
|
|
|
|
data = item.getData();
|
|
|
|
// Resolve indexed values
|
|
|
|
if ( this.index[data] ) {
|
|
|
|
data = this.index[data].attributes;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit( 'select', data );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a serchable index of references.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceSearchWidget.prototype.buildIndex = function () {
|
|
|
|
var i, iLen, j, jLen, group, groupName, groupNames, view, text, attr, firstNodes, indexOrder,
|
2013-09-25 02:18:36 +00:00
|
|
|
refnode, matches, name, citation,
|
2013-06-27 23:17:59 +00:00
|
|
|
internalList = this.surface.getModel().getDocument().getInternalList(),
|
|
|
|
groups = internalList.getNodeGroups();
|
|
|
|
|
|
|
|
function extractAttrs() {
|
2014-02-04 16:05:03 +00:00
|
|
|
text += ' ' + this.getAttribute( 'href' );
|
2013-06-27 23:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.index = [];
|
|
|
|
groupNames = ve.getObjectKeys( groups ).sort();
|
|
|
|
|
|
|
|
for ( i = 0, iLen = groupNames.length; i < iLen; i++ ) {
|
|
|
|
groupName = groupNames[i];
|
|
|
|
if ( groupName.lastIndexOf( 'mwReference/' ) !== 0 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
group = groups[groupName];
|
|
|
|
firstNodes = group.firstNodes;
|
|
|
|
indexOrder = group.indexOrder;
|
|
|
|
for ( j = 0, jLen = indexOrder.length; j < jLen; j++ ) {
|
|
|
|
refnode = firstNodes[indexOrder[j]];
|
2013-07-28 20:51:32 +00:00
|
|
|
attr = ve.copy( refnode.getAttributes() );
|
2013-06-27 23:17:59 +00:00
|
|
|
view = new ve.ce.InternalItemNode( internalList.getItemNode( attr.listIndex ) );
|
|
|
|
|
|
|
|
// HACK: PHP parser doesn't wrap single lines in a paragraph
|
2013-11-01 19:45:59 +00:00
|
|
|
if ( view.$element.children().length === 1 && view.$element.children( 'p' ).length === 1 ) {
|
2013-06-27 23:17:59 +00:00
|
|
|
// unwrap inner
|
2013-11-01 19:45:59 +00:00
|
|
|
view.$element.children().replaceWith( view.$element.children().contents() );
|
2013-06-27 23:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
citation = ( attr.refGroup.length ? attr.refGroup + ' ' : '' ) + ( j + 1 );
|
2013-09-25 02:18:36 +00:00
|
|
|
matches = attr.listKey.match( /^literal\/(.*)$/ );
|
|
|
|
name = matches && matches[1] || '';
|
2013-06-27 23:17:59 +00:00
|
|
|
// Make visible text, citation and reference name searchable
|
2013-11-01 19:45:59 +00:00
|
|
|
text = [ view.$element.text().toLowerCase(), citation, name ].join( ' ' );
|
2013-06-27 23:17:59 +00:00
|
|
|
// Make URLs searchable
|
2013-11-01 19:45:59 +00:00
|
|
|
view.$element.find( 'a[href]' ).each( extractAttrs );
|
2013-06-27 23:17:59 +00:00
|
|
|
|
|
|
|
this.index.push( {
|
2014-02-04 15:51:10 +00:00
|
|
|
'$element': view.$element.clone().show(),
|
2013-06-27 23:17:59 +00:00
|
|
|
'text': text,
|
|
|
|
'attributes': attr,
|
|
|
|
'citation': citation,
|
|
|
|
'name': name
|
|
|
|
} );
|
|
|
|
view.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-populate
|
|
|
|
this.onQueryChange();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle media query response events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceSearchWidget.prototype.addResults = function () {
|
|
|
|
var i, len, item, $citation, $name,
|
|
|
|
value = this.query.getValue(),
|
|
|
|
query = value.toLowerCase(),
|
|
|
|
items = [];
|
|
|
|
|
|
|
|
for ( i = 0, len = this.index.length; i < len; i++ ) {
|
|
|
|
item = this.index[i];
|
|
|
|
if ( item.text.indexOf( query ) >= 0 ) {
|
2013-11-01 19:45:59 +00:00
|
|
|
$citation = this.$( '<div>' )
|
2013-06-27 23:17:59 +00:00
|
|
|
.addClass( 've-ui-mwReferenceSearchWidget-citation' )
|
|
|
|
.text( '[' + item.citation + ']' );
|
2013-11-01 19:45:59 +00:00
|
|
|
$name = this.$( '<div>' )
|
2013-06-27 23:17:59 +00:00
|
|
|
.addClass( 've-ui-mwReferenceSearchWidget-name' )
|
|
|
|
.text( item.name );
|
|
|
|
items.push(
|
|
|
|
new ve.ui.MWReferenceResultWidget( i, {
|
2013-11-01 19:45:59 +00:00
|
|
|
'$': this.$, 'label': $citation.add( $name ).add( item.$element )
|
2013-06-27 23:17:59 +00:00
|
|
|
} )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.results.addItems( items );
|
|
|
|
};
|