r103571 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103570‎ | r103571 | r103572 >
Date:05:44, 18 November 2011
Author:inez
Status:deferred
Tags:
Comment:
Code cleanup in SurfaceView - no any major changes
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
@@ -7,7 +7,9 @@
88 * @param {es.SurfaceModel} model Surface model to view
99 */
1010 es.SurfaceView = function( $container, model ) {
11 - var _this = this;
 11+ // References for use in closures
 12+ var _this = this,
 13+ $document = $( document );
1214
1315 es.EventEmitter.call( this );
1416
@@ -15,12 +17,15 @@
1618 this.$window = $( window );
1719 this.model = model;
1820 this.selection = new es.Range();
19 - this.previousSelection = null;
 21+
 22+ // Mac uses different mapping for keyboard shortcuts
2023 this.mac = navigator.userAgent.match(/mac/i) ? true : false;
2124
2225 this.model.getDocument().on( 'update', function() {
2326 _this.emit( 'update' );
2427 } );
 28+
 29+ this.previousSelection = null;
2530 this.emitSelect = function() {
2631 if ( _this.previousSelection ) {
2732 if (
@@ -53,65 +58,55 @@
5459 selectingMode: null,
5560 selectedRange: null
5661 };
57 -
 62+
5863 this.cursor = {
5964 $: $( '<div class="es-surfaceView-cursor"></div>' ).appendTo( this.$ ),
6065 interval: null,
6166 initialLeft: null,
6267 initialBias: false
6368 };
 69+
6470 this.keyboard = {
6571 selecting: false,
6672 cursorAnchor: null,
6773 keydownTimeout: null,
68 - keys: {
69 - shift: false,
70 - //control: false,
71 - //command: false,
72 - //alt: false
73 - }
 74+ keys: { shift: false }
7475 };
7576
76 - // References for use in closures
77 - var surfaceView = this,
78 - $document = $( document );
79 -
8077 // MouseDown and DoubleClick on surface
8178 this.$.on( {
8279 'mousedown' : function(e) {
83 - return surfaceView.onMouseDown( e );
 80+ return _this.onMouseDown( e );
8481 }
8582 } );
86 -
 83+
8784 // Hidden input
8885 this.$input = $( '<textarea class="es-surfaceView-textarea" />' )
8986 .prependTo( this.$ )
9087 .on( {
9188 'focus' : function() {
92 - //console.log("focus");
9389 $document.off( '.es-surfaceView' );
9490 $document.on({
9591 'mousemove.es-surfaceView': function(e) {
96 - return surfaceView.onMouseMove( e );
 92+ return _this.onMouseMove( e );
9793 },
9894 'mouseup.es-surfaceView': function(e) {
99 - return surfaceView.onMouseUp( e );
 95+ return _this.onMouseUp( e );
10096 },
10197 'keydown.es-surfaceView': function( e ) {
102 - return surfaceView.onKeyDown( e );
 98+ return _this.onKeyDown( e );
10399 },
104100 'keyup.es-surfaceView': function( e ) {
105 - return surfaceView.onKeyUp( e );
 101+ return _this.onKeyUp( e );
106102 }
107103 });
108104 },
109105 'blur': function( e ) {
110 - //console.log("blur");
111106 $document.off( '.es-surfaceView' );
112 - surfaceView.hideCursor();
 107+ _this.hideCursor();
113108 }
114109 } ).focus();
115 -
 110+
116111 // First render
117112 this.documentView.renderContent();
118113
@@ -122,18 +117,20 @@
123118 };
124119
125120 // Re-render when resizing horizontally
 121+ // TODO: Instead of re-rendering on every single 'resize' event wait till user is done with
 122+ // resizing - can be implemented with setTimeout
126123 this.$window.resize( function() {
127 - surfaceView.hideCursor();
128 - surfaceView.dimensions.height = surfaceView.$window.height();
129 - var width = surfaceView.$.width();
130 - if ( surfaceView.dimensions.width !== width ) {
131 - surfaceView.dimensions.width = width;
132 - surfaceView.documentView.renderContent();
 124+ _this.hideCursor();
 125+ _this.dimensions.height = _this.$window.height();
 126+ var width = _this.$.width();
 127+ if ( _this.dimensions.width !== width ) {
 128+ _this.dimensions.width = width;
 129+ _this.documentView.renderContent();
133130 }
134131 } );
135 -
 132+
136133 this.$window.scroll( function() {
137 - surfaceView.dimensions.scrollTop = surfaceView.$window.scrollTop();
 134+ _this.dimensions.scrollTop = _this.$window.scrollTop();
138135 } );
139136 };
140137
@@ -144,8 +141,6 @@
145142
146143 var offset = this.documentView.getOffsetFromEvent( e );
147144
148 - //console.log('onMouseDown; offset: ' + offset);
149 -
150145 if ( e.originalEvent.detail === 1 ) { // single click
151146 this.mouse.selectingMode = 1; // used in mouseMove handler
152147
@@ -245,23 +240,13 @@
246241 };
247242
248243 es.SurfaceView.prototype.onKeyDown = function( e ) {
249 - var tx;
 244+ this.selection.normalize();
 245+
250246 switch ( e.keyCode ) {
251247 case 16: // Shift
252248 this.keyboard.keys.shift = true;
253249 this.keyboard.selecting = true;
254250 break;
255 - case 17: // Control
256 - //this.keyboard.keys.control = true;
257 - break;
258 - case 18: // Alt
259 - //this.keyboard.keys.alt = true;
260 - break;
261 - case 91: // Left Command in WebKit
262 - case 93: // Right Command in WebKit
263 - case 224: // Command in FireFox
264 - //this.keyboard.keys.command = true;
265 - break;
266251 case 36: // Home
267252 this.moveCursor( 'left', 'line' );
268253 break;
@@ -333,76 +318,10 @@
334319 }
335320 break;
336321 case 8: // Backspace
337 - this.selection.normalize();
338 -
339 - var range;
340 -
341 - if ( this.selection.from === this.selection.to ) {
342 - range = new es.Range(
343 - this.documentView.getModel().getRelativeContentOffset( this.selection.from, -1 ),
344 - this.selection.from
345 - );
346 - } else {
347 - this.documentView.clearSelection();
348 - range = this.selection;
349 - }
350 -
351 - var tx = this.documentView.model.prepareRemoval( range );
352 - this.documentView.model.commit ( tx );
353 - this.selection.from = this.selection.to = range.start;
354 - this.showCursor();
355322 break;
356323 case 46: // Delete
357 - this.selection.normalize();
358 -
359 - var range;
360 -
361 - if ( this.selection.from === this.selection.to ) {
362 - range = new es.Range(
363 - this.documentView.getModel().getRelativeContentOffset( this.selection.from, 1 ),
364 - this.selection.from
365 - );
366 - } else {
367 - this.documentView.clearSelection();
368 - range = this.selection;
369 - }
370 -
371 - var tx = this.documentView.model.prepareRemoval( range );
372 - this.documentView.model.commit ( tx );
373 - this.selection.from = this.selection.to = range.start;
374 - this.showCursor();
375324 break;
376325 case 13: // Enter
377 - if ( this.selection.from === this.selection.to ) {
378 - var node = this.documentView.getNodeFromOffset( this.selection.to, false ).model,
379 - nodeType,
380 - stack = [];
381 -
382 - while ( node ) {
383 - nodeType = node.getElementType();
384 - stack.splice(
385 - stack.length / 2,
386 - 0,
387 - { 'type': '/' + nodeType },
388 - { 'type': nodeType, 'attributes': es.copyObject( node.element.attributes ) }
389 - );
390 - node = node.getParent();
391 - if ( es.DocumentView.splitRules[ nodeType ].self === true ) {
392 - nodeType = node.getElementType();
393 - if ( es.DocumentView.splitRules[ nodeType ].children === true) {
394 - break;
395 - }
396 - }
397 - }
398 -
399 - var tx = this.documentView.model.prepareInsertion( this.selection.to, stack );
400 - this.documentView.model.commit( tx );
401 -
402 - this.selection.from = this.selection.to = this.documentView.getModel().getRelativeContentOffset( this.selection.to, 1 );
403 - this.showCursor();
404 - e.preventDefault();
405 - return false;
406 - }
407326 break;
408327 default: // Insert content (maybe)
409328 if ( this.keyboard.keydownTimeout ) {
@@ -417,17 +336,6 @@
418337 return true;
419338 };
420339
421 -es.SurfaceView.prototype.insertFromInput = function() {
422 - var val = this.$input.val();
423 - this.$input.val( '' );
424 - if ( val.length > 0 ) {
425 - var transaction = this.documentView.model.prepareInsertion( this.selection.to, val.split('') );
426 - this.documentView.model.commit ( transaction );
427 - this.selection.from = this.selection.to += val.length;
428 - this.showCursor();
429 - }
430 -};
431 -
432340 es.SurfaceView.prototype.onKeyUp = function( e ) {
433341 switch ( e.keyCode ) {
434342 case 16: // Shift
@@ -436,36 +344,34 @@
437345 this.keyboard.selecting = false;
438346 }
439347 break;
440 - case 17: // Control
441 - //this.keyboard.keys.control = false;
442 - break;
443 - case 18: // Alt
444 - //this.keyboard.keys.alt = false;
445 - break;
446 - case 91: // Left Command in WebKit
447 - case 93: // Right Command in WebKit
448 - case 224: // Command in FireFox
449 - //this.keyboard.keys.command = false;
450 - break;
451348 default:
452349 break;
453350 }
454351 return true;
455352 };
456353
 354+es.SurfaceView.prototype.insertFromInput = function() {
 355+ var val = this.$input.val();
 356+ this.$input.val( '' );
 357+ if ( val.length > 0 ) {
 358+ var transaction = this.documentView.model.prepareInsertion( this.selection.to, val.split('') );
 359+ this.documentView.model.commit ( transaction );
 360+ this.selection.from = this.selection.to += val.length;
 361+ this.showCursor();
 362+ }
 363+};
 364+
 365+
 366+
457367 /**
458368 * @param {String} direction up | down | left | right
459369 * @param {String} unit char | word | line | node | page
460370 */
461371 es.SurfaceView.prototype.moveCursor = function( direction, unit ) {
462 - //console.log('moveCursor; direction: ' + direction + ', unit: ' + unit);
463 -
464372 if ( direction !== 'up' && direction !== 'down' ) {
465373 this.cursor.initialLeft = null;
466374 }
467375
468 - this.selection.normalize();
469 -
470376 var to;
471377
472378 switch ( direction ) {

Status & tagging log