<commit_before>/**
 * @license
 * Copyright 2015 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.
 */

// TODO(kgr): remove use of SimpleValue, just use data$ binding instead.
CLASS({
  package: 'foam.ui.md',
  name: 'DAOListView',

  requires: ['SimpleValue'],

  extends: 'foam.ui.SimpleView',
  traits: ['foam.ui.DAODataViewTrait'],

  constants: {
    ROW_CLICK: ['row-click']
  },

  imports: [
    'selection$'
  ],

  properties: [
    {
      model_: 'BooleanProperty',
      name: 'isHidden',
      defaultValue: false,
      postSet: function(_, isHidden) {
        if ( this.dao && ! isHidden ) this.onDAOUpdate();
      }
    },
    {
      name: 'tagName',
      defaultValue: 'div',
    },
    {
      name: 'orientation',
      view: { factory_: 'foam.ui.ChoiceView', choices: ['vertical', 'horizontal'] },
      defaultValue: 'vertical',
    },
    {
      name: 'rowCache_',
      factory: function() { return {}; },
    },
    {
      name: 'data',
      postSet: function(old,nu) {
        if ( old ) {
          old.unlisten(this);
          this.daoRemovalCheck();
        }
        if ( nu ) {
          this.count_ = 0;
          nu.pipe(this);
        }
      }
    },
    {
      name: 'mode',
      defaultValue: 'read-write'
    },
    {
      name: 'count_',
      hidden: true,
      documentation: 'Internal tracker of insertion order',
      defaultValue: 0,
    },
    {
      name: 'removalCheck_',
      model_: 'BooleanProperty',
      documentation: 'Internal tracker of removal check sweep.',
      defaultValue: false,
    },
    {
      model_: 'ViewFactoryProperty',
      name: 'rowView',
      defaultValue: 'foam.ui.DetailView'
    },

  ],

  methods: {

    init: function() {
      this.SUPER();

      // reset selection$ in case of nested list views
      this.Y.set('selection$', undefined);
    },

    put: function(o) {
      /* Sink function to receive updates from the dao */
      if ( this.rowCache_[o.id] ) {
        //console.log("put cached", o.id);
        var d = this.rowCache_[o.id];
        if ( ! equals(d.view.data, o) ) d.view.data = o;
        if ( d.ordering < 0 ) d.ordering = this.count_++; // reset on removal check
      } else {
        //console.log("put new", o.id);

        if ( this.mode === 'read-write' ) o = o.model_.create(o, this.Y); //.clone();
        var view = this.rowView({data: o, model: o.model_}, this.Y);
        // TODO: Something isn't working with the Context, fix
        view.DAO = this.dao;
        if ( this.mode === 'read-write' ) {
          o.addPropertyListener(null, function(o, topic) {
            var prop = o.model_.getProperty(topic[1]);
            // TODO(kgr): remove the deepClone when the DAO does this itself.
            if ( ! prop.transient ) {
              // TODO: if o.id changed, remove the old one?
              view.DAO.put(o.deepClone());
            }
          });
        }
        this.addChild(view);
        this.rowCache_[o.id] = { view: view, ordering: this.count_++ };
        //this.onDAOUpdate();
      }
    },

    remove: function(o) {
      /* Sink function to receive updates from the dao */
      if ( this.rowCache_[o.id] ) {
        //console.log("remove", o.id);
        var r = this.rowCache_[o.id];
        var v = r.view;
        v.destroy();
        if ( v.$ ) {
          var otherOffset = ( this.orientation == 'vertical' ) ?  -v.$.offsetWidth : -v.$.offsetHeight;
          if (  this.orientation == 'vertical' ) {
            v.$.style.transform = v.$.style.webkitTransform =
              "translate3d("+otherOffset+"px, "+r.offset+"px, 0px)";
          } else {
            v.$.style.transform = v.$.style.webkitTransform =
              "translate3d("+r.offset+"px, "+otherOffset+"px, 0px)";
          }<commit_msg>foam.ui.md.DAOListView: Hookup listeners even if the row was cached.<commit_after>/**
 * @license
 * Copyright 2015 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.
 */

CLASS({
  package: 'foam.ui.md',
  name: 'DAOListView',

  extends: 'foam.ui.SimpleView',
  traits: ['foam.ui.DAODataViewTrait'],

  constants: {
    ROW_CLICK: ['row-click']
  },

  imports: [
    'selection$'
  ],

  properties: [
    {
      model_: 'BooleanProperty',
      name: 'isHidden',
      defaultValue: false,
      postSet: function(_, isHidden) {
        if ( this.dao && ! isHidden ) this.onDAOUpdate();
      }
    },
    {
      name: 'tagName',
      defaultValue: 'div',
    },
    {
      name: 'orientation',
      view: { factory_: 'foam.ui.ChoiceView', choices: ['vertical', 'horizontal'] },
      defaultValue: 'vertical',
    },
    {
      name: 'rowCache_',
      factory: function() { return {}; },
    },
    {
      name: 'data',
      postSet: function(old,nu) {
        if ( old ) {
          old.unlisten(this);
          this.daoRemovalCheck();
        }
        if ( nu ) {
          this.count_ = 0;
          nu.pipe(this);
        }
      }
    },
    {
      name: 'mode',
      defaultValue: 'read-write'
    },
    {
      name: 'count_',
      hidden: true,
      documentation: 'Internal tracker of insertion order',
      defaultValue: 0,
    },
    {
      name: 'removalCheck_',
      model_: 'BooleanProperty',
      documentation: 'Internal tracker of removal check sweep.',
      defaultValue: false,
    },
    {
      model_: 'ViewFactoryProperty',
      name: 'rowView',
      defaultValue: 'foam.ui.DetailView'
    },

  ],

  methods: {

    init: function() {
      this.SUPER();

      // reset selection$ in case of nested list views
      this.Y.set('selection$', undefined);
    },

    put: function(o) {
      /* Sink function to receive updates from the dao */
      if ( this.mode === 'read-write' ) o = o.model_.create(o, this.Y); //.clone();

      if ( this.rowCache_[o.id] ) {
        //console.log("put cached", o.id);
        var d = this.rowCache_[o.id];
        var view = d.view;
        view.data = o;
//        if ( ! equals(d.view.data, o) ) d.view.data = o;
        if ( d.ordering < 0 ) d.ordering = this.count_++; // reset on removal check
      } else {
        view = this.rowView({ data: o, model: o.model_}, this.Y);
        this.addChild(view);
        this.rowCache_[o.id] = { view: view, ordering: this.count_++ };
      }

      var dao = this.dao;
      if ( this.mode === 'read-write' ) {
        o.addPropertyListener(null, function(o, topic) {
          var prop = o.model_.getProperty(topic[1]);
          // TODO(kgr): remove the deepClone when the DAO does this itself.
          if ( ! prop.transient ) {
            // TODO: if o.id changed, remove the old one?
            dao.put(o.deepClone());
          }
        });
      }
    },

    remove: function(o) {
      /* Sink function to receive updates from the dao */
      if ( this.rowCache_[o.id] ) {
        //console.log("remove", o.id);
        var r = this.rowCache_[o.id];
        var v = r.view;
        v.destroy();
        if ( v.$ ) {
          var otherOffset = ( this.orientation == 'vertical' ) ?  -v.$.offsetWidth : -v.$.offsetHeight;
          if (  this.orientation == 'vertical' ) {
            v.$.style.transform = v.$.style.webkitTransform =
              "translate3d("+otherOffset+"px, "+r.offset+"px, 0px)";
          } else {
            v.$.style.transform = v.$.style.webkitTransform =
              "translate3d("+r.offset+"px, "+otherOffset+"px, 0px)";
          }
{'fraction_non_alphanumeric': 0.1468193083913608, 'fraction_numerical': 0.0037767024666587987, 'mean_word_length': 2.0275098249374777, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5296967', 'n_tokens_mistral': 2674, 'n_tokens_neox': 2615, 'n_words': 686}