This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathsettings-panel.js
669 lines (563 loc) · 20.6 KB
/
settings-panel.js
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/** @babel */
import {CompositeDisposable, Disposable, TextEditor} from 'atom'
import _ from 'underscore-plus'
import CollapsibleSectionPanel from './collapsible-section-panel'
import {getSettingDescription} from './rich-description'
const SCOPED_SETTINGS = [
'autoIndent',
'autoIndentOnPaste',
'invisibles',
'nonWordCharacters',
'preferredLineLength',
'scrollPastEnd',
'showIndentGuide',
'showInvisibles',
'softWrap',
'softWrapAtPreferredLineLength',
'softWrapHangingIndent',
'tabLength',
'tabType'
]
export default class SettingsPanel extends CollapsibleSectionPanel {
constructor (options = {}) {
super()
let namespace = options.namespace
this.element = document.createElement('section')
this.element.classList.add('section', 'settings-panel')
this.options = options
this.disposables = new CompositeDisposable()
let settings
if (this.options.scopeName) {
namespace = 'editor'
settings = {}
for (const name of SCOPED_SETTINGS) {
settings[name] = atom.config.get(name, {scope: [this.options.scopeName]})
}
} else {
settings = atom.config.get(namespace)
}
this.element.appendChild(this.elementForSettings(namespace, settings))
this.disposables.add(this.bindInputFields())
this.disposables.add(this.bindSelectFields())
this.disposables.add(this.bindEditors())
this.disposables.add(this.bindTooltips())
this.disposables.add(this.handleEvents())
}
destroy () {
this.disposables.dispose()
this.element.remove()
}
elementForSettings (namespace, settings) {
if (_.isEmpty(settings)) {
return document.createDocumentFragment()
}
let {title} = this.options
const includeTitle = this.options.includeTitle != null ? this.options.includeTitle : true
if (includeTitle) {
if (title == null) {
title = `${_.undasherize(_.uncamelcase(namespace))} Settings`
}
} else {
if (title == null) {
title = 'Settings'
}
}
const icon = this.options.icon != null ? this.options.icon : 'gear'
const {note} = this.options
const sortedSettings = this.sortSettings(namespace, settings)
const container = document.createElement('div')
container.classList.add('section-container')
const heading = document.createElement('div')
heading.classList.add('block', 'section-heading', 'icon', `icon-${icon}`)
heading.textContent = title
container.appendChild(heading)
if (note) {
container.insertAdjacentHTML('beforeend', note)
}
const body = document.createElement('div')
body.classList.add('section-body')
for (const name of sortedSettings) {
body.appendChild(elementForSetting(namespace, name, settings[name]))
}
container.appendChild(body)
return container
}
sortSettings (namespace, settings) {
return sortSettings(namespace, settings)
}
bindInputFields () {
const disposables = Array.from(this.element.querySelectorAll('input[id]')).map((input) => {
let type = input.type
let name = type === 'radio' ? input.name : input.id
this.observe(name, (value) => {
if (type === 'checkbox') {
input.checked = value
} else if (type === 'radio') {
input.checked = (value === this.parseValue(atom.config.getSchema(name).type, input.value))
} else {
if (type === 'color') {
if (value && value.toHexString && value.toHexString()) {
value = value.toHexString()
}
}
if (value) {
input.value = value
}
}
})
const changeHandler = () => {
let value = input.value
if (type === 'checkbox') {
value = input.checked
} else if (type === 'radio') {
value = this.parseValue(atom.config.getSchema(name).type, value)
} else {
value = this.parseValue(type, value)
}
if (type === 'color') {
// This is debounced since the color wheel fires lots of events
// as you are dragging it around
clearTimeout(this.colorDebounceTimeout)
this.colorDebounceTimeout = setTimeout(() => { this.set(name, value) }, 100)
} else {
this.set(name, value)
}
}
input.addEventListener('change', changeHandler)
return new Disposable(() => input.removeEventListener('change', changeHandler))
})
return new CompositeDisposable(...disposables)
}
observe (name, callback) {
let params = {sources: [atom.config.getUserConfigPath()]}
if (this.options.scopeName != null) {
params.scope = [this.options.scopeName]
}
this.disposables.add(atom.config.observe(name, params, callback))
}
isDefault (name) {
let params = {sources: [atom.config.getUserConfigPath()]}
if (this.options.scopeName != null) {
params.scope = [this.options.scopeName]
}
let defaultValue = this.getDefault(name)
let value = atom.config.get(name, params)
return (value == null) || (defaultValue === value)
}
getDefault (name) {
let params = {excludeSources: [atom.config.getUserConfigPath()]}
if (this.options.scopeName != null) {
params.scope = [this.options.scopeName]
}
let defaultValue = atom.config.get(name, params)
if (this.options.scopeName != null) {
// If the unscoped default is the same as the scoped default, check the actual config.cson
// to make sure that there isn't a non-default value that is overriding the scoped value
// For example: the default editor.tabLength is 2, but if someone sets it to 4
// the above check still returns 2 and not 4 for a scoped editor.tabLength,
// because it bypasses config.cson.
if (atom.config.get(name, {excludeSources: [atom.config.getUserConfigPath()]}) === defaultValue) {
defaultValue = atom.config.get(name)
}
}
return defaultValue
}
set (name, value) {
if (this.options.scopeName) {
if (value === undefined) {
atom.config.unset(name, {scopeSelector: this.options.scopeName})
return true
} else {
return atom.config.set(name, value, {scopeSelector: this.options.scopeName})
}
} else {
return atom.config.set(name, value)
}
}
setText (editor, name, type, value) {
let stringValue
if (this.isDefault(name)) {
stringValue = ''
} else {
stringValue = this.valueToString(value) || ''
}
if (stringValue === editor.getText() || _.isEqual(value, this.parseValue(type, editor.getText()))) {
return
}
editor.setText(stringValue)
editor.moveToEndOfLine()
}
bindSelectFields () {
const disposables = Array.from(this.element.querySelectorAll('select[id]')).map((select) => {
const name = select.id
this.observe(name, value => { select.value = value })
const changeHandler = () => {
this.set(name, select.value)
}
select.addEventListener('change', changeHandler)
return new Disposable(() => select.removeEventListener('change', changeHandler))
})
return new CompositeDisposable(...disposables)
}
bindEditors () {
const disposables = Array.from(this.element.querySelectorAll('atom-text-editor')).map((editorElement) => {
let editor = editorElement.getModel()
let name = editorElement.id
let type = editorElement.getAttribute('type')
let defaultValue = this.valueToString(this.getDefault(name))
if (defaultValue != null) {
editor.setPlaceholderText(`Default: ${defaultValue}`)
}
const subscriptions = new CompositeDisposable()
const focusHandler = () => {
if (this.isDefault(name)) {
editor.setText(this.valueToString(this.getDefault(name)) || '')
}
}
editorElement.addEventListener('focus', focusHandler)
subscriptions.add(new Disposable(() => editorElement.removeEventListener('focus', focusHandler)))
const blurHandler = () => {
if (this.isDefault(name)) {
editor.setText('')
}
}
editorElement.addEventListener('blur', blurHandler)
subscriptions.add(new Disposable(() => editorElement.removeEventListener('blur', blurHandler)))
this.observe(name, (value) => {
this.setText(editor, name, type, value)
})
subscriptions.add(editor.onDidStopChanging(() => {
const {minimum, maximum} = atom.config.getSchema(name)
const value = this.parseValue(type, editor.getText())
if (minimum != null && value < minimum) {
this.set(name, minimum)
this.setText(editor, name, type, minimum)
} else if (maximum != null && value > maximum) {
this.set(name, maximum)
this.setText(editor, name, type, maximum)
} else if (!this.set(name, value)) {
this.setText(editor, name, type, atom.config.get(name))
}
}))
return subscriptions
})
return new CompositeDisposable(...disposables)
}
bindTooltips () {
const disposables = Array.from(this.element.querySelectorAll('input[id], select[id], atom-text-editor[id]')).map((element) => {
const schema = atom.config.getSchema(element.id)
let defaultValue = this.valueToString(this.getDefault(element.id))
if (defaultValue != null) {
if (schema.enum && _.findWhere(schema.enum, {value: defaultValue})) {
defaultValue = _.findWhere(schema.enum, {value: defaultValue}).description
}
return atom.tooltips.add(element, {
title: `Default: ${defaultValue}`,
delay: {show: 100},
placement: 'auto left'
})
} else {
return new Disposable(() => {}) // no-op
}
})
return new CompositeDisposable(...disposables)
}
valueToString (value) {
if (Array.isArray(value)) {
if (value.length === 0) {
return null
}
return value.map((val) => val.toString().replace(/,/g, '\\,')).join(', ')
} else if (value != null) {
return value.toString()
} else {
return null
}
}
parseValue (type, value) {
if (value === '') {
return undefined
} else if (type === 'number') {
let floatValue = parseFloat(value)
if (isNaN(floatValue)) {
return value
} else {
return floatValue
}
} else if (type === 'integer') {
let intValue = parseInt(value)
if (isNaN(intValue)) {
return value
} else {
return intValue
}
} else if (type === 'array') {
let arrayValue = (value || '').split(',')
arrayValue = arrayValue.reduce((values, val) => {
const last = values.length - 1
if (last >= 0 && values[last].endsWith('\\')) {
values[last] = values[last].replace(/\\$/, ',') + val
} else {
values.push(val)
}
return values
}, [])
return arrayValue.filter((val) => val).map((val) => val.trim())
} else {
return value
}
}
}
/*
* Space Pen Helpers
*/
let isEditableArray = function (array) {
for (let item of array) {
if (!_.isString(item)) {
return false
}
}
return true
}
function sortSettings (namespace, settings) {
return _.chain(settings)
.keys()
.sortBy((name) => name)
.sortBy((name) => {
const schema = atom.config.getSchema(`${namespace}.${name}`)
return schema ? schema.order : null
})
.value()
}
function elementForSetting (namespace, name, value) {
if (namespace === 'core') {
if (name === 'themes') { return document.createDocumentFragment() } // Handled in the Themes panel
if (name === 'disabledPackages') { return document.createDocumentFragment() } // Handled in the Packages panel
if (name === 'customFileTypes') { return document.createDocumentFragment() }
if (name === 'uriHandlerRegistration') { return document.createDocumentFragment() } // Handled in the URI Handler panel
}
if (namespace === 'editor') {
// There's no global default for these, they are defined by language packages
if (['commentStart', 'commentEnd', 'increaseIndentPattern', 'decreaseIndentPattern', 'foldEndPattern'].includes(name)) {
return document.createDocumentFragment()
}
}
const controlGroup = document.createElement('div')
controlGroup.classList.add('control-group')
const controls = document.createElement('div')
controls.classList.add('controls')
controlGroup.appendChild(controls)
let schema = atom.config.getSchema(`${namespace}.${name}`)
if (schema && schema.enum) {
controls.appendChild(elementForOptions(namespace, name, value, {radio: schema.radio}))
} else if (schema && schema.type === 'color') {
controls.appendChild(elementForColor(namespace, name, value))
} else if (_.isBoolean(value) || (schema && schema.type === 'boolean')) {
controls.appendChild(elementForCheckbox(namespace, name, value))
} else if (_.isArray(value) || (schema && schema.type === 'array')) {
if (isEditableArray(value)) {
controls.appendChild(elementForArray(namespace, name, value))
}
} else if (_.isObject(value) || (schema && schema.type === 'object')) {
controls.appendChild(elementForObject(namespace, name, value))
} else {
controls.appendChild(elementForEditor(namespace, name, value))
}
return controlGroup
}
function getSettingTitle (keyPath, name) {
if (name == null) {
name = ''
}
const schema = atom.config.getSchema(keyPath)
const title = schema != null ? schema.title : null
return title || _.uncamelcase(name).split('.').map(_.capitalize).join(' ')
}
function elementForOptions (namespace, name, value, {radio = false}) {
let keyPath = `${namespace}.${name}`
let schema = atom.config.getSchema(keyPath)
let options = (schema && schema.enum) ? schema.enum : []
const fragment = document.createDocumentFragment()
const label = document.createElement('label')
label.classList.add('control-label')
const titleDiv = document.createElement('div')
titleDiv.classList.add('setting-title')
titleDiv.textContent = getSettingTitle(keyPath, name)
label.appendChild(titleDiv)
const descriptionDiv = document.createElement('div')
descriptionDiv.classList.add('setting-description')
descriptionDiv.innerHTML = getSettingDescription(keyPath)
label.appendChild(descriptionDiv)
fragment.appendChild(label)
fragment.appendChild(enumOptions(options, {keyPath, radio}))
return fragment
}
function elementForCheckbox (namespace, name, value) {
let keyPath = `${namespace}.${name}`
const div = document.createElement('div')
div.classList.add('checkbox')
const label = document.createElement('label')
label.for = keyPath
const input = document.createElement('input')
input.id = keyPath
input.type = 'checkbox'
input.classList.add('input-checkbox')
label.appendChild(input)
const titleDiv = document.createElement('div')
titleDiv.classList.add('setting-title')
titleDiv.textContent = getSettingTitle(keyPath, name)
label.appendChild(titleDiv)
div.appendChild(label)
const descriptionDiv = document.createElement('div')
descriptionDiv.classList.add('setting-description')
descriptionDiv.innerHTML = getSettingDescription(keyPath)
div.appendChild(descriptionDiv)
return div
}
function elementForColor (namespace, name, value) {
let keyPath = `${namespace}.${name}`
const div = document.createElement('div')
div.classList.add('color')
const label = document.createElement('label')
label.for = keyPath
const input = document.createElement('input')
input.id = keyPath
input.type = 'color'
label.appendChild(input)
const titleDiv = document.createElement('div')
titleDiv.classList.add('setting-title')
titleDiv.textContent = getSettingTitle(keyPath, name)
label.appendChild(titleDiv)
div.appendChild(label)
const descriptionDiv = document.createElement('div')
descriptionDiv.classList.add('setting-description')
descriptionDiv.innerHTML = getSettingDescription(keyPath)
div.appendChild(descriptionDiv)
return div
}
function elementForEditor (namespace, name, value) {
let keyPath = `${namespace}.${name}`
let type = _.isNumber(value) ? 'number' : 'string'
const fragment = document.createDocumentFragment()
const label = document.createElement('label')
label.classList.add('control-label')
const titleDiv = document.createElement('div')
titleDiv.classList.add('setting-title')
titleDiv.textContent = getSettingTitle(keyPath, name)
label.appendChild(titleDiv)
const descriptionDiv = document.createElement('div')
descriptionDiv.classList.add('setting-description')
descriptionDiv.innerHTML = getSettingDescription(keyPath)
label.appendChild(descriptionDiv)
fragment.appendChild(label)
const controls = document.createElement('div')
controls.classList.add('controls')
const editorContainer = document.createElement('div')
editorContainer.classList.add('editor-container')
const editor = new TextEditor({mini: true})
editor.element.id = keyPath
editor.element.setAttribute('type', type)
editorContainer.appendChild(editor.element)
controls.appendChild(editorContainer)
fragment.appendChild(controls)
return fragment
}
function elementForArray (namespace, name, value) {
let keyPath = `${namespace}.${name}`
const fragment = document.createDocumentFragment()
const label = document.createElement('label')
label.classList.add('control-label')
const titleDiv = document.createElement('div')
titleDiv.classList.add('setting-title')
titleDiv.textContent = getSettingTitle(keyPath, name)
label.appendChild(titleDiv)
const descriptionDiv = document.createElement('div')
descriptionDiv.classList.add('setting-description')
descriptionDiv.innerHTML = getSettingDescription(keyPath)
label.appendChild(descriptionDiv)
fragment.appendChild(label)
const controls = document.createElement('div')
controls.classList.add('controls')
const editorContainer = document.createElement('div')
editorContainer.classList.add('editor-container')
const editor = new TextEditor({mini: true})
editor.element.id = keyPath
editor.element.setAttribute('type', 'array')
editorContainer.appendChild(editor.element)
controls.appendChild(editorContainer)
fragment.appendChild(controls)
return fragment
}
function elementForObject (namespace, name, value) {
if (_.keys(value).length === 0) {
return document.createDocumentFragment()
} else {
let keyPath = `${namespace}.${name}`
let schema = atom.config.getSchema(keyPath)
let isCollapsed = schema.collapsed === true
const section = document.createElement('section')
section.classList.add('sub-section')
if (isCollapsed) {
section.classList.add('collapsed')
}
const h3 = document.createElement('h3')
h3.classList.add('sub-section-heading', 'has-items')
h3.textContent = getSettingTitle(keyPath, name)
section.appendChild(h3)
const descriptionDiv = document.createElement('div')
descriptionDiv.classList.add('setting-description')
descriptionDiv.innerHTML = getSettingDescription(keyPath)
section.appendChild(descriptionDiv)
const div = document.createElement('div')
div.classList.add('sub-section-body')
for (const key of sortSettings(keyPath, value)) {
div.appendChild(elementForSetting(namespace, `${name}.${key}`, value[key]))
}
section.appendChild(div)
return section
}
}
function enumOptions (options, {keyPath, radio}) {
const containerTag = radio ? 'fieldset' : 'select'
const container = document.createElement(containerTag)
container.id = keyPath
const containerClass = radio ? 'input-radio-group' : 'form-control'
container.classList.add(containerClass)
const conversion = radio ? optionToRadio : optionToSelect
const optionElements = options.map(option => conversion(option, keyPath))
for (const optionElement of optionElements) { container.appendChild(optionElement) }
return container
}
function optionToRadio (option, keyPath) {
const button = document.createElement('input')
const label = document.createElement('label')
label.classList.add('input-label')
let value
let description = ''
if (option.hasOwnProperty('value')) {
value = option.value
description = option.description
} else {
value = option
description = option
}
button.classList.add('input-radio')
button.id = `${keyPath}[${value}]`
button.name = keyPath
button.type = 'radio'
button.value = value
label.appendChild(button)
label.appendChild(document.createTextNode(description))
return label
}
function optionToSelect (option, keyPath) {
const optionElement = document.createElement('option')
if (option.hasOwnProperty('value')) {
optionElement.value = option.value
optionElement.textContent = option.description
} else {
optionElement.value = option
optionElement.textContent = option
}
return optionElement
}