summaryrefslogtreecommitdiff
path: root/extensions/SyntaxHighlight_GeSHi/modules/ve-syntaxhighlight/ve.ui.MWSyntaxHighlightInspector.js
blob: 257951aff6d36f737b4941099ca1035a696ffdfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*!
 * VisualEditor UserInterface MWSyntaxHighlightInspector class.
 *
 * @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */

/**
 * MediaWiki syntax highlight inspector.
 *
 * @class
 * @extends ve.ui.MWLiveExtensionInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.MWSyntaxHighlightInspector = function VeUiMWSyntaxHighlightInspector() {
	// Parent constructor
	ve.ui.MWSyntaxHighlightInspector.super.apply( this, arguments );
};

/* Inheritance */

OO.inheritClass( ve.ui.MWSyntaxHighlightInspector, ve.ui.MWLiveExtensionInspector );

/* Static properties */

ve.ui.MWSyntaxHighlightInspector.static.name = 'syntaxhighlight';

ve.ui.MWSyntaxHighlightInspector.static.icon = 'alienextension';

ve.ui.MWSyntaxHighlightInspector.static.size = 'large';

ve.ui.MWSyntaxHighlightInspector.static.title = OO.ui.deferMsg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-title' );

ve.ui.MWSyntaxHighlightInspector.static.modelClasses = [ ve.dm.MWBlockSyntaxHighlightNode, ve.dm.MWInlineSyntaxHighlightNode ];

ve.ui.MWSyntaxHighlightInspector.static.dir = 'ltr';

/* Methods */

/**
 * @inheritdoc
 */
ve.ui.MWSyntaxHighlightInspector.prototype.initialize = function () {
	var languageField, codeField, showLinesField,
		noneMsg = ve.msg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-none' );
	// Parent method
	ve.ui.MWSyntaxHighlightInspector.super.prototype.initialize.call( this );

	this.language = new OO.ui.ComboBoxWidget( {
		menu: {
			filterFromInput: true,
			items: $.map( ve.dm.MWSyntaxHighlightNode.static.getLanguages(), function ( lang ) {
				return new OO.ui.MenuOptionWidget( { data: lang, label: lang || noneMsg } );
			} )
		},
		input: { validate: function ( input ) {
			return ve.dm.MWSyntaxHighlightNode.static.isLanguageSupported( input );
		} }
	} );
	this.language.getInput().connect( this, { change: 'onLanguageInputChange' } );

	this.showLinesCheckbox = new OO.ui.CheckboxInputWidget();

	languageField = new OO.ui.FieldLayout( this.language, {
		align: 'top',
		label: ve.msg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-language' )
	} );
	codeField = new OO.ui.FieldLayout( this.input, {
		align: 'top',
		label: ve.msg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-code' )
	} );
	showLinesField = new OO.ui.FieldLayout( this.showLinesCheckbox, {
		align: 'inline',
		label: ve.msg( 'syntaxhighlight-visualeditor-mwsyntaxhighlightinspector-showlines' )
	} );

	// Initialization
	this.$content.addClass( 've-ui-mwSyntaxHighlightInspector-content' );
	this.form.$element.prepend(
		languageField.$element,
		codeField.$element,
		showLinesField.$element
	);
};

/**
 * Handle input change events
 *
 * @param {string} value New value
 */
ve.ui.MWSyntaxHighlightInspector.prototype.onLanguageInputChange = function () {
	var inspector = this;
	this.language.getInput().isValid().done( function ( valid ) {
		inspector.getActions().setAbilities( { done: valid } );
	} );
};

/**
 * @inheritdoc
 */
ve.ui.MWSyntaxHighlightInspector.prototype.getReadyProcess = function ( data ) {
	return ve.ui.MWSyntaxHighlightInspector.super.prototype.getReadyProcess.call( this, data )
		.next( function () {
			if ( this.language.input.getValue() ) {
				this.input.focus();
			} else {
				this.language.getMenu().toggle( true );
			}
		}, this );
};

/**
 * @inheritdoc
 */
ve.ui.MWSyntaxHighlightInspector.prototype.getSetupProcess = function ( data ) {
	return ve.ui.MWSyntaxHighlightInspector.super.prototype.getSetupProcess.call( this, data )
		.next( function () {
			var attrs = this.selectedNode.getAttribute( 'mw' ).attrs,
				language = attrs.lang || '',
				showLines = attrs.line !== undefined;

			if ( ve.dm.MWSyntaxHighlightNode.static.isLanguageSupported( language ) ) {
				this.language.input.setValue( language );
			}
			this.language.input.on( 'change', this.onChangeHandler );

			this.showLinesCheckbox.setSelected( showLines );
			this.showLinesCheckbox.on( 'change', this.onChangeHandler );
		}, this );
};

/**
 * @inheritdoc
 */
ve.ui.MWSyntaxHighlightInspector.prototype.getTeardownProcess = function ( data ) {
	return ve.ui.MWSyntaxHighlightInspector.super.prototype.getTeardownProcess.call( this, data )
		.first( function () {
			this.language.input.off( 'change', this.onChangeHandler );
			this.showLinesCheckbox.off( 'change', this.onChangeHandler );
		}, this );
};

/**
 * @inheritdoc
 */
ve.ui.MWSyntaxHighlightInspector.prototype.updateMwData = function ( mwData ) {
	var language, showLines;

	// Parent method
	ve.ui.MWSyntaxHighlightInspector.super.prototype.updateMwData.call( this, mwData );

	language = this.language.input.getValue();
	showLines = this.showLinesCheckbox.isSelected();

	mwData.attrs.lang = language || undefined;
	mwData.attrs.line = showLines ? '1' : undefined;
};

/* Registration */

ve.ui.windowFactory.register( ve.ui.MWSyntaxHighlightInspector );