| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview A JavaScript class for walking the page using WebKit |
| 7 * selection. |
| 8 */ |
| 9 |
| 10 |
| 11 goog.provide('cvox.SelectionWalker'); |
| 12 |
| 13 goog.require('cvox.SelectionUtil'); |
| 14 goog.require('cvox.TraverseContent'); |
| 15 |
| 16 /** |
| 17 * @constructor |
| 18 */ |
| 19 cvox.SelectionWalker = function() { |
| 20 this.traverseContent = new cvox.TraverseContent(); |
| 21 this.currentGranularity = 0; |
| 22 }; |
| 23 |
| 24 /** |
| 25 * @type {Object} |
| 26 */ |
| 27 cvox.SelectionWalker.GRANULARITY_LEVELS = new Array('paragraph', |
| 28 'sentence', 'word', 'character'); |
| 29 |
| 30 /** |
| 31 * Decreases the granularity. |
| 32 * @return {boolean} Returns true if the granularity was changed successfully. |
| 33 */ |
| 34 cvox.SelectionWalker.prototype.lessGranular = function() { |
| 35 this.currentGranularity = this.currentGranularity - 1; |
| 36 if (this.currentGranularity < 0) { |
| 37 this.currentGranularity = 0; |
| 38 return false; |
| 39 } |
| 40 return true; |
| 41 }; |
| 42 |
| 43 /** |
| 44 * Increases the granularity. |
| 45 * @return {boolean} Returns true if the granularity was changed successfully. |
| 46 */ |
| 47 cvox.SelectionWalker.prototype.moreGranular = function() { |
| 48 this.currentGranularity = this.currentGranularity + 1; |
| 49 var max = cvox.SelectionWalker.GRANULARITY_LEVELS.length - 1; |
| 50 if (this.currentGranularity > max) { |
| 51 this.currentGranularity = max; |
| 52 return false; |
| 53 } |
| 54 return true; |
| 55 }; |
| 56 |
| 57 /** |
| 58 * Moves selection to the next item. |
| 59 * @return {boolean} Returns true if the selection was moved successfully. |
| 60 */ |
| 61 cvox.SelectionWalker.prototype.next = function() { |
| 62 var status = this.traverseContent.nextElement( |
| 63 cvox.SelectionWalker.GRANULARITY_LEVELS[this.currentGranularity]); |
| 64 return !!status; |
| 65 }; |
| 66 |
| 67 /** |
| 68 * Moves selection to the previous item. |
| 69 * @return {boolean} Returns true if the selection was moved successfully. |
| 70 */ |
| 71 cvox.SelectionWalker.prototype.previous = function() { |
| 72 var status = this.traverseContent.prevElement( |
| 73 cvox.SelectionWalker.GRANULARITY_LEVELS[this.currentGranularity]); |
| 74 return !!status; |
| 75 }; |
| 76 |
| 77 /** |
| 78 * Returns the current granularity setting. |
| 79 * @return {string} The granularity setting. |
| 80 */ |
| 81 cvox.SelectionWalker.prototype.getGranularity = function() { |
| 82 return cvox.SelectionWalker.GRANULARITY_LEVELS[this.currentGranularity]; |
| 83 }; |
| 84 |
| 85 /** |
| 86 * Sets the node that the SelectionWalker should be moving through. |
| 87 * @param {Node} currentNode The node to set the SelectionWalker to. |
| 88 */ |
| 89 cvox.SelectionWalker.prototype.setCurrentNode = function(currentNode) { |
| 90 this.traverseContent = new cvox.TraverseContent(currentNode); |
| 91 this.traverseContent.reset(); |
| 92 }; |
| 93 |
| 94 /** |
| 95 * Returns the current content of the selection. |
| 96 * @return {string} The content of the selection. |
| 97 */ |
| 98 cvox.SelectionWalker.prototype.getCurrentContent = function() { |
| 99 // TODO (dmazzoni, clchen): Implement getting content from virtual navigation |
| 100 // here. |
| 101 return cvox.SelectionUtil.getText(); |
| 102 }; |
| 103 |
| OLD | NEW |