]> git.gir.st - VimFx.git/blob - extension/packages/find.coffee
Merge branch 'find' into develop
[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 setFindStr = (document, findStr) ->
20 document = document
21
22 span = document.getElementById "VimFxFindSpan"
23 span.textContent = "/#{ findStr }"
24
25 createFindContainer = (document) ->
26 return utils.parseHTML document, """
27 <div class="VimFxReset" id="#{ CONTAINER_ID }">
28 <span class="VimFxReset" id="VimFxFindSpan">/</span>
29 </div>
30 """
31
32 find = (window, findStr, backwards=false) ->
33 f = ->
34 smartCase = findStr.toLowerCase() != findStr
35
36 return window.find \
37 findStr,
38 smartCase, # Smart case sensitivity
39 backwards, # To avoid getting last search result in the beginning
40 true, # aWrapAround - Doesn't currently work as expected
41 false, # aWholeWord - Not implemented according to MDN
42 true, # aSearchInFrames - Hell yea, search in frames!
43 false # aShowDialog - No dialog please
44
45
46 success = false
47
48 # Perform find only if query string isn't empty to avoid find dialog pop up
49 if findStr.length > 0
50 # Wrap Around is broken... Therefore
51 if not success = f()
52 # If first search attemp has failed then
53 # reset current selection and try again
54 window.getSelection().removeAllRanges()
55 success = f()
56
57 return success
58
59
60 exports.injectFind = injectFind
61 exports.removeFind = removeFind
62 exports.setFindStr = setFindStr
63 exports.find = find
Imprint / Impressum