Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/dm/metaitems/ve.dm.MWDefaultSortMetaItem.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWDefaultSortMetaItem class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel category default sort meta item.
10 *
11 * @class
12 * @extends ve.dm.MetaItem
13 * @constructor
14 * @param {Object} element Reference to element in meta-linmod
15 */
16 ve.dm.MWDefaultSortMetaItem = function VeDmMWDefaultSortMetaItem() {
17 // Parent constructor
18 ve.dm.MWDefaultSortMetaItem.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.dm.MWDefaultSortMetaItem, ve.dm.MetaItem );
24
25 /* Static Properties */
26
27 ve.dm.MWDefaultSortMetaItem.static.name = 'mwDefaultSort';
28
29 ve.dm.MWDefaultSortMetaItem.static.group = 'mwDefaultSort';
30
31 ve.dm.MWDefaultSortMetaItem.static.matchTagNames = [ 'span' ];
32
33 ve.dm.MWDefaultSortMetaItem.static.matchRdfaTypes = [ 'mw:Transclusion' ];
34
35 ve.dm.MWDefaultSortMetaItem.static.matchFunction = function ( domElement ) {
36 const mwDataJSON = domElement.getAttribute( 'data-mw' ),
37 mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
38 return ve.getProp( mwData, 'parts', '0', 'template', 'target', 'function' ) === 'defaultsort';
39 };
40
41 ve.dm.MWDefaultSortMetaItem.static.toDataElement = function ( domElements ) {
42 const mwDataJSON = domElements[ 0 ].getAttribute( 'data-mw' ),
43 mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {},
44 input = ve.getProp( mwData, 'parts', '0', 'template', 'target', 'wt' );
45 let prefix, sortKey;
46 if ( input ) {
47 prefix = input.split( ':' )[ 0 ];
48 sortKey = input.slice( prefix.length + 1 );
49 }
50 return {
51 type: this.name,
52 attributes: {
53 prefix: prefix,
54 sortkey: sortKey
55 }
56 };
57 };
58
59 ve.dm.MWDefaultSortMetaItem.static.toDomElements = function ( dataElement, doc ) {
60 const prefix = dataElement.attributes.prefix ||
61 mw.config.get( 'wgVisualEditorConfig' ).defaultSortPrefix,
62 sortKey = dataElement.attributes.sortkey || '',
63 mwData = {
64 parts: [
65 {
66 template: {
67 target: {
68 wt: prefix + ':' + sortKey,
69 function: 'defaultsort'
70 }
71 }
72 }
73 ]
74 };
75
76 const span = doc.createElement( 'span' );
77 span.setAttribute( 'typeof', 'mw:Transclusion' );
78 span.setAttribute( 'data-mw', JSON.stringify( mwData ) );
79 return [ span ];
80 };
81
82 /* Registration */
83
84 ve.dm.modelRegistry.register( ve.dm.MWDefaultSortMetaItem );