Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | (function() { |
michael@0 | 2 | "use strict"; |
michael@0 | 3 | |
michael@0 | 4 | CodeMirror.defineOption("foldGutter", false, function(cm, val, old) { |
michael@0 | 5 | if (old && old != CodeMirror.Init) { |
michael@0 | 6 | cm.clearGutter(cm.state.foldGutter.options.gutter); |
michael@0 | 7 | cm.state.foldGutter = null; |
michael@0 | 8 | cm.off("gutterClick", onGutterClick); |
michael@0 | 9 | cm.off("change", onChange); |
michael@0 | 10 | cm.off("viewportChange", onViewportChange); |
michael@0 | 11 | cm.off("fold", onFold); |
michael@0 | 12 | cm.off("unfold", onFold); |
michael@0 | 13 | cm.off("swapDoc", updateInViewport); |
michael@0 | 14 | } |
michael@0 | 15 | if (val) { |
michael@0 | 16 | cm.state.foldGutter = new State(parseOptions(val)); |
michael@0 | 17 | updateInViewport(cm); |
michael@0 | 18 | cm.on("gutterClick", onGutterClick); |
michael@0 | 19 | cm.on("change", onChange); |
michael@0 | 20 | cm.on("viewportChange", onViewportChange); |
michael@0 | 21 | cm.on("fold", onFold); |
michael@0 | 22 | cm.on("unfold", onFold); |
michael@0 | 23 | cm.on("swapDoc", updateInViewport); |
michael@0 | 24 | } |
michael@0 | 25 | }); |
michael@0 | 26 | |
michael@0 | 27 | var Pos = CodeMirror.Pos; |
michael@0 | 28 | |
michael@0 | 29 | function State(options) { |
michael@0 | 30 | this.options = options; |
michael@0 | 31 | this.from = this.to = 0; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | function parseOptions(opts) { |
michael@0 | 35 | if (opts === true) opts = {}; |
michael@0 | 36 | if (opts.gutter == null) opts.gutter = "CodeMirror-foldgutter"; |
michael@0 | 37 | if (opts.indicatorOpen == null) opts.indicatorOpen = "CodeMirror-foldgutter-open"; |
michael@0 | 38 | if (opts.indicatorFolded == null) opts.indicatorFolded = "CodeMirror-foldgutter-folded"; |
michael@0 | 39 | return opts; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function isFolded(cm, line) { |
michael@0 | 43 | var marks = cm.findMarksAt(Pos(line)); |
michael@0 | 44 | for (var i = 0; i < marks.length; ++i) |
michael@0 | 45 | if (marks[i].__isFold && marks[i].find().from.line == line) return true; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | function marker(spec) { |
michael@0 | 49 | if (typeof spec == "string") { |
michael@0 | 50 | var elt = document.createElement("div"); |
michael@0 | 51 | elt.className = spec; |
michael@0 | 52 | return elt; |
michael@0 | 53 | } else { |
michael@0 | 54 | return spec.cloneNode(true); |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | function updateFoldInfo(cm, from, to) { |
michael@0 | 59 | var opts = cm.state.foldGutter.options, cur = from; |
michael@0 | 60 | cm.eachLine(from, to, function(line) { |
michael@0 | 61 | var mark = null; |
michael@0 | 62 | if (isFolded(cm, cur)) { |
michael@0 | 63 | mark = marker(opts.indicatorFolded); |
michael@0 | 64 | } else { |
michael@0 | 65 | var pos = Pos(cur, 0), func = opts.rangeFinder || CodeMirror.fold.auto; |
michael@0 | 66 | var range = func && func(cm, pos); |
michael@0 | 67 | if (range && range.from.line + 1 < range.to.line) |
michael@0 | 68 | mark = marker(opts.indicatorOpen); |
michael@0 | 69 | } |
michael@0 | 70 | cm.setGutterMarker(line, opts.gutter, mark); |
michael@0 | 71 | ++cur; |
michael@0 | 72 | }); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | function updateInViewport(cm) { |
michael@0 | 76 | var vp = cm.getViewport(), state = cm.state.foldGutter; |
michael@0 | 77 | if (!state) return; |
michael@0 | 78 | cm.operation(function() { |
michael@0 | 79 | updateFoldInfo(cm, vp.from, vp.to); |
michael@0 | 80 | }); |
michael@0 | 81 | state.from = vp.from; state.to = vp.to; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | function onGutterClick(cm, line, gutter) { |
michael@0 | 85 | var opts = cm.state.foldGutter.options; |
michael@0 | 86 | if (gutter != opts.gutter) return; |
michael@0 | 87 | cm.foldCode(Pos(line, 0), opts.rangeFinder); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | function onChange(cm) { |
michael@0 | 91 | var state = cm.state.foldGutter, opts = cm.state.foldGutter.options; |
michael@0 | 92 | state.from = state.to = 0; |
michael@0 | 93 | clearTimeout(state.changeUpdate); |
michael@0 | 94 | state.changeUpdate = setTimeout(function() { updateInViewport(cm); }, opts.foldOnChangeTimeSpan || 600); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | function onViewportChange(cm) { |
michael@0 | 98 | var state = cm.state.foldGutter, opts = cm.state.foldGutter.options; |
michael@0 | 99 | clearTimeout(state.changeUpdate); |
michael@0 | 100 | state.changeUpdate = setTimeout(function() { |
michael@0 | 101 | var vp = cm.getViewport(); |
michael@0 | 102 | if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) { |
michael@0 | 103 | updateInViewport(cm); |
michael@0 | 104 | } else { |
michael@0 | 105 | cm.operation(function() { |
michael@0 | 106 | if (vp.from < state.from) { |
michael@0 | 107 | updateFoldInfo(cm, vp.from, state.from); |
michael@0 | 108 | state.from = vp.from; |
michael@0 | 109 | } |
michael@0 | 110 | if (vp.to > state.to) { |
michael@0 | 111 | updateFoldInfo(cm, state.to, vp.to); |
michael@0 | 112 | state.to = vp.to; |
michael@0 | 113 | } |
michael@0 | 114 | }); |
michael@0 | 115 | } |
michael@0 | 116 | }, opts.updateViewportTimeSpan || 400); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | function onFold(cm, from) { |
michael@0 | 120 | var state = cm.state.foldGutter, line = from.line; |
michael@0 | 121 | if (line >= state.from && line < state.to) |
michael@0 | 122 | updateFoldInfo(cm, line, line + 1); |
michael@0 | 123 | } |
michael@0 | 124 | })(); |