|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:js_interop'; |
| 6 | + |
| 7 | +import 'package:collection/collection.dart'; |
| 8 | +import 'package:web/web.dart'; |
| 9 | + |
| 10 | +import '../../web_util.dart'; |
| 11 | + |
| 12 | +/// Create a switch widget on [element]. |
| 13 | +/// |
| 14 | +/// A `data-switch-target` is required, this must be a CSS selector for the |
| 15 | +/// element(s) that are affected when this widget is clicked. |
| 16 | +/// |
| 17 | +/// The optional `data-switch-initial-state` property may be used to provide an |
| 18 | +/// initial state. The initial state is used if state can be loaded from |
| 19 | +/// `localStorage` (because there is none). If not provided, initial state is |
| 20 | +/// derived from document state. |
| 21 | +/// |
| 22 | +/// The optional `data-switch-state-id` property may be used to provide an |
| 23 | +/// identifier for the sttae of this switch in `localStorage`. If supplied state |
| 24 | +/// will be sync'ed across windows. |
| 25 | +/// |
| 26 | +/// The optional `data-switch-enabled` property may be used to specify a space |
| 27 | +/// separated list of classes to be applied to `data-switch-target` when the |
| 28 | +/// switch is enabled. |
| 29 | +/// |
| 30 | +/// The optional `data-switch-disabled` property may be used to specify a space |
| 31 | +/// separated list of classes to be applied to `data-switch-target` when the |
| 32 | +/// switch is disabled. |
| 33 | +void create(HTMLElement element, Map<String, String> options) { |
| 34 | + final target = options['target']; |
| 35 | + if (target == null) { |
| 36 | + throw UnsupportedError('data-switch-target required'); |
| 37 | + } |
| 38 | + final initialState_ = options['initial-state']; |
| 39 | + final stateId = options['state-id']; |
| 40 | + final enabledClassList = (options['enabled'] ?? '') |
| 41 | + .split(' ') |
| 42 | + .where((s) => s.isNotEmpty) |
| 43 | + .toSet() |
| 44 | + .toList(); |
| 45 | + final disabledClassList = (options['disabled'] ?? '') |
| 46 | + .split(' ') |
| 47 | + .where((s) => s.isNotEmpty) |
| 48 | + .toSet() |
| 49 | + .toList(); |
| 50 | + |
| 51 | + void applyState(bool enabled) { |
| 52 | + for (final e in document.querySelectorAll(target).toList()) { |
| 53 | + if (e.isA<HTMLElement>()) { |
| 54 | + final element = e as HTMLHtmlElement; |
| 55 | + if (enabled) { |
| 56 | + for (final c in enabledClassList) { |
| 57 | + element.classList.add(c); |
| 58 | + } |
| 59 | + for (final c in disabledClassList) { |
| 60 | + element.classList.remove(c); |
| 61 | + } |
| 62 | + } else { |
| 63 | + for (final c in enabledClassList) { |
| 64 | + element.classList.remove(c); |
| 65 | + } |
| 66 | + for (final c in disabledClassList) { |
| 67 | + element.classList.add(c); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + bool? initialState; |
| 75 | + if (stateId != null) { |
| 76 | + void onStorage(StorageEvent e) { |
| 77 | + if (e.key == 'switch-$stateId' && e.storageArea == window.localStorage) { |
| 78 | + applyState(e.newValue == 'true'); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + window.addEventListener('storage', onStorage.toJS); |
| 83 | + final state = window.localStorage.getItem('switch-$stateId'); |
| 84 | + if (state != null) { |
| 85 | + initialState = state == 'true'; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Set initialState, if present |
| 90 | + if (initialState_ != null) { |
| 91 | + initialState ??= initialState_ == 'true'; |
| 92 | + } |
| 93 | + // If there are no classes to be applied, this is weird, but then we can't |
| 94 | + // infer an initial state. |
| 95 | + if (enabledClassList.isEmpty && disabledClassList.isEmpty) { |
| 96 | + initialState ??= false; |
| 97 | + } |
| 98 | + // Infer initial state from document state, unless loaded from localStorage |
| 99 | + initialState ??= document |
| 100 | + .querySelectorAll(target) |
| 101 | + .toList() |
| 102 | + .where((e) => e.isA<HTMLElement>()) |
| 103 | + .map((e) => e as HTMLElement) |
| 104 | + .every( |
| 105 | + (e) => |
| 106 | + enabledClassList.every((c) => e.classList.contains(c)) && |
| 107 | + disabledClassList.none((c) => e.classList.contains(c)), |
| 108 | + ); |
| 109 | + |
| 110 | + applyState(initialState); |
| 111 | + var state = initialState; |
| 112 | + |
| 113 | + void onClick(MouseEvent e) { |
| 114 | + if (e.defaultPrevented) { |
| 115 | + return; |
| 116 | + } |
| 117 | + e.preventDefault(); |
| 118 | + |
| 119 | + state = !state; |
| 120 | + applyState(state); |
| 121 | + if (stateId != null) { |
| 122 | + window.localStorage.setItem('switch-$stateId', state.toString()); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + element.addEventListener('click', onClick.toJS); |
| 127 | +} |
0 commit comments