Skip to content
This repository
tree: 6ee4dbaf39
Fetching contributors…

Cannot retrieve contributors at this time

file 521 lines (432 sloc) 17.424 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
this.vichrome ?= {}
g = this.vichrome

class g.Mode
    exit : ->
    enter : ->
    getUseNumPrefix : -> false
    enterInteractiveOpen : (baseCom, opt)->
        dscr = baseCom
        sources = []
        if opt.bookmark
            dscr += " Bookmark"
            sources.push( class : "CandSourceBookmark" )
        if opt.history
            dscr += " History"
            sources.push( class : "CandSourceHistory" )
        if opt.web
            dscr += " Web"
            sources.push( class : "CandSourceWebSuggest" )

        if !opt.bookmark and !opt.history and !opt.web
            if opt.search
                baseCom += " g"
                dscr += " Google Search"
                sources = [
                    { class : "CandSourceGoogleSuggest" }
                ]
            else
                sources = [
                    { class: "CandSourceGoogleSuggest", num: 3, reqPrefix: true }
                    { class: "CandSourceWebSuggest", num: 3 }
                    { class: "CandSourceBookmark", num: 3 }
                    { class: "CandSourceHistory", num: 3 }
                ]

        executer = (new g.CommandExecuter).setDescription(dscr).set(baseCom)
        g.model.enterCommandMode( executer, sources )

    reqOpen : (args) ->
        urls = []
        for arg in args then switch arg
            when "-i" then interactive = true
            when "-b" then bookmark = true
            when "-w" then web = true
            when "-h" then history = true
            when "-g","g" then search = true
            else
                urls.push arg.replace( /%url/g, g.view.getHref() )

        if interactive or bookmark or history or web
            opt =
                bookmark : bookmark
                history : history
                web : web
                search : search
            com = "Open" + urls.join(' ')
            @enterInteractiveOpen( com, opt )
        else if search
            word = ""
            word += "+" + encodeURIComponent(i) for i in urls
            word = word.substr(1)

            url = "http://" + g.model.getSetting("searchEngine") + "/search?gcx=c&sourceid=chrome&ie=UTF-8&q=" + word + "&qscrl=1"
            g.view.open( url, "_self" )

        else
            chrome.extension.sendRequest {command: "ExtendURL", url: urls[0]}, (url) ->
                g.view.open( url, "_self" )

    reqTabOpenNew : (args, times) ->
        words = []
        times = 1 if times > 10 # prevent pluto force attack
        for arg in args then switch arg
            when "-i" then interactive = true
            when "-b" then bookmark = true
            when "-w" then web = true
            when "-h" then history = true
            when "-g","g" then search = true
            else
                words.push arg.replace( /%url/g, g.view.getHref() )

        if interactive or bookmark or history or web
            opt =
                bookmark : bookmark
                history : history
                web : web
                search : search
            com = "TabOpenNew " + words.join(' ')
            @enterInteractiveOpen( com, opt )
        else if search
            word = ""
            word += "+" + i for i in words
            word = word.substr(1)

            url = "http://" + g.model.getSetting("searchEngine") + "/search?gcx=c&sourceid=chrome&ie=UTF-8&q=" + word + "&qscrl=1"
            urls = []
            urls.push url
            chrome.extension.sendRequest {command : "TabOpenNew", args : urls, times : times}, g.handler.onCommandResponse
        else
            chrome.extension.sendRequest {command : "TabOpenNew", args : words, times : times}, g.handler.onCommandResponse

    blur : ->
    reqScrollDown : (args, times) -> g.view.scrollBy( 0, g.model.getSetting("scrollPixelCount") * times )
    reqScrollUp : (args, times) -> g.view.scrollBy( 0, -g.model.getSetting("scrollPixelCount") * times )
    reqScrollLeft : (args, times) -> g.view.scrollBy( -g.model.getSetting("scrollPixelCount") * times, 0 )
    reqScrollRight : (args, times) -> g.view.scrollBy( g.model.getSetting("scrollPixelCount") * times, 0 )
    reqPageHalfDown : (args, times) -> g.view.scrollHalfPage( hor : 0, ver : times )
    reqPageHalfUp : (args, times) -> g.view.scrollHalfPage( hor : 0, ver : -times )
    reqPageDown : (args, times) -> g.view.scrollHalfPage( hor : 0, ver : 2*times )
    reqPageUp : (args, times) -> g.view.scrollHalfPage( hor : 0, ver : -2*times )
    reqGoTop : ->
        g.model.setPageMark()
        g.view.goTop()

    reqGoBottom : ->
        g.model.setPageMark()
        g.view.goBottom()

    reqBackHist : -> g.view.backHist()
    reqForwardHist : -> g.view.forwardHist()
    reqTabReload : -> g.view.reload()
    reqGoSearchModeForward : -> g.model.enterSearchMode( false )
    reqGoSearchModeBackward : -> g.model.enterSearchMode( true )
    reqGoLinkTextSearchMode : -> g.model.enterSearchMode( false, new g.LinkTextSearcher )
    reqGoEmergencyMode : -> g.model.enterEmergencyMode()

    reqBackToPageMark : ->
        # TODO:enable to go any pagemark, not only unnamed.
        g.model.goPageMark()

    reqEscape : ->
        g.view.blurActiveElement()
        g.model.escape()
        @escape?()

    reqGoFMode : (args) ->
        for arg in args then switch arg
            when "--newtab" then newTab = true
            when "--continuous" then continuous = true

        opt = { newTab : newTab, continuous : continuous }
        g.model.enterFMode( opt )


    reqGoCommandMode : (args) ->
        sources = [
            { class : "CandSourceCommand" }
            { class : "CandSourceAlias" }
        ]
        g.model.enterCommandMode( new g.CommandExecuter, sources )


    reqFocusOnFirstInput : ->
        g.model.setPageMark()
        g.view.focusInput( 0 )

    reqTabList : ->
        sources = [
            { class : "CandSourceTabs" }
        ]
        executer = (new g.CommandExecuter)
                   .set("MoveToNextTab")
                   .setDescription("TabList")

        g.model.enterCommandMode( executer, sources )

    reqBarrelRoll : ->
        $(document.body).addClass('vichrome-barrelroll')
        setTimeout( ->
            $(document.body).removeClass('vichrome-barrelroll')
        , 2000 )

    reqHideJimmy : -> $("div#siteNotice").hide()

    reqToggleImageSize : ->
        if document.images.length == 1
            evt = document.createEvent('MouseEvents')
            evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
            document.images[0].dispatchEvent(evt)

    req_ChangeLogLevel : (args) ->
        if not args or args.length < 1 then return

        if g.logLevels[args[0]]?
            g.LOG_LEVEL = g.logLevels[args[0]]
        else
            g.view.setStatusLineText "log level '#{args[0]}' doesn't exist", 2000

    getKeyMapping : -> g.model.getNMap()

