Index: trunk/parsers/wikidom/lib/synth/views/es.SurfaceView.js |
— | — | @@ -33,19 +33,18 @@ |
34 | 34 | 'alt': false |
35 | 35 | } |
36 | 36 | }; |
37 | | - |
38 | | - /* |
39 | | - this.selecting = false; |
40 | | - this.from = this.to = 0; |
41 | | - */ |
42 | | - |
| 37 | + this.selection = { |
| 38 | + 'from': 0, |
| 39 | + 'to': 0 |
| 40 | + }; |
| 41 | + |
43 | 42 | // Cursor |
44 | 43 | this.blinkInterval = null; |
45 | 44 | this.$cursor = $( '<div class="editSurface-cursor"></div>' ).appendTo( this.$ ); |
46 | 45 | |
47 | 46 | // References for use in closures |
48 | 47 | var surfaceView = this, |
49 | | - $document = $(document); |
| 48 | + $document = $( document ); |
50 | 49 | |
51 | 50 | // MouseDown on surface |
52 | 51 | this.$.bind( { |
— | — | @@ -75,7 +74,7 @@ |
76 | 75 | }); |
77 | 76 | }, |
78 | 77 | 'blur': function( e ) { |
79 | | - $document.unbind('.editSurface'); |
| 78 | + $document.unbind( '.editSurface' ); |
80 | 79 | surfaceView.hideCursor(); |
81 | 80 | }, |
82 | 81 | 'cut': function( e ) { |
— | — | @@ -103,56 +102,6 @@ |
104 | 103 | this.documentView.renderContent(); |
105 | 104 | }; |
106 | 105 | |
107 | | -/** |
108 | | - * Shows the cursor in a new position. |
109 | | - * |
110 | | - * @method |
111 | | - * @param position {Position} Position to show the cursor at |
112 | | - * @param offset {Position} Offset to be added to position |
113 | | - */ |
114 | | -es.SurfaceView.prototype.showCursor = function( position, offset ) { |
115 | | - |
116 | | - // TODO: test/reimplement |
117 | | - |
118 | | - if ( position ) { |
119 | | - if ( $.isPlainObject( offset ) ) { |
120 | | - position.left += offset.left; |
121 | | - position.top += offset.top; |
122 | | - position.bottom += offset.top; |
123 | | - } |
124 | | - this.$cursor.css( { |
125 | | - 'left': position.left, |
126 | | - 'top': position.top, |
127 | | - 'height': position.bottom - position.top |
128 | | - } ).show(); |
129 | | - } else { |
130 | | - this.$cursor.show(); |
131 | | - } |
132 | | - |
133 | | - if ( this.blinkInterval ) { |
134 | | - clearInterval( this.blinkInterval ); |
135 | | - } |
136 | | - var $cursor = this.$cursor; |
137 | | - this.blinkInterval = setInterval( function() { |
138 | | - $cursor.$.css( 'display' ) === 'block' ? $cursor.$.hide() : $cursor.$.show(); |
139 | | - }, 500 ); |
140 | | -}; |
141 | | - |
142 | | -/** |
143 | | - * Hides the cursor. |
144 | | - * |
145 | | - * @method |
146 | | - */ |
147 | | -es.SurfaceView.prototype.hideCursor = function() { |
148 | | - |
149 | | - // TODO: test/reimplement |
150 | | - |
151 | | - if( this.blinkInterval ) { |
152 | | - clearInterval( this.blinkInterval ); |
153 | | - } |
154 | | - this.$cursor.hide(); |
155 | | -}; |
156 | | - |
157 | 106 | es.SurfaceView.prototype.onKeyDown = function( e ) { |
158 | 107 | switch ( e.keyCode ) { |
159 | 108 | case 16: // Shift |
— | — | @@ -216,37 +165,43 @@ |
217 | 166 | var contentOffset = this.documentView.getOffsetFromEvent( e ), |
218 | 167 | position = this.documentView.getRenderedPosition( contentOffset ); |
219 | 168 | |
220 | | - if ( position !== null ) { |
| 169 | + if ( e.button === 0 ) { |
| 170 | + if ( this.keyboard.keys.shift ) { |
| 171 | + this.selection.to = contentOffset; |
| 172 | + } else { |
| 173 | + this.selection.from = this.selection.to = contentOffset; |
| 174 | + } |
| 175 | + |
221 | 176 | this.showCursor( position ); |
| 177 | + this.mouse.selecting = true; |
| 178 | + |
| 179 | + this.documentView.drawSelection( new es.Range( this.selection.from, this.selection.to ) ); |
222 | 180 | } |
223 | 181 | |
224 | | - /* |
225 | | - this.from = contentOffset; |
226 | | - this.selecting = true; |
227 | | - |
228 | 182 | if ( !this.$input.is(':focus') ) { |
229 | 183 | this.$input.focus().select(); |
230 | 184 | } |
231 | | - */ |
232 | 185 | |
233 | 186 | return false; |
234 | 187 | }; |
235 | 188 | |
236 | 189 | es.SurfaceView.prototype.onMouseMove = function( e ) { |
237 | | - /* |
238 | | - if (this.selecting ) { |
239 | | - var contentOffset = this.documentView.getOffsetFromEvent( e ); |
240 | | - |
241 | | - this.to = contentOffset; |
242 | | - this.documentView.drawSelection( new es.Range( this.from, this.to ) ); |
| 190 | + if ( e.button === 0 && this.mouse.selecting ) { |
| 191 | + this.hideCursor(); |
| 192 | + this.selection.to = this.documentView.getOffsetFromEvent( e ); |
| 193 | + if ( !this.drawSelection() ) { |
| 194 | + this.showCursor(); |
| 195 | + } |
243 | 196 | } |
244 | | - */ |
245 | | - // TODO: Respond to mouse move event, updating selection while painting |
246 | 197 | }; |
247 | 198 | |
248 | 199 | es.SurfaceView.prototype.onMouseUp = function( e ) { |
249 | | - this.selecting = false; |
250 | | - // TODO: Respond to mouse up event, possibly ending selection painting |
| 200 | + if ( e.button === 0 && this.selection.to ) { |
| 201 | + if ( this.drawSelection() ) { |
| 202 | + this.hideCursor(); |
| 203 | + } |
| 204 | + } |
| 205 | + this.mouse.selecting = false; |
251 | 206 | }; |
252 | 207 | |
253 | 208 | es.SurfaceView.prototype.onCut = function( e ) { |
— | — | @@ -277,6 +232,13 @@ |
278 | 233 | // TODO: Set the value of this.$input |
279 | 234 | }; |
280 | 235 | |
| 236 | +/** |
| 237 | + * Shows the cursor in a new position. |
| 238 | + * |
| 239 | + * @method |
| 240 | + * @param position {Position} Position to show the cursor at |
| 241 | + * @param offset {Position} Offset to be added to position |
| 242 | + */ |
281 | 243 | es.SurfaceView.prototype.showCursor = function( position ) { |
282 | 244 | if ( position ) { |
283 | 245 | this.$cursor.css( { |
— | — | @@ -297,9 +259,23 @@ |
298 | 260 | }, 500, this ); |
299 | 261 | }; |
300 | 262 | |
| 263 | +/** |
| 264 | + * Hides the cursor. |
| 265 | + * |
| 266 | + * @method |
| 267 | + */ |
301 | 268 | es.SurfaceView.prototype.hideCursor = function( position ) { |
302 | 269 | if( this.blinkInterval ) { |
303 | 270 | clearInterval( this.blinkInterval ); |
304 | 271 | } |
305 | 272 | this.$cursor.hide(); |
| 273 | +}; |
| 274 | + |
| 275 | +es.SurfaceView.prototype.drawSelection = function() { |
| 276 | + if ( this.selection.from !== this.selection.to ) { |
| 277 | + this.documentView.drawSelection( new es.Range( this.selection.from, this.selection.to ) ); |
| 278 | + return true; |
| 279 | + } else { |
| 280 | + return false; |
| 281 | + } |
306 | 282 | }; |
\ No newline at end of file |