<commit_before>// Copyright (c) 2013, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

library spark_widgets.overlay;

import 'dart:async';
import 'dart:html';

import 'package:polymer/polymer.dart';

import '../common/spark_widget.dart';

@CustomTag("spark-overlay")
class SparkOverlay extends SparkWidget {
  // Track overlays for z-index and focus managemant.
  // TODO(ussuri): The z-index management with a fixed base z-index is shaky at
  // best. SparkOverlay doesn't know in what z-index environment its instances
  // will live, so assumption that 1000 is always a good value is invalid.
  // Consider:
  // 1) Stacking contexts (https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context).
  // 2) Some way to set the base z-index in the client.
  // 3) Set the same z-index for all spark-overlays in the client, then bump
  //    the current overlay's z-index if any other are currently opened.
  static final List<SparkOverlay> overlays = [];

  static void _trackOverlays(SparkOverlay inOverlay) {
    if (inOverlay.opened) {
      final int z0 = _currentOverlayZ();
      overlays.add(inOverlay);
      final int z1 = _currentOverlayZ();
      if (z0 != null && z1 != null && z1 <= z0) {
        _applyOverlayZ(inOverlay, z0);
      }
    } else {
      final int i = overlays.indexOf(inOverlay);
      if (i >= 0) {
        overlays.removeAt(i);
        _setZ(inOverlay, null);
      }
    }
  }

  static void _applyOverlayZ(SparkOverlay inOverlay, int inAboveZ) {
    _setZ(inOverlay, inAboveZ + 2);
  }

  static void _setZ(Element inNode, int inZ) {
    inNode.style.zIndex = "$inZ";
  }

  static _currentOverlay() {
    return overlays.isNotEmpty ? overlays.last : null;
  }

  // TODO(ussuri): This widget doesn't know in which z-index environment it's
  // going to live. Choosing an arbitrary starting z-index here is wrong. Redo.
  static const int _DEFAULT_Z = 1000;

  static _currentOverlayZ() {
    int z = _DEFAULT_Z;
    final SparkOverlay current = _currentOverlay();
    if (current != null) {
      final z1 = current.getComputedStyle().zIndex;
      z = int.parse(z1, onError: (source) { });
    }
    return z;
  }

  static void _focusOverlay() {
    final SparkOverlay current = _currentOverlay();
    if (current != null) {
      current.focus();
    }
  }

  // Function closures aren't canonicalized: need to have one pointer for the
  // listener's handler that is added/removed.
  EventListener _captureHandlerInst;
  EventListener _resizeHandlerInst;

  SparkOverlay.created(): super.created() {
    _captureHandlerInst = _captureHandler;
    _resizeHandlerInst = _resizeHandler;
  }

  bool _opened = false;

  /**
   * Set opened to true to show an overlay and to false to hide it.
   * A spark-overlay may be made intially opened by setting its opened
   * attribute.
   */
  @published bool get opened => _opened;

  @published set opened(bool val) {
    print("overlay: $val");
    if (_opened != val) {
      _opened = val;
      // TODO(ussuri): Getter/setter were needed to fix the Menu and Modal not
      // working in the deployed code. With a simple `@published bool opened`,<commit_msg>Remove stray debugging code<commit_after>// Copyright (c) 2013, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

library spark_widgets.overlay;

import 'dart:async';
import 'dart:html';

import 'package:polymer/polymer.dart';

import '../common/spark_widget.dart';

@CustomTag("spark-overlay")
class SparkOverlay extends SparkWidget {
  // Track overlays for z-index and focus managemant.
  // TODO(ussuri): The z-index management with a fixed base z-index is shaky at
  // best. SparkOverlay doesn't know in what z-index environment its instances
  // will live, so assumption that 1000 is always a good value is invalid.
  // Consider:
  // 1) Stacking contexts (https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context).
  // 2) Some way to set the base z-index in the client.
  // 3) Set the same z-index for all spark-overlays in the client, then bump
  //    the current overlay's z-index if any other are currently opened.
  static final List<SparkOverlay> overlays = [];

  static void _trackOverlays(SparkOverlay inOverlay) {
    if (inOverlay.opened) {
      final int z0 = _currentOverlayZ();
      overlays.add(inOverlay);
      final int z1 = _currentOverlayZ();
      if (z0 != null && z1 != null && z1 <= z0) {
        _applyOverlayZ(inOverlay, z0);
      }
    } else {
      final int i = overlays.indexOf(inOverlay);
      if (i >= 0) {
        overlays.removeAt(i);
        _setZ(inOverlay, null);
      }
    }
  }

  static void _applyOverlayZ(SparkOverlay inOverlay, int inAboveZ) {
    _setZ(inOverlay, inAboveZ + 2);
  }

  static void _setZ(Element inNode, int inZ) {
    inNode.style.zIndex = "$inZ";
  }

  static _currentOverlay() {
    return overlays.isNotEmpty ? overlays.last : null;
  }

  // TODO(ussuri): This widget doesn't know in which z-index environment it's
  // going to live. Choosing an arbitrary starting z-index here is wrong. Redo.
  static const int _DEFAULT_Z = 1000;

  static _currentOverlayZ() {
    int z = _DEFAULT_Z;
    final SparkOverlay current = _currentOverlay();
    if (current != null) {
      final z1 = current.getComputedStyle().zIndex;
      z = int.parse(z1, onError: (source) { });
    }
    return z;
  }

  static void _focusOverlay() {
    final SparkOverlay current = _currentOverlay();
    if (current != null) {
      current.focus();
    }
  }

  // Function closures aren't canonicalized: need to have one pointer for the
  // listener's handler that is added/removed.
  EventListener _captureHandlerInst;
  EventListener _resizeHandlerInst;

  SparkOverlay.created(): super.created() {
    _captureHandlerInst = _captureHandler;
    _resizeHandlerInst = _resizeHandler;
  }

  bool _opened = false;

  /**
   * Set opened to true to show an overlay and to false to hide it.
   * A spark-overlay may be made intially opened by setting its opened
   * attribute.
   */
  @published bool get opened => _opened;

  @published set opened(bool val) {
    if (_opened != val) {
      _opened = val;
      // TODO(ussuri): Getter/setter were needed to fix the Menu and Modal not
      // working in the deployed code. With a simple `@published bool opened`,
{'fraction_non_alphanumeric': 0.1027231467473525, 'fraction_numerical': 0.007866868381240545, 'mean_word_length': 3.0758323057953145, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4220078', 'n_tokens_mistral': 2095, 'n_tokens_neox': 2026, 'n_words': 690}