summaryrefslogtreecommitdiff
path: root/resources/mediawiki.action/mediawiki.action.edit.js
blob: e685ca948d8dac32c05db85e690639f3ce8c2384 (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
( function ( $, mw ) {
	var isReady, toolbar, currentFocused;

	isReady = false;

	toolbar = {
		$toolbar: false,
		buttons: [],
		/**
		 * If you want to add buttons, use
		 * mw.toolbar.addButton( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText );
		 */
		addButton: function () {
			if ( isReady ) {
				toolbar.insertButton.apply( toolbar, arguments );
			} else {
				toolbar.buttons.push( [].slice.call( arguments ) );
			}	
		},
		insertButton: function ( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) {
			var image = $('<img>', {
				width : 23,
				height: 22,
				src   : imageFile,
				alt   : speedTip,
				title : speedTip,
				id    : imageId || '',
				'class': 'mw-toolbar-editbutton'
			} ).click( function () {
				mw.toolbar.insertTags( tagOpen, tagClose, sampleText, selectText );
				return false;
			} );

			toolbar.$toolbar.append( image );
			return true;
		},

		/**
		 * apply tagOpen/tagClose to selection in textarea,
		 * use sampleText instead of selection if there is none.
		 */
		insertTags: function ( tagOpen, tagClose, sampleText, selectText ) {
			if ( currentFocused && currentFocused.length ) {
				currentFocused.textSelection(
					'encapsulateSelection', {
						'pre': tagOpen,
						'peri': sampleText,
						'post': tagClose
					}
				);
			}
		},

		// For backwards compatibility
		init: function () {}
	};

	// Legacy (for compatibility with the code previously in skins/common.edit.js)
	window.addButton = toolbar.addButton;
	window.insertTags = toolbar.insertTags;

	// Explose publicly
	mw.toolbar = toolbar;

	$( document ).ready( function () {
		var buttons, i, c, iframe;

		// currentFocus is used to determine where to insert tags
		currentFocused = $( '#wpTextbox1' );

		// Populate the selector cache for $toolbar 
		toolbar.$toolbar = $( '#toolbar' );

		// Legacy: Merge buttons from mwCustomEditButtons
		buttons = [].concat( toolbar.buttons, window.mwCustomEditButtons );
		for ( i = 0; i < buttons.length; i++ ) {
			if ( $.isArray( buttons[i] ) ) {
				// Passes our button array as arguments
				toolbar.insertButton.apply( toolbar, buttons[i] );
			} else {
				// Legacy mwCustomEditButtons is an object
				c = buttons[i];
				toolbar.insertButton( c.imageFile, c.speedTip, c.tagOpen, 
					c.tagClose, c.sampleText, c.imageId, c.selectText );
			}
		}

		// This causes further calls to addButton to go to insertion directly
		// instead of to the toolbar.buttons queue.
		// It is important that this is after the one and only loop through
		// the the toolbar.buttons queue
		isReady = true;

		// Make sure edit summary does not exceed byte limit
		$( '#wpSummary' ).byteLimit( 250 );

		/**
		 * Restore the edit box scroll state following a preview operation,
		 * and set up a form submission handler to remember this state
		 */
		( function scrollEditBox() {
			var editBox, scrollTop, $editForm;

			editBox = document.getElementById( 'wpTextbox1' );
			scrollTop = document.getElementById( 'wpScrolltop' );
			$editForm = $( '#editform' );
			if ( $editForm.length && editBox && scrollTop ) {
				if ( scrollTop.value ) {
					editBox.scrollTop = scrollTop.value;
				}
				$editForm.submit( function () {
					scrollTop.value = editBox.scrollTop;
				});
			}
		}() );

		$( 'textarea, input:text' ).focus( function () {
			currentFocused = $(this);
		});

		// HACK: make currentFocused work with the usability iframe
		// With proper focus detection support (HTML 5!) this'll be much cleaner
		iframe = $( '.wikiEditor-ui-text iframe' );
		if ( iframe.length > 0 ) {
			$( iframe.get( 0 ).contentWindow.document )
				// for IE
				.add( iframe.get( 0 ).contentWindow.document.body )
				.focus( function () {
					currentFocused = iframe;
				} );
		}
	});

}( jQuery, mediaWiki ) );