<commit_before>/**
 * @license
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*

  onResize:
    resize scrollbar
    repaint

  onDAOUpdate
    reCount size
    repaint

*/
CLASS({
  package: 'foam.ui',
  name: 'TableView',
  extends: 'foam.ui.AbstractDAOView',

  label: 'Table View',

  requires: [
    'foam.graphics.ScrollCView',
    'foam.ui.EditColumnsView',
    'foam.input.touch.GestureTarget'
  ],
  imports: [
    'hardSelection$',
    'softSelection$ as selection$'
  ],

  constants: {
    MIN_COLUMN_SIZE: 5, // If column is resized below this size, then remove the column instead of shrinking it

    ROW_SELECTED: ['escape'],

    CLICK: "click", // event topic

    DOUBLE_CLICK: "double-click" // event topic
  },

  properties: [
    {
      type: 'Model',
      name:  'model',
      defaultValueFn: function() { return this.X.model ||
                                   ( this.data && this.data.model ); }
    },
    {
      type: 'String',
      name: 'className',
      lazyFactory: function() {
        return 'foamTable ' + this.model.name + 'Table';
      }
    },
    {
      type: 'StringArray',
      name:  'properties',
      postSet: function() { this.paintTable(); }
    },
    {
      name:  'hardSelection',
      postSet: function(_, v) { this.publish(this.ROW_SELECTED, v); }
    },
    {
      name:  'selection'
    },
    {
      name:  'children',
      // type:  'Array[View]',
      factory: function() { return []; }
    },
    {
      name:  'sortOrder',
      // type:  'Comparator',
      postSet: function() { this.paintTable(); },
      defaultValue: undefined
    },
    {
      name:  'sortProp',
      defaultValue: undefined
    },
    {
      name: 'ascIcon',
      defaultValue: '&#9650;'
    },
    {
      name: 'descIcon',
      defaultValue: '&#9660;'
    },
    {
      name: 'rows',
      type:  'Int',
      defaultValue: 50,
      postSet: function(oldValue, newValue) {
        if ( oldValue !== newValue ) this.paintData();
      }
    },
    {
      type:  'Int',
      name: 'height'
    },
    {
      type:  'Boolean',
      name: 'scrollEnabled',
      defaultValue: false
    },
    {
      type: 'Boolean',
      name: 'columnResizeEnabled',
      defaultValue: false
    },
    {
      type: 'Boolean',
      name: 'editColumnsEnabled',
      defaultValue: false
    },
    {
      name: 'scrollbar',
      // type: 'ScrollCView',
      factory: function() {
        return this.ScrollCView.create({height:800, width: 24, x: 1, y: 0, size: 200, extent: 10});
      },
      postSet: function(old, nu) {
        if ( old === nu ) return;
        if ( old ) old.value$.removeListener(this.paintData);
        if ( nu ) nu.value$.addListener(this.paintData);
      }
    },
    {
      name: 'scrollPitch',
      help: 'Number of (CSS) pixels of touch drag required to scroll by one',
      defaultValue: 10
    },
    {
      name: 'touchPrev',
      hidden: true,
      transient: true,
      defaultValue: 0
    },
    {
      type: 'Array',
      name: 'selectionListeners_',
      lazyFactory: function() { return []; }
    },
    {
      type: 'Array',
      name: 'hardSelectionListeners_',
      lazyFactory: function() { return []; }
    },
    {
      type: 'Int',
      name: 'mouseX',
      defaultValue: 0
    },
    {
      type: 'Int',
      name: 'mouseY',
      defaultValue: 0
    },
    {
      type: 'Boolean',
      name: 'mouseOverRow',
      defaultValue: false
    },
    {
      name: 'useInnerContainer',
      documentation: 'Designed to be overridden by the MD TableView. Changes ' +
          'the $container defaultValueFn below to use this.$ instead of its parent.',
      defaultValue: false
    },
    {
      name: '$container',
      defaultValueFn: function() {
        return !this.$ || this.useInnerContainer ? this.$ : this.$.parentElement.parentElement;
      },
      postSet: function(old, nu) {
        if ( old === nu ) return;
        this.onResize();
      }
    },
    {
      name: '$table',
      defaultValueFn: function() { return this.$ && this.$.querySelector('table'); }
    },
    {
      name: '$thead',
      defaultValueFn: function() { return this.$ && this.$.querySelector('thead'); }
    },
    {
      name: '$tbody',
      defaultValueFn: function() { return this.$ && this.$.querySelector('tbody'); }
    }
  ],

  actions: [
    {
      name: 'selectRow',
      keyboardShortcuts: [ 13 /* enter */ ],
      code: function() {
        if ( this.selection ) this.hardSelection = this.selection;
        this.publish(this.CLICK, this.selection);
      }
    },
    {
      name: 'prevRow',
      keyboardShortcuts: [ 38 /* up arrow */, 75 /* k */ ],
      code: function() {
        if ( ! this. objs || ! this.objs.length ) return;
        if ( ! this.selection && this.hardSelection ) this.selection = this.hardSelection;
        if ( this.selection ) {
          var i = this.objs.indexOf(this.selection);
          this.scrollbar.value--;
          if ( i > 0 ) this.selection = this.objs[i-1];
        } else {
          this.selection = this.objs[0];
        }
        this.paintData();
      }
    },
    {
      name: 'nextRow',
      keyboardShortcuts: [ 40 /* down arrow */, 74 /* j */ ],
      code: function() {
        if ( ! this. objs || ! this.objs.length ) return;
        if ( ! this.selection && this.hardSelection ) this.selection = this.hardSelection;
        if ( this.selection ) {
          var i = this.objs.indexOf(this.selection);
          this.scrollbar.value++;
          if ( i < this.objs.length-1 ) this.selection = this.objs[i+1];
        } else {
          this.selection = this.objs[0];
        }
        this.paintData();
      }
    }
  ],

  listeners: [
    {
      name: 'onResize',
      isMerged: 200,
      code: function() {
        if ( ! this.$ ) return;

        var thead = this.$thead;
        var tbody = this.$tbody;
        var row = tbody && tbody.firstChild;
        if ( row ) {
          var containerHeight = this.$container.offsetHeight;
          var headHeight = thead.offsetHeight;
          var rowHeight = row.offsetHeight;
          var rows = Math.floor((containerHeight - headHeight) / rowHeight);
          this.rows = rows;
          this.scrollbar.extent = rows;
          this.scrollbar.height = containerHeight - headHeight - 10;
          this.scrollbar.view.paint();
        } else {<commit_msg>U1 TableView: Only paint the scrollbar when scrolling is enabled<commit_after>/**
 * @license
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*

  onResize:
    resize scrollbar
    repaint

  onDAOUpdate
    reCount size
    repaint

*/
CLASS({
  package: 'foam.ui',
  name: 'TableView',
  extends: 'foam.ui.AbstractDAOView',

  label: 'Table View',

  requires: [
    'foam.graphics.ScrollCView',
    'foam.ui.EditColumnsView',
    'foam.input.touch.GestureTarget'
  ],
  imports: [
    'hardSelection$',
    'softSelection$ as selection$'
  ],

  constants: {
    MIN_COLUMN_SIZE: 5, // If column is resized below this size, then remove the column instead of shrinking it

    ROW_SELECTED: ['escape'],

    CLICK: "click", // event topic

    DOUBLE_CLICK: "double-click" // event topic
  },

  properties: [
    {
      type: 'Model',
      name:  'model',
      defaultValueFn: function() { return this.X.model ||
                                   ( this.data && this.data.model ); }
    },
    {
      type: 'String',
      name: 'className',
      lazyFactory: function() {
        return 'foamTable ' + this.model.name + 'Table';
      }
    },
    {
      type: 'StringArray',
      name:  'properties',
      postSet: function() { this.paintTable(); }
    },
    {
      name:  'hardSelection',
      postSet: function(_, v) { this.publish(this.ROW_SELECTED, v); }
    },
    {
      name:  'selection'
    },
    {
      name:  'children',
      // type:  'Array[View]',
      factory: function() { return []; }
    },
    {
      name:  'sortOrder',
      // type:  'Comparator',
      postSet: function() { this.paintTable(); },
      defaultValue: undefined
    },
    {
      name:  'sortProp',
      defaultValue: undefined
    },
    {
      name: 'ascIcon',
      defaultValue: '&#9650;'
    },
    {
      name: 'descIcon',
      defaultValue: '&#9660;'
    },
    {
      name: 'rows',
      type:  'Int',
      defaultValue: 50,
      postSet: function(oldValue, newValue) {
        if ( oldValue !== newValue ) this.paintData();
      }
    },
    {
      type:  'Int',
      name: 'height'
    },
    {
      type:  'Boolean',
      name: 'scrollEnabled',
      defaultValue: false
    },
    {
      type: 'Boolean',
      name: 'columnResizeEnabled',
      defaultValue: false
    },
    {
      type: 'Boolean',
      name: 'editColumnsEnabled',
      defaultValue: false
    },
    {
      name: 'scrollbar',
      // type: 'ScrollCView',
      factory: function() {
        return this.ScrollCView.create({height:800, width: 24, x: 1, y: 0, size: 200, extent: 10});
      },
      postSet: function(old, nu) {
        if ( old === nu ) return;
        if ( old ) old.value$.removeListener(this.paintData);
        if ( nu ) nu.value$.addListener(this.paintData);
      }
    },
    {
      name: 'scrollPitch',
      help: 'Number of (CSS) pixels of touch drag required to scroll by one',
      defaultValue: 10
    },
    {
      name: 'touchPrev',
      hidden: true,
      transient: true,
      defaultValue: 0
    },
    {
      type: 'Array',
      name: 'selectionListeners_',
      lazyFactory: function() { return []; }
    },
    {
      type: 'Array',
      name: 'hardSelectionListeners_',
      lazyFactory: function() { return []; }
    },
    {
      type: 'Int',
      name: 'mouseX',
      defaultValue: 0
    },
    {
      type: 'Int',
      name: 'mouseY',
      defaultValue: 0
    },
    {
      type: 'Boolean',
      name: 'mouseOverRow',
      defaultValue: false
    },
    {
      name: 'useInnerContainer',
      documentation: 'Designed to be overridden by the MD TableView. Changes ' +
          'the $container defaultValueFn below to use this.$ instead of its parent.',
      defaultValue: false
    },
    {
      name: '$container',
      defaultValueFn: function() {
        return !this.$ || this.useInnerContainer ? this.$ : this.$.parentElement.parentElement;
      },
      postSet: function(old, nu) {
        if ( old === nu ) return;
        this.onResize();
      }
    },
    {
      name: '$table',
      defaultValueFn: function() { return this.$ && this.$.querySelector('table'); }
    },
    {
      name: '$thead',
      defaultValueFn: function() { return this.$ && this.$.querySelector('thead'); }
    },
    {
      name: '$tbody',
      defaultValueFn: function() { return this.$ && this.$.querySelector('tbody'); }
    }
  ],

  actions: [
    {
      name: 'selectRow',
      keyboardShortcuts: [ 13 /* enter */ ],
      code: function() {
        if ( this.selection ) this.hardSelection = this.selection;
        this.publish(this.CLICK, this.selection);
      }
    },
    {
      name: 'prevRow',
      keyboardShortcuts: [ 38 /* up arrow */, 75 /* k */ ],
      code: function() {
        if ( ! this. objs || ! this.objs.length ) return;
        if ( ! this.selection && this.hardSelection ) this.selection = this.hardSelection;
        if ( this.selection ) {
          var i = this.objs.indexOf(this.selection);
          this.scrollbar.value--;
          if ( i > 0 ) this.selection = this.objs[i-1];
        } else {
          this.selection = this.objs[0];
        }
        this.paintData();
      }
    },
    {
      name: 'nextRow',
      keyboardShortcuts: [ 40 /* down arrow */, 74 /* j */ ],
      code: function() {
        if ( ! this. objs || ! this.objs.length ) return;
        if ( ! this.selection && this.hardSelection ) this.selection = this.hardSelection;
        if ( this.selection ) {
          var i = this.objs.indexOf(this.selection);
          this.scrollbar.value++;
          if ( i < this.objs.length-1 ) this.selection = this.objs[i+1];
        } else {
          this.selection = this.objs[0];
        }
        this.paintData();
      }
    }
  ],

  listeners: [
    {
      name: 'onResize',
      isMerged: 200,
      code: function() {
        if ( ! this.$ ) return;

        var thead = this.$thead;
        var tbody = this.$tbody;
        var row = tbody && tbody.firstChild;
        if ( row ) {
          var containerHeight = this.$container.offsetHeight;
          var headHeight = thead.offsetHeight;
          var rowHeight = row.offsetHeight;
          var rows = Math.floor((containerHeight - headHeight) / rowHeight);
          this.rows = rows;
          if ( this.scrollEnabled ) {
            this.scrollbar.extent = rows;
            this.scrollbar.height = containerHeight - headHeight - 10;
            this.scrollbar.view.paint();
          }
        } else {
{'fraction_non_alphanumeric': 0.1402634389462442, 'fraction_numerical': 0.008187967248131008, 'mean_word_length': 1.9639164380671028, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2096205', 'n_tokens_mistral': 4380, 'n_tokens_neox': 4288, 'n_words': 1057}