class g.NormalMode extends g.Mode
    getName : -> "NormalMode"
    getUseNumPrefix : -> true
    prePostKeyEvent : (key, ctrl, alt, meta) -> true
    escape : ->
        g.model.cancelSearchHighlight()
    enter : ->
    reqNextSearch : -> g.model.goNextSearchResult( false )
    reqPrevSearch : -> g.model.goNextSearchResult( true )

class g.InsertMode extends g.Mode
    getName : -> "InsertMode"
    blur : -> g.model.enterNormalMode()
    getKeyMapping : -> g.model.getIMap()

    prePostKeyEvent : (key, ctrl, alt, meta) ->
        if ctrl or alt or meta then return true
        if g.KeyManager.isNumber(key) or g.KeyManager.isAlphabet(key)
            return false
        true

class g.SearchMode extends g.Mode
    getName : -> "SearchMode"

    init : ( searcher_, backward_, opt_ ) ->
        @opt = opt_ ? {
            wrap : g.model.getSetting("wrapSearch")
            ignoreCase : g.model.getSetting("ignoreCase")
            incSearch : g.model.getSetting("incSearch")
            useMigemo : g.model.getSetting("useMigemo")
            minIncSearch : g.model.getSetting("minIncSearch")
            minMigemoLength : g.model.getSetting("minMigemoLength")
            backward : backward_
        }
        @searcher = searcher_.init( @opt )
        @backward = backward_
        this

    cancelSearch : ->
        g.model.goPageMark()
        @searcher.finalize()
        g.model.enterNormalMode();

    prePostKeyEvent : (key, ctrl, alt, meta) -> true

    escape : -> @cancelSearch()

    enter : ->
        sources = [
            { class : "CandSourceSearchHist" }
        ]

        g.view.setStatusLineText ""
        param =
            sources : sources
            mode : 'Search'
            modeChar : if @backward == true then '?' else '/'
            incSearch : @opt.incSearch
        g.model.openCommandBox( param )

    exit : ->
        g.view.hideCommandFrame()
        window.focus()

    notifyInputUpdated : (msg) ->
        clearTimeout @timerId if @waiting

        @timerId = setTimeout( =>
            g.logger.e "set"
            @searcher.updateInput(msg.word)
            @waiting = false
        , 200 )
        @waiting = true

    notifySearchFixed : (msg) ->
        if @waiting
            clearTimeout @timerId
            @waiting = false

        @searcher.fix( msg.word )
        g.model.setSearcher( this.searcher )
        g.model.enterNormalMode()

    getKeyMapping : -> g.model.getCMap()

