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