]> git.gir.st - VimFx.git/blob - extension/packages/find.coffee
Closes #38. Now there is an indication that find yielded no results
[VimFx.git] / extension / packages / find.coffee
1 utils = require 'utils'
2
3 CONTAINER_ID = 'VimFxFindContainer'
4
5 # Create and inserts into DOM find controls and handlers
6 injectFind = (document) ->
7 # Clean up just in case...
8 removeFind document
9
10 container = createFindContainer(document)
11
12 document.documentElement.appendChild container
13
14 # Removes find controls from DOM
15 removeFind = (document) ->
16 if div = document.getElementById CONTAINER_ID
17 document.documentElement.removeChild div
18
19 flashFind = (document, findStr) ->
20 window = document.defaultView
21
22 injectFind document
23 setFindStr document, findStr
24
25 window.setTimeout (-> removeFind document), 1000
26
27 setFindStr = (document, findStr) ->
28 if span = document.getElementById "VimFxFindSpan"
29 span.textContent = "/#{ findStr }"
30
31 createFindContainer = (document) ->
32 return utils.parseHTML document, """
33 <div class="VimFxReset" id="#{ CONTAINER_ID }">
34 <span class="VimFxReset" id="VimFxFindSpan">/</span>
35 </div>
36 """
37
38 find = (window, findStr, backwards=false) ->
39 f = ->
40 smartCase = findStr.toLowerCase() != findStr
41
42 return window.find \
43 findStr,
44 smartCase, # Smart case sensitivity
45 backwards, # To avoid getting last search result in the beginning
46 true, # aWrapAround - Doesn't currently work as expected
47 false, # aWholeWord - Not implemented according to MDN
48 true, # aSearchInFrames - Hell yea, search in frames!
49 false # aShowDialog - No dialog please
50
51
52 success = false
53
54 # Perform find only if query string isn't empty to avoid find dialog pop up
55 if findStr.length > 0
56 # This will change the ::selection css rule
57 addClass window.document.body, "VimFxFindModeBody"
58
59 # Wrap Around is broken... Therefore
60 if not success = f()
61 # If first search attemp has failed then
62 # reset current selection and try again
63 window.getSelection().removeAllRanges()
64 success = f()
65
66 # For now let's rely on the fact that Fiefox doesn't update the selection
67 # if the css fule that governs it is chnaged
68 window.setTimeout (-> removeClass window.document.body, "VimFxFindModeBody"), 1000
69
70 return success
71
72 # Adds a class the the `element.className` trying to keep the whole class string
73 # will formed (without extra spaces at the tails)
74 addClass = (element, klass) ->
75 if element.className?.search klass == -1
76 if element.className
77 element.className += " #{ klass }"
78 else
79 element.className = klass
80
81 # Remove a class from the `element.className`
82 removeClass = (element, klass) ->
83 name = element.className.replace new RegExp("\\s*#{ klass }"), ""
84 element.className = name or null
85
86
87 exports.injectFind = injectFind
88 exports.removeFind = removeFind
89 exports.flashFind = flashFind
90 exports.setFindStr = setFindStr
91 exports.find = find
Imprint / Impressum