update method

  1. @override
void update(
  1. String? id,
  2. String? classes,
  3. Map<String, String>? styles,
  4. Map<String, String>? attributes,
  5. Map<String, EventCallback>? events,
)
override

Implementation

@override
void update(
  String? id,
  String? classes,
  Map<String, String>? styles,
  Map<String, String>? attributes,
  Map<String, EventCallback>? events,
) {
  late Set<String> attributesToRemove;

  attributesToRemove = {};
  for (var i = 0; i < node.attributes.length; i++) {
    attributesToRemove.add(node.attributes.item(i)!.name);
  }

  node.clearOrSetAttribute('id', id);
  node.clearOrSetAttribute('class', classes == null || classes.isEmpty ? null : classes);
  node.clearOrSetAttribute(
    'style',
    styles == null || styles.isEmpty ? null : styles.entries.map((e) => '${e.key}: ${e.value}').join('; '),
  );

  if (attributes != null && attributes.isNotEmpty) {
    for (final attr in attributes.entries) {
      if (attr.key == 'value' && node.isHtmlInputElement && (node as web.HTMLInputElement).value != attr.value) {
        if (kVerboseMode) {
          print("Set input value: ${attr.value}");
        }
        (node as web.HTMLInputElement).value = attr.value;
        continue;
      }

      if (attr.key == 'value' && node.isHtmlSelectElement && (node as web.HTMLSelectElement).value != attr.value) {
        if (kVerboseMode) {
          print("Set select value: ${attr.value}");
        }
        (node as web.HTMLSelectElement).value = attr.value;
        continue;
      }

      node.clearOrSetAttribute(attr.key, attr.value);
    }
  }

  attributesToRemove.removeAll(['id', 'class', 'style', ...?attributes?.keys]);
  if (attributesToRemove.isNotEmpty) {
    for (final name in attributesToRemove) {
      node.removeAttribute(name);
      if (kVerboseMode) {
        print("Remove attribute: $name");
      }
    }
  }

  if (events != null && events.isNotEmpty) {
    final prevEventTypes = this.events?.keys.toSet();
    this.events ??= <String, EventBinding>{};
    final dataEvents = this.events!;
    events.forEach((type, fn) {
      prevEventTypes?.remove(type);
      final currentBinding = dataEvents[type];
      if (currentBinding != null) {
        currentBinding.fn = fn;
      } else {
        dataEvents[type] = EventBinding(node, type, fn);
      }
    });
    prevEventTypes?.forEach((type) {
      dataEvents.remove(type)?.clear();
    });
  } else {
    this.events?.forEach((type, binding) {
      binding.clear();
    });
    this.events = null;
  }
}