<commit_before>'use strict';

import Morph from './Morph.js';

export default class Window extends Morph {

  // window title
  get title() {
    return this._title;
  }
  set title(val) {
    this._title =  val;
    this.render();
  }

  get isFixed() {
    return this.hasAttribute('fixed');
  }

  attachedCallback() {
    console.log('window attachedCallback!');

    // define shortcut variables
    this.titleSpan = this.shadowRoot.querySelector('.window-title span');
    this.minButton = this.shadowRoot.querySelector('.window-min');
    this.maxButton = this.shadowRoot.querySelector('.window-max');
    this.pinButton = this.shadowRoot.querySelector('.window-pin');
    this.resizeButton = this.shadowRoot.querySelector('.window-resize');
    this.closeButton = this.shadowRoot.querySelector('.window-close');
    this.contentBlock = this.shadowRoot.querySelector('#window-content');

    // bind events for window behavior
    this.dragging = false;
    this.addEventListener('mousedown', (e) => { this.focus(); });
    this.closeButton.addEventListener('click', (e) => { this.closeButtonClicked(e) });
    this.minButton.addEventListener('click', (e) => { this.minButtonClicked(e) });
    this.maxButton.addEventListener('click', (e) => { this.maxButtonClicked(e) });
    this.pinButton.addEventListener('click', (e) => { this.pinButtonClicked(e) });
    this.resizeButton.addEventListener('mousedown', (e) => { this.resizeMouseDown(e) });
    this.shadowRoot.querySelector('.window-title').addEventListener('mousedown', (e) => { this.titleMouseDown(e) });

    document.body.addEventListener('mousemove', (e) => { this.windowMouseMove(e) });
    document.body.addEventListener('mouseup', (e) => { this.windowMouseUp(e); });

    this.addEventListener('created', (e) => { this.focus() });

    this.created = true;
    this.render();
  }

  attributeChangedCallback(attrName, oldValue, newValue) {
    switch(attrName) {
      case 'title':
        this.render();
        break;
      case 'fixed':
        this.reposition();
        break;
      default:
        //
    }
  }

  // (re)render the element
  render() {
    console.log('window render!');
    if (this.created) {
      if(this.attributes['title']) {
        this.titleSpan.innerHTML = this.attributes['title'].value;
      }
    }
  }

  reposition() {
    let rect = this.getBoundingClientRect();
    let scrollX = document.scrollingElement.scrollLeft || 0;
    let scrollY = document.scrollingElement.scrollTop || 0;

    if (this.isFixed) {
      this.style.left = rect.left + 'px';
      this.style.top = rect.top + 'px';
      this.classList.add('window-fixed');
      this.pinButton.classList.add('active');
    } else {
      this.style.left = (rect.left + scrollX) + 'px';
      this.style.top = (rect.top + scrollY) + 'px';
      this.classList.remove('window-fixed');
      this.pinButton.classList.remove('active');
    }
  }

  focus(e) {
    var allWindows = Array.from(document.querySelectorAll('lively-window'));
    allWindows.forEach((w) => {
      w.style['z-index'] = 0;
      $(w.getSubmorph('.window')).removeClass('focused');
    });
    this.style['z-index'] = 10;
    $(this.getSubmorph('.window')).addClass('focused');
  }

  minButtonClicked(e) {
    // TODO
  }

  maxButtonClicked(e) {
    // TODO<commit_msg>z-index handling of windows now remembers the ordering of unfocused windows<commit_after>'use strict';

import Morph from './Morph.js';

export default class Window extends Morph {

  // window title
  get title() {
    return this._title;
  }
  set title(val) {
    this._title =  val;
    this.render();
  }

  get isFixed() {
    return this.hasAttribute('fixed');
  }

  attachedCallback() {
    console.log('window attachedCallback!');

    // define shortcut variables
    this.titleSpan = this.shadowRoot.querySelector('.window-title span');
    this.minButton = this.shadowRoot.querySelector('.window-min');
    this.maxButton = this.shadowRoot.querySelector('.window-max');
    this.pinButton = this.shadowRoot.querySelector('.window-pin');
    this.resizeButton = this.shadowRoot.querySelector('.window-resize');
    this.closeButton = this.shadowRoot.querySelector('.window-close');
    this.contentBlock = this.shadowRoot.querySelector('#window-content');

    // bind events for window behavior
    this.dragging = false;
    this.addEventListener('mousedown', (e) => { this.focus(); });
    this.closeButton.addEventListener('click', (e) => { this.closeButtonClicked(e) });
    this.minButton.addEventListener('click', (e) => { this.minButtonClicked(e) });
    this.maxButton.addEventListener('click', (e) => { this.maxButtonClicked(e) });
    this.pinButton.addEventListener('click', (e) => { this.pinButtonClicked(e) });
    this.resizeButton.addEventListener('mousedown', (e) => { this.resizeMouseDown(e) });
    this.shadowRoot.querySelector('.window-title').addEventListener('mousedown', (e) => { this.titleMouseDown(e) });

    document.body.addEventListener('mousemove', (e) => { this.windowMouseMove(e) });
    document.body.addEventListener('mouseup', (e) => { this.windowMouseUp(e); });

    this.addEventListener('created', (e) => { this.focus() });

    this.created = true;
    this.render();
  }

  attributeChangedCallback(attrName, oldValue, newValue) {
    switch(attrName) {
      case 'title':
        this.render();
        break;
      case 'fixed':
        this.reposition();
        break;
      default:
        //
    }
  }

  // (re)render the element
  render() {
    console.log('window render!');
    if (this.created) {
      if(this.attributes['title']) {
        this.titleSpan.innerHTML = this.attributes['title'].value;
      }
    }
  }

  reposition() {
    let rect = this.getBoundingClientRect();
    let scrollX = document.scrollingElement.scrollLeft || 0;
    let scrollY = document.scrollingElement.scrollTop || 0;

    if (this.isFixed) {
      this.style.left = rect.left + 'px';
      this.style.top = rect.top + 'px';
      this.classList.add('window-fixed');
      this.pinButton.classList.add('active');
    } else {
      this.style.left = (rect.left + scrollX) + 'px';
      this.style.top = (rect.top + scrollY) + 'px';
      this.classList.remove('window-fixed');
      this.pinButton.classList.remove('active');
    }
  }

  focus(e) {
    var minZIndex = 100;
    // find all windows but this one
    var allWindowsButThis = Array.from(document.querySelectorAll('lively-window'));
    var thisIdx = allWindowsButThis.indexOf(this);
    allWindowsButThis.splice(thisIdx, 1);

    // sort ascending by z-index
    allWindowsButThis.sort((a, b) => {
      return parseInt(a.style["z-index"]) - parseInt(b.style["z-index"]);
    });

    // assign new z-index
    allWindowsButThis.forEach((win, idx) => {
      win.style["z-index"] = minZIndex + idx;
      $(win.getSubmorph('.window')).removeClass('focused');
    });
    this.style["z-index"] = minZIndex + allWindowsButThis.length;
    $(this.getSubmorph('.window')).addClass('focused');
  }

  minButtonClicked(e) {
    // TODO
  }

  maxButtonClicked(e) {
    // TODO
{'fraction_non_alphanumeric': 0.16788732394366196, 'fraction_numerical': 0.0015492957746478873, 'mean_word_length': 3.259748050389922, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, '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': '1333356', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2179, 'n_words': 358}