<commit_before>/*jshint bitwise:true, indent:2, curly:true eqeqeq:true, immed:true,
latedef:true, newcap:true, noarg:true, regexp:true, undef:true,
trailing:true white:true*/
/*global XT:true, XM:true, XV:true, enyo:true*/

(function () {

  /**
    Must include a component called `list`.
    List must be of sub-kind `XV.ListRelations`.
    The `value` must be set to a collection of `XM.Info` models.

    @class
    @name XV.ListRelationsBox
    @extends XV.Groupbox
  */
  enyo.kind(/** @lends XV.ListRelationsBox# */{
    name: "XV.ListRelationsBox",
    kind: "XV.Groupbox",
    classes: "panel",
    published: {
      attr: null,
      value: null,
      title: "",
      parentKey: "",
      listRelations: "",
      searchList: "",
      canOpen: true
    },
    events: {
      onSearch: "",
      onWorkspace: ""
    },
    handlers: {
      onSelect: "selectionChanged",
      onDeselect: "selectionChanged",
      onParentStatusChange: "workspaceModelStatusChanged"
    },
    create: function () {
      this.inherited(arguments);
      var canAttach = this.getSearchList().length > 0,
        buttons;

      // Header
      this.createComponent({
        kind: "onyx.GroupboxHeader",
        content: this.getTitle()
      });

      // List
      this.createComponent({
        kind: this.getListRelations(),
        name: "list",
        attr: this.getAttr(),
        fit: true
      });

      // Buttons
      buttons = {kind: 'FittableColumns', classes: "xv-groupbox-buttons",
        components: [
      ]};
      var canOpen = this.getCanOpen();
      if (canOpen) {
        buttons.components.push(
        {kind: "onyx.Button", name: "newButton", onclick: "newItem",
          content: "_new".loc(), classes: "xv-groupbox-button-left",
          disabled: true});
      }
      if (canAttach) {
        buttons.components.push(
        {kind: "onyx.Button", name: "attachButton", onclick: "attachItem",
          content: "_attach".loc(), classes: "xv-groupbox-button-center",
          disabled: true},
        {kind: "onyx.Button", name: "detachButton", onclick: "detachItem",
          content: "_detach".loc(), classes: "xv-groupbox-button-center",
          disabled: true});
      }
      if (canOpen) {
        buttons.components.push(
        {kind: "onyx.Button", name: "openButton", onclick: "openItem",
          content: "_open".loc(), classes: "xv-groupbox-button-right",
          disabled: true, fit: canAttach});
      }
      this.createComponent(buttons);
    },
    attachItem: function () {
      var list = this.$.list,
        key = this.getParentKey(),
        parent = list.getParent(),
        searchList = this.getSearchList(),
        inEvent,

        // Callback to handle selection...
        callback = function (selectedModel) {

          // Instantiate the models involved
          var Klass = XT.getObjectByName(selectedModel.editableModel),
            model = new Klass({id: selectedModel.id}),
            InfoKlass = model.getRelation('account').relatedModel,
            infoModel = new InfoKlass({id: parent.id}),
            setAndSave = function () {
              var K = XM.Model,
                options = {};
              if (model.getStatus() === K.READY_CLEAN &&
                  infoModel.getStatus() === K.READY_CLEAN) {
                model.off('statusChange', setAndSave);
                infoModel.off('statusChange', setAndSave);

                // Callback to update our list with changes when save complete
                options.success = function () {
                  list.getValue().add(selectedModel);
                };

                // Set and save our contact with the new account relation
                model.set(key, infoModel);
                model.save(null, options);
              }
            };

          // When fetch complete, trigger set and save
          model.on('statusChange', setAndSave);
          infoModel.on('statusChange', setAndSave);

          // Go get the data
          model.fetch();
          infoModel.fetch();
        };<commit_msg>issue #18746: list relations add related model instead of list item<commit_after>/*jshint bitwise:true, indent:2, curly:true eqeqeq:true, immed:true,
latedef:true, newcap:true, noarg:true, regexp:true, undef:true,
trailing:true white:true*/
/*global XT:true, XM:true, XV:true, enyo:true*/

(function () {

  /**
    Must include a component called `list`.
    List must be of sub-kind `XV.ListRelations`.
    The `value` must be set to a collection of `XM.Info` models.

    @class
    @name XV.ListRelationsBox
    @extends XV.Groupbox
  */
  enyo.kind(/** @lends XV.ListRelationsBox# */{
    name: "XV.ListRelationsBox",
    kind: "XV.Groupbox",
    classes: "panel",
    published: {
      attr: null,
      value: null,
      title: "",
      parentKey: "",
      listRelations: "",
      searchList: "",
      canOpen: true
    },
    events: {
      onSearch: "",
      onWorkspace: ""
    },
    handlers: {
      onSelect: "selectionChanged",
      onDeselect: "selectionChanged",
      onParentStatusChange: "workspaceModelStatusChanged"
    },
    create: function () {
      this.inherited(arguments);
      var canAttach = this.getSearchList().length > 0,
        buttons;

      // Header
      this.createComponent({
        kind: "onyx.GroupboxHeader",
        content: this.getTitle()
      });

      // List
      this.createComponent({
        kind: this.getListRelations(),
        name: "list",
        attr: this.getAttr(),
        fit: true
      });

      // Buttons
      buttons = {kind: 'FittableColumns', classes: "xv-groupbox-buttons",
        components: [
      ]};
      var canOpen = this.getCanOpen();
      if (canOpen) {
        buttons.components.push(
        {kind: "onyx.Button", name: "newButton", onclick: "newItem",
          content: "_new".loc(), classes: "xv-groupbox-button-left",
          disabled: true});
      }
      if (canAttach) {
        buttons.components.push(
        {kind: "onyx.Button", name: "attachButton", onclick: "attachItem",
          content: "_attach".loc(), classes: "xv-groupbox-button-center",
          disabled: true},
        {kind: "onyx.Button", name: "detachButton", onclick: "detachItem",
          content: "_detach".loc(), classes: "xv-groupbox-button-center",
          disabled: true});
      }
      if (canOpen) {
        buttons.components.push(
        {kind: "onyx.Button", name: "openButton", onclick: "openItem",
          content: "_open".loc(), classes: "xv-groupbox-button-right",
          disabled: true, fit: canAttach});
      }
      this.createComponent(buttons);
    },
    attachItem: function () {
      var list = this.$.list,
        ListModel = list.getValue().model,
        key = this.getParentKey(),
        parent = list.getParent(),
        searchList = this.getSearchList(),
        inEvent,

        // Callback to handle selection...
        callback = function (selectedModel) {

          // Instantiate the models involved.
          // this looks a little backwards, because what's actually going
          // on here is that we're taking a related model of the workspace
          // that we're in and attaching it to an editable model of the
          // item that we've just selected. This is on purpose!
          // Meanwhile, we have to populate the list with a related model
          // of the item that we've just selected, as selectedModel refers
          // to an info model, which is not the type of model we want
          // to use in the list
          var Klass = XT.getObjectByName(selectedModel.editableModel),
            model = new Klass({id: selectedModel.id}),
            InfoKlass = model.getRelation(key).relatedModel,
            infoModel = new InfoKlass({id: parent.id}),
            listModel = new ListModel({id : selectedModel.id}),
            setAndSave = function () {
              var K = XM.Model,
                options = {};
              if (model.getStatus() === K.READY_CLEAN &&
                  infoModel.getStatus() === K.READY_CLEAN &&
                  listModel.getStatus() === K.READY_CLEAN) {
                model.off('statusChange', setAndSave);
                infoModel.off('statusChange', setAndSave);
                listModel.off('statusChange', setAndSave);

                // Callback to update our list with changes when save complete
                options.success = function () {
                  list.getValue().add(listModel);
                };

                // Set and save our contact with the new account relation
                model.set(key, infoModel);
                model.save(null, options);
              }
            };

          // When fetch complete, trigger set and save
          model.on('statusChange', setAndSave);
          infoModel.on('statusChange', setAndSave);
          listModel.on('statusChange', setAndSave);

          // Go get the data
          model.fetch();
          infoModel.fetch();
          listModel.fetch();
        };
{'fraction_non_alphanumeric': 0.1292215701472705, 'fraction_numerical': 0.0009965673790277932, 'mean_word_length': 2.0247823174815807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4368974', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2519, 'n_words': 636}