diff options
| author | Jez Ng | 2012-07-04 23:58:26 -0700 |
|---|---|---|
| committer | Jez Ng | 2012-07-06 22:31:11 -0700 |
| commit | ed21d9b1abe42c1556f27390476302a1393dedb8 (patch) | |
| tree | 115b7da614543b33e4e60332432faa62f3eb2ac8 | |
| parent | d35e04a39f58d17510b7808b582cd322dac62a7c (diff) | |
| download | vimium-ed21d9b1abe42c1556f27390476302a1393dedb8.tar.bz2 | |
Use more Coffeescript idioms.
| -rw-r--r-- | background_scripts/commands.coffee | 20 | ||||
| -rw-r--r-- | content_scripts/link_hints.coffee | 49 | ||||
| -rw-r--r-- | content_scripts/vimium_frontend.coffee | 2 |
3 files changed, 34 insertions, 37 deletions
diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index 4f6da9d8..f012890c 100644 --- a/background_scripts/commands.coffee +++ b/background_scripts/commands.coffee @@ -1,7 +1,7 @@ Commands = init: -> - for command of commandDescriptions - @addCommand(command, commandDescriptions[command][0], commandDescriptions[command][1]) + for command, description of commandDescriptions + @addCommand(command, description[0], description[1]) availableCommands: {} keyToCommandRegistry: {} @@ -12,7 +12,7 @@ Commands = # - passCountToFunction: true if this command should have any digits which were typed prior to the # command passed to it. This is used to implement e.g. "closing of 3 tabs". addCommand: (command, description, options) -> - if @availableCommands[command] + if command of @availableCommands console.log(command, "is already defined! Check commands.js for duplicates.") return @@ -52,23 +52,23 @@ Commands = for line in lines continue if (line[0] == "\"" || line[0] == "#") - split_line = line.split(/\s+/) + splitLine = line.split(/\s+/) - lineCommand = split_line[0] + lineCommand = splitLine[0] if (lineCommand == "map") - continue if (split_line.length != 3) - key = @normalizeKey(split_line[1]) - vimiumCommand = split_line[2] + continue if (splitLine.length != 3) + key = @normalizeKey(splitLine[1]) + vimiumCommand = splitLine[2] continue unless @availableCommands[vimiumCommand] console.log("Mapping", key, "to", vimiumCommand) @mapKeyToCommand(key, vimiumCommand) else if (lineCommand == "unmap") - continue if (split_line.length != 2) + continue if (splitLine.length != 2) - key = @normalizeKey(split_line[1]) + key = @normalizeKey(splitLine[1]) console.log("Unmapping", key) @unmapKey(key) else if (lineCommand == "unmapAll") diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index 9858882e..533a8775 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -117,8 +117,7 @@ LinkHints = visibleElements = [] # Find all visible clickable elements. - for i in [0...resultSet.snapshotLength] - # for (i = 0, count = resultSet.snapshotLength; i < count; i++) { + for i in [0...resultSet.snapshotLength] by 1 element = resultSet.snapshotItem(i) clientRect = DomUtils.getVisibleClientRect(element, clientRect) if (clientRect != null) @@ -168,16 +167,16 @@ LinkHints = else keyResult = @markerMatcher.matchHintsByKey(event, @hintMarkers) linksMatched = keyResult.linksMatched - delay = (if keyResult.delay? then keyResult.delay else 0) + delay = keyResult.delay ? 0 if (linksMatched.length == 0) @deactivateMode() else if (linksMatched.length == 1) @activateLink(linksMatched[0], delay) else - for i of @hintMarkers - @hideMarker(@hintMarkers[i]) - for i of linksMatched - @showMarker(linksMatched[i], @markerMatcher.hintKeystrokeQueue.length) + for i, marker of @hintMarkers + @hideMarker(marker) + for i, matched of linksMatched + @showMarker(matched, @markerMatcher.hintKeystrokeQueue.length) false # We've handled this key, so prevent propagation. # @@ -289,13 +288,12 @@ alphabetHints = # will be spread evenly throughout the array. # shuffleHints: (hints, characterSetLength) -> - buckets = [] - buckets[i] = [] for i in [0...characterSetLength] - for i in [0...hints.length] - buckets[i % buckets.length].push(hints[i]) + buckets = ([] for i in [0...characterSetLength] by 1) + for hint in hints + buckets[i % buckets.length].push(hint) result = [] - for i in [0...buckets.length] - result = result.concat(buckets[i]) + for bucket in buckets + result = result.concat(bucket) result # @@ -316,7 +314,7 @@ alphabetHints = # Pad the hint string we're returning so that it matches numHintDigits. # Note: the loop body changes hintString.length, so the original length must be cached! hintStringLength = hintString.length - for i in [0...(numHintDigits - hintStringLength)] + for i in [0...(numHintDigits - hintStringLength)] by 1 hintString.unshift(characterSet[0]) hintString.join("") @@ -347,10 +345,10 @@ filterHints = # generateLabelMap: -> labels = document.querySelectorAll("label") - for i in [0...labels.length] - forElement = labels[i].getAttribute("for") + for label in labels + forElement = label.getAttribute("for") if (forElement) - labelText = labels[i].textContent.trim() + labelText = label.textContent.trim() # remove trailing : commonly found in labels if (labelText[labelText.length-1] == ":") labelText = labelText.substr(0, labelText.length-1) @@ -388,8 +386,8 @@ filterHints = getHintMarkers: (visibleElements) -> @generateLabelMap() hintMarkers = [] - for i in [0...visibleElements.length] - marker = hintUtils.createMarkerFor(visibleElements[i]) + for visibleElement, i in visibleElements + marker = hintUtils.createMarkerFor(visibleElement) marker.hintString = @generateHintString(i) linkTextObject = @generateLinkText(marker.clickableItem) marker.linkText = linkTextObject.text @@ -406,9 +404,9 @@ filterHints = if (event.keyCode == keyCodes.enter) # activate the lowest-numbered link hint that is visible - for i in [0...hintMarkers.length] - if (hintMarkers[i].style.display != "none") - return { linksMatched: [ hintMarkers[i] ] } + for marker in hintMarkers + if (marker.style.display != "none") + return { linksMatched: [ marker ] } else if (event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey) # backspace clears hint key queue first, then acts on link text key queue. # if both queues are empty. exit hinting mode @@ -447,8 +445,7 @@ filterHints = linksMatched = [] linkSearchString = @linkTextKeystrokeQueue.join("") - for i in [0...hintMarkers.length] - linkMarker = hintMarkers[i] + for linkMarker in hintMarkers matchedLink = linkMarker.linkText.toLowerCase().indexOf(linkSearchString.toLowerCase()) >= 0 if (!matchedLink) @@ -473,8 +470,8 @@ hintUtils = # spanWrap: (hintString) -> innerHTML = [] - for i in [0...hintString.length] - innerHTML.push("<span class='vimiumReset'>" + hintString[i] + "</span>") + for char in hintString + innerHTML.push("<span class='vimiumReset'>" + char + "</span>") innerHTML.join("") # diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 451b96e2..77c9e296 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -227,7 +227,7 @@ onDOMActivate = (event) -> activatedElement = event.target # activatedElement is different from document.activeElement -- the latter seems to be reserved mostly for # input elements. This mechanism allows us to decide whether to scroll a div or to scroll the whole document. # -scrollActivatedElementBy= (direction, amount) -> +scrollActivatedElementBy = (direction, amount) -> # if this is called before domReady, just use the window scroll function if (!document.body) if (direction == "x") |
