1 utils = require 'utils'
3 CONTAINER_ID = 'VimFxFindContainer'
5 # Create and inserts into DOM find controls and handlers
6 injectFind = (document) ->
7 # Clean up just in case...
10 container = createFindContainer(document)
12 document.documentElement.appendChild container
14 # Removes find controls from DOM
15 removeFind = (document) ->
16 if div = document.getElementById CONTAINER_ID
17 document.documentElement.removeChild div
19 flashFind = (document, findStr) ->
20 window = document.defaultView
23 setFindStr document, findStr
25 window.setTimeout (-> removeFind document), 1000
27 setFindStr = (document, findStr) ->
28 if span = document.getElementById "VimFxFindSpan"
29 span.textContent = "/#{ findStr }"
31 createFindContainer = (document) ->
32 return utils.parseHTML document, """
33 <div class="VimFxReset" id="#{ CONTAINER_ID }">
34 <span class="VimFxReset" id="VimFxFindSpan">/</span>
38 find = (window, findStr, backwards=false) ->
40 smartCase = findStr.toLowerCase() != 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
54 # Perform find only if query string isn't empty to avoid find dialog pop up
56 # This will change the ::selection css rule
57 addClass window.document.body, "VimFxFindModeBody"
59 # Wrap Around is broken... Therefore
61 # If first search attemp has failed then
62 # reset current selection and try again
63 window.getSelection().removeAllRanges()
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
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
77 element.className += " #{ klass }"
79 element.className = klass
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
87 exports.injectFind = injectFind
88 exports.removeFind = removeFind
89 exports.flashFind = flashFind
90 exports.setFindStr = setFindStr