|
1 | 1 | /******************************************************************************* |
2 | 2 |
|
3 | 3 | uBlock Origin - a browser extension to block requests. |
4 | | - Copyright (C) 2016 Raymond Hill |
| 4 | + Copyright (C) 2016-present Raymond Hill |
5 | 5 |
|
6 | 6 | This program is free software: you can redistribute it and/or modify |
7 | 7 | it under the terms of the GNU General Public License as published by |
|
19 | 19 | Home: https://github.com/gorhill/uBlock |
20 | 20 | */ |
21 | 21 |
|
22 | | -/* global uDom */ |
| 22 | +/* global CodeMirror, uDom, uBlockDashboard */ |
23 | 23 |
|
24 | 24 | 'use strict'; |
25 | 25 |
|
26 | 26 | /******************************************************************************/ |
27 | 27 |
|
28 | | -(function() { |
| 28 | +(function() { // >>>> Start of private namespace |
29 | 29 |
|
30 | 30 | /******************************************************************************/ |
31 | 31 |
|
32 | | -var messaging = vAPI.messaging; |
33 | | -var cachedData = ''; |
34 | | -var rawAdvancedSettings = uDom.nodeFromId('advancedSettings'); |
| 32 | +let messaging = vAPI.messaging; |
| 33 | +let noopFunc = function(){}; |
| 34 | + |
| 35 | +let beforeHash = ''; |
| 36 | + |
| 37 | +let cmEditor = new CodeMirror( |
| 38 | + document.getElementById('advancedSettings'), |
| 39 | + { |
| 40 | + autofocus: true, |
| 41 | + lineNumbers: true, |
| 42 | + lineWrapping: true, |
| 43 | + styleActiveLine: true |
| 44 | + } |
| 45 | +); |
| 46 | + |
| 47 | +uBlockDashboard.patchCodeMirrorEditor(cmEditor); |
35 | 48 |
|
36 | 49 | /******************************************************************************/ |
37 | 50 |
|
38 | | -var hashFromAdvancedSettings = function(raw) { |
| 51 | +let hashFromAdvancedSettings = function(raw) { |
39 | 52 | return raw.trim().replace(/\s+/g, '|'); |
40 | 53 | }; |
41 | 54 |
|
42 | 55 | /******************************************************************************/ |
43 | 56 |
|
44 | 57 | // This is to give a visual hint that the content of user blacklist has changed. |
45 | 58 |
|
46 | | -var advancedSettingsChanged = (function () { |
47 | | - var timer = null; |
| 59 | +let advancedSettingsChanged = (function () { |
| 60 | + let timer = null; |
48 | 61 |
|
49 | | - var handler = function() { |
| 62 | + let handler = ( ) => { |
50 | 63 | timer = null; |
51 | | - var changed = hashFromAdvancedSettings(rawAdvancedSettings.value) !== cachedData; |
| 64 | + let changed = hashFromAdvancedSettings(cmEditor.getValue()) !== beforeHash; |
52 | 65 | uDom.nodeFromId('advancedSettingsApply').disabled = !changed; |
| 66 | + CodeMirror.commands.save = changed ? applyChanges : noopFunc; |
53 | 67 | }; |
54 | 68 |
|
55 | 69 | return function() { |
56 | | - if ( timer !== null ) { |
57 | | - clearTimeout(timer); |
58 | | - } |
| 70 | + if ( timer !== null ) { clearTimeout(timer); } |
59 | 71 | timer = vAPI.setTimeout(handler, 100); |
60 | 72 | }; |
61 | 73 | })(); |
62 | 74 |
|
| 75 | +cmEditor.on('changes', advancedSettingsChanged); |
| 76 | + |
63 | 77 | /******************************************************************************/ |
64 | 78 |
|
65 | | -function renderAdvancedSettings() { |
66 | | - var onRead = function(raw) { |
67 | | - cachedData = hashFromAdvancedSettings(raw); |
68 | | - var pretty = [], |
| 79 | +let renderAdvancedSettings = function(first) { |
| 80 | + let onRead = function(raw) { |
| 81 | + beforeHash = hashFromAdvancedSettings(raw); |
| 82 | + let pretty = [], |
69 | 83 | whitespaces = ' ', |
70 | 84 | lines = raw.split('\n'), |
71 | | - max = 0, |
72 | | - pos, |
73 | | - i, n = lines.length; |
74 | | - for ( i = 0; i < n; i++ ) { |
75 | | - pos = lines[i].indexOf(' '); |
76 | | - if ( pos > max ) { |
77 | | - max = pos; |
78 | | - } |
| 85 | + max = 0; |
| 86 | + for ( let line of lines ) { |
| 87 | + let pos = line.indexOf(' '); |
| 88 | + if ( pos > max ) { max = pos; } |
| 89 | + } |
| 90 | + for ( let line of lines ) { |
| 91 | + let pos = line.indexOf(' '); |
| 92 | + pretty.push(whitespaces.slice(0, max - pos) + line); |
79 | 93 | } |
80 | | - for ( i = 0; i < n; i++ ) { |
81 | | - pos = lines[i].indexOf(' '); |
82 | | - pretty.push(whitespaces.slice(0, max - pos) + lines[i]); |
| 94 | + cmEditor.setValue(pretty.join('\n') + '\n'); |
| 95 | + if ( first ) { |
| 96 | + cmEditor.clearHistory(); |
83 | 97 | } |
84 | | - rawAdvancedSettings.value = pretty.join('\n') + '\n'; |
85 | 98 | advancedSettingsChanged(); |
86 | | - rawAdvancedSettings.focus(); |
| 99 | + cmEditor.focus(); |
87 | 100 | }; |
88 | 101 | messaging.send('dashboard', { what: 'readHiddenSettings' }, onRead); |
89 | | -} |
| 102 | +}; |
90 | 103 |
|
91 | 104 | /******************************************************************************/ |
92 | 105 |
|
93 | | -var applyChanges = function() { |
| 106 | +let applyChanges = function() { |
94 | 107 | messaging.send( |
95 | 108 | 'dashboard', |
96 | 109 | { |
97 | 110 | what: 'writeHiddenSettings', |
98 | | - content: rawAdvancedSettings.value |
| 111 | + content: cmEditor.getValue() |
99 | 112 | }, |
100 | 113 | renderAdvancedSettings |
101 | 114 | ); |
102 | 115 | }; |
103 | 116 |
|
104 | 117 | /******************************************************************************/ |
105 | 118 |
|
106 | | -// Handle user interaction |
107 | | -uDom('#advancedSettings').on('input', advancedSettingsChanged); |
108 | | -uDom('#advancedSettingsApply').on('click', applyChanges); |
| 119 | +uDom.nodeFromId('advancedSettings').addEventListener( |
| 120 | + 'input', |
| 121 | + advancedSettingsChanged |
| 122 | +); |
| 123 | +uDom.nodeFromId('advancedSettingsApply').addEventListener( |
| 124 | + 'click', |
| 125 | + applyChanges |
| 126 | +); |
109 | 127 |
|
110 | | -renderAdvancedSettings(); |
| 128 | +renderAdvancedSettings(true); |
111 | 129 |
|
112 | 130 | /******************************************************************************/ |
113 | 131 |
|
114 | | -})(); |
| 132 | +})(); // <<<< End of private namespace |
0 commit comments