class g.CommandMode extends g.Mode
    getName : -> "CommandMode"
    reqExecuteCommand : (req) ->
        try
            @executer.set(req.commandLine).parse().execute()
            g.view.hideStatusLine()
        catch e
            g.view.setStatusLineText("Command Not Found : " + this.executer.get(), 2000);
        g.model.enterNormalMode()

    prePostKeyEvent : (key, ctrl, alt, meta) -> true

    enter : ->
        @executer ?= new g.CommandExecuter
        if @executer.getDescription()?
            g.view.setStatusLineText( @executer.getDescription() )
        else
            g.view.setStatusLineText( @executer.get() )

        param =
            sources : @sources
            mode : 'Command'
            modeChar : ':'
        g.model.openCommandBox( param )

    exit : ->
        g.view.hideCommandFrame()
        window.focus()

    getKeyMapping : -> g.model.getCMap()

    setExecuter : (@executer) -> this
    setSources : (@sources) -> this

class g.EmergencyMode extends g.Mode
    getName : -> "EmergencyMode"

    prePostKeyEvent : (key, ctrl, alt, meta) -> true

    enter : ->
        keyMap = g.model.getEMap()
        text = "Emergency Mode: press "
        for key,mapped of keyMap
            text += key + ", " if mapped == "Escape"
        text = text
               .replace( /</g, "&lt;" )
               .replace( />/g, "&gt;" )
               .replace( /, $/, " " )
        text += "to escape"
        g.view.setStatusLineText( text )

    exit : -> g.view.hideStatusLine()

    blur : (target) ->
        if g.util.isEmbededFlash target
            g.model.enterNormalMode()

    getKeyMapping : -> g.model.getEMap()

class g.FMode extends g.Mode
    getName : -> "FMode"
    setOption : (@opt) -> this

    hit : (i) ->
        primary = false
        target = $(@hints[i].target)

        if target.is('a')
            primary = @opt.newTab
            unless @opt.continuous
                g.model.enterNormalMode()
        else
            target.focus()
            if g.util.isEditable( target.get(0) )
                g.model.enterInsertMode()
            else
                g.model.enterNormalMode()

        g.util.dispatchMouseClickEvent target.get(0), primary, false, false

    isValidKey : (key) ->
        return ( @keys.indexOf( key ) >= 0 && key.length == 1 ) ||
               ( key == 'BS' ) ||
               ( key == 'DEL' )

    searchTarget : ->
        for elem, i in @hints
            if @currentInput == elem.key then return i
        return -1

    highlightCandidate : ->
        # TODO:

    treatNewInput : (key) ->
        if key == "BS" || key == "DEL"
            if @currentInput.length == 0
                g.model.enterNormalMode()
                return
            @currentInput = @currentInput.slice( 0, @currentInput.length - 1 )
        else
            @currentInput += key

        g.view.setStatusLineText( 'f Mode : ' + @currentInput )

        if @currentInput.length < @keyLength
            @updateHints()
            return
        else
            idx = @searchTarget()
            if idx >= 0
                @hit idx
            else
                g.model.enterNormalMode() unless @opt.continuous

            if @opt.continuous
                @currentInput = ""
                g.view.setStatusLineText 'f Mode : '
            else
                g.view.hideStatusLine()

    prePostKeyEvent : (key, ctrl, alt, meta) ->
        if key == "ESC" then return true
        if ctrl or alt or meta then return true

        key = key.toUpperCase() if g.model.getSetting("fModeIgnoreCase")

        if @isValidKey( key )
            event.stopPropagation()
            event.preventDefault()
            @treatNewInput( key )
            return false
        else
            return true

    getKeyLength : (candiNum) ->
        if candiNum == 1 then return 1
        if @keys.length == 1 then return 1
        Math.ceil( Math.log( candiNum ) / Math.log( @keys.length ) )

    updateHints : ->
        @highlightCandidate()
        for hint in @hints
            if hint.key.indexOf( @currentInput ) == 0
                hint.elem.find("span#vichromehintchar").remove()
                for c in @currentInput
                    hint.elem = hint.elem.append( $('<span id="vichromehintchar" />')
                               .css("color", g.model.getSetting("hintColorSelected"))
                               .html(c) )
                for c in hint.key.slice(@currentInput.length)
                    hint.elem = hint.elem.append( $('<span id="vichromehintchar" />')
                               .css("color", g.model.getSetting("hintColor"))
                               .html(c) )
                if not hint.elem.is(':visible')
                    @showFunc.call( hint.elem )
                $(hint.target).addClass('vichrome-fModeTarget')
            else
                #hint.elem.fadeOut(200)
                @hideFunc.call( hint.elem )
                $(hint.target).removeClass('vichrome-fModeTarget')

    createHints : (links) ->
        for elem,i in links
            key=''
            j = @keyLength
            k = i
            while j--
                key = @keys.charAt( k % @keys.length ) + key
                k /= @keys.length

            @hints[i] = {}
            @hints[i].key = key
            @hints[i].target = elem

            $(elem).addClass('vichrome-fModeTarget')

        hintHeight = "" + (g.model.getSetting("hintFontSize") + 4) + "px"
        tmpElem = $( '<span id="vichromehint" />' )
                 .css("height", hintHeight)
                 .css("line-height", hintHeight)
                 .css("font-size", "" + g.model.getSetting("hintFontSize") + "px")
                 .css("background-color", g.model.getSetting("hintBackgroundColor"))
        for hint in @hints
            offset = hint.target._offset_
            top = offset.top - 7
            left = offset.left - 7
            top = 0 if top < 0
            left = 0 if left < 0
            elem = tmpElem.clone()
                   .css("top", top)
                   .css("left", left)
            for c in hint.key
                elem = elem.append( $('<span id="vichromehintchar" />').html(c) ).hide()
            hint.elem = elem
            $('html').append( hint.elem )
            @showFunc.call( hint.elem )

    enter : ->
        @currentInput = ""
        @hints = []
        if g.model.getSetting("useFModeAnimation")
            @showFunc = -> this.fadeIn(200)
            @hideFunc = -> this.fadeOut(200)
        else
            @showFunc = $.fn.show
            @hideFunc = $.fn.hide

        links = $('a:_visible,*:input:_visible,.button:_visible')

        if links.length == 0
            g.view.setStatusLineText( "No visible links found", 2000 )
            setTimeout( ( -> g.model.enterNormalMode() ) , 0 )
            return

        @keys = g.model.getSetting("fModeAvailableKeys")
        @keys = @keys.toUpperCase() if g.model.getSetting("fModeIgnoreCase")
        @keyLength = @getKeyLength( links.length )

        @createHints( links )

        g.view.setStatusLineText 'f Mode : '

    exit : ->
        $('span#vichromehint').remove()
        $('.vichrome-fModeTarget').removeClass('vichrome-fModeTarget')

$.extend( $.expr[':'],
    _visible : (elem) ->
        winLeft = window.pageXOffset
        winTop = window.pageYOffset
        winH = window.innerHeight
        winW = window.innerWidth
        offset = $(elem).offset()

        if winTop > offset.top or winTop + winH < offset.top
            return false
        if winLeft > offset.left or offset.left > winLeft + winW
            return false

        if $.expr[':'].hidden(elem)
            return false
        if $.curCSS(elem, 'visibility') == 'hidden'
            return false

        elem._offset_ = offset # for performance improvement
        true
)
Something went wrong with that request. Please try again.