<commit_before>/*!
    Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
    Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
    Created By: <EMAIL>
    Maintained By: <EMAIL>
*/

(function(can, $) {

can.Control("GGRC.Controllers.Modals", {
  BUTTON_VIEW_DONE : GGRC.mustache_path + "/modals/done_buttons.mustache"
  , BUTTON_VIEW_CLOSE : GGRC.mustache_path + "/modals/close_buttons.mustache"
//  BUTTON_VIEW_SAVE
  , BUTTON_VIEW_SAVE_CANCEL : GGRC.mustache_path + "/modals/save_cancel_buttons.mustache"
  , BUTTON_VIEW_SAVE_CANCEL_DELETE : GGRC.mustache_path + "/modals/save_cancel_delete_buttons.mustache"

  , defaults : {
    preload_view : GGRC.mustache_path + "/dashboard/modal_preload.mustache"
    , content_view : GGRC.mustache_path + "/help/help_modal_content.mustache"
    , header_view : GGRC.mustache_path + "/modals/modal_header.mustache"
    , button_view : null
    , model : null    // model class to use when finding or creating new
    , instance : null // model instance to use instead of finding/creating (e.g. for update)
    , new_object_form : false
    , mapping : false
    , find_params : {}
    , add_more : false
    , ui_array : []
    , reset_visible : false
  }

  , init : function() {
    this.defaults.button_view = this.BUTTON_VIEW_DONE;
  }

  , confirm : function(options, success, dismiss) {
    var $target = $('<div class="modal hide"></div>');
    $target
    .modal({ backdrop: "static" })
    .ggrc_controllers_modals(can.extend({
      new_object_form : false
      , button_view : GGRC.mustache_path + "/modals/confirm_buttons.mustache"
      , modal_confirm : "Confirm"
      , modal_description : "description"
      , modal_title : "Confirm"
      , content_view : GGRC.mustache_path + "/modals/confirm.mustache"
    }, options))
    .on('click', 'a.btn[data-toggle=confirm]', function(e) {
      var params = $(e.target).closest('.modal').find('form').serializeArray();
      $target.modal('hide').remove();
      success && success(params, $(e.target).data('option'));
    })
    .on('click.modal-form.close', '[data-dismiss="modal"]', function() {
      $target.modal('hide').remove();
      dismiss && dismiss();
    });
  }
}, {
  init : function() {
    if (!(this.options instanceof can.Observe)) {
      this.options = new can.Observe(this.options);
    }

    if(!this.element.find(".modal-body").length) {
      can.view(this.options.preload_view, {}, this.proxy("after_preload"));
    } else {
      this.after_preload()
    }
    //this.options.attr("mapping", !!this.options.mapping);
  }

  , after_preload : function(content) {
    var that = this;
    if (content) {
      this.element.html(content);
    }
    this.options.attr("$header", this.element.find(".modal-header"));
    this.options.attr("$content", this.element.find(".modal-body"));
    this.options.attr("$footer", this.element.find(".modal-footer"));
    this.on();
    this.fetch_all()
      .then(this.proxy("apply_object_params"))
      .then(this.proxy("serialize_form"))
      .then(function() {
        // If the modal is closed early, the element no longer exists
        that.element && that.element.trigger('preload')
      })
      .then(this.proxy("autocomplete"));
  }

  , apply_object_params : function() {
    var self = this;

    if (this.options.object_params)
      this.options.object_params.each(function(value, key) {
        self.set_value({ name: key, value: value });
      });
  }

  , "input[data-lookup] focus" : function(el, ev) {
    this.autocomplete(el);
  }
  , "input[data-lookup] keyup" : function(el, ev) {
    // Set the transient field for validation
    var name = el.attr('name').split('.'),
        instance = this.options.instance,
        value = el.val();

    name.pop(); //set the owner to null, not the email
    instance._transient || instance.attr("_transient", new can.Observe({}));
    can.reduce(name.slice(0, -1), function(current, next) {
      current = current + "." + next;
      instance.attr(current) || instance.attr(current, new can.Observe({}));
      return current;
    }, "_transient");
    instance.attr(["_transient"].concat(name).join("."), value);
  }

  , autocomplete : function(el) {
    $.cms_autocomplete.call(this, el);
  }

  , autocomplete_select : function(el, event, ui) {
    var original_event,
        that = this;

    $('#extended-info').trigger('mouseleave'); // Make sure the extra info tooltip closes
      var path = el.attr("name").split(".")
        , instance = this.options.instance
        , index = 0
        , prop = path.pop();

      if (/^\d+$/.test(path[path.length - 1])) {
        index = parseInt(path.pop(), 10);
        path = path.join(".");
        if (!this.options.instance.attr(path)) {
          this.options.instance.attr(path, []);
        }
        this.options.instance.attr(path).splice(index, 1, ui.item.stub());
      }
      else {
        path = path.join(".");
      
        // we (ab)use databinding to make the input field change for us
        // doing it three times like this ensures stability

        // handle emptying field and choosing same suggestion
        this.options.instance.attr(path, null);
        // the first change sets to null, 2nd works
        this.options.instance.attr(path, ui.item.stub());
        this.options.instance.attr(path, ui.item.stub());
      }
  }

  , immediate_find_or_create : function(el, ev, data) {
    var that = this
    , prop = el.data("drop")
    , model = CMS.Models[el.data("lookup")]
    , params = { context : that.options.instance.context && that.options.instance.context.serialize ? that.options.instance.context.serialize() : that.options.instance.context };
<commit_msg>CORE-879 Can not select Control in request modal<commit_after>/*!
    Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
    Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
    Created By: <EMAIL>
    Maintained By: <EMAIL>
*/

(function(can, $) {

can.Control("GGRC.Controllers.Modals", {
  BUTTON_VIEW_DONE : GGRC.mustache_path + "/modals/done_buttons.mustache"
  , BUTTON_VIEW_CLOSE : GGRC.mustache_path + "/modals/close_buttons.mustache"
//  BUTTON_VIEW_SAVE
  , BUTTON_VIEW_SAVE_CANCEL : GGRC.mustache_path + "/modals/save_cancel_buttons.mustache"
  , BUTTON_VIEW_SAVE_CANCEL_DELETE : GGRC.mustache_path + "/modals/save_cancel_delete_buttons.mustache"

  , defaults : {
    preload_view : GGRC.mustache_path + "/dashboard/modal_preload.mustache"
    , content_view : GGRC.mustache_path + "/help/help_modal_content.mustache"
    , header_view : GGRC.mustache_path + "/modals/modal_header.mustache"
    , button_view : null
    , model : null    // model class to use when finding or creating new
    , instance : null // model instance to use instead of finding/creating (e.g. for update)
    , new_object_form : false
    , mapping : false
    , find_params : {}
    , add_more : false
    , ui_array : []
    , reset_visible : false
  }

  , init : function() {
    this.defaults.button_view = this.BUTTON_VIEW_DONE;
  }

  , confirm : function(options, success, dismiss) {
    var $target = $('<div class="modal hide"></div>');
    $target
    .modal({ backdrop: "static" })
    .ggrc_controllers_modals(can.extend({
      new_object_form : false
      , button_view : GGRC.mustache_path + "/modals/confirm_buttons.mustache"
      , modal_confirm : "Confirm"
      , modal_description : "description"
      , modal_title : "Confirm"
      , content_view : GGRC.mustache_path + "/modals/confirm.mustache"
    }, options))
    .on('click', 'a.btn[data-toggle=confirm]', function(e) {
      var params = $(e.target).closest('.modal').find('form').serializeArray();
      $target.modal('hide').remove();
      success && success(params, $(e.target).data('option'));
    })
    .on('click.modal-form.close', '[data-dismiss="modal"]', function() {
      $target.modal('hide').remove();
      dismiss && dismiss();
    });
  }
}, {
  init : function() {
    if (!(this.options instanceof can.Observe)) {
      this.options = new can.Observe(this.options);
    }

    if(!this.element.find(".modal-body").length) {
      can.view(this.options.preload_view, {}, this.proxy("after_preload"));
    } else {
      this.after_preload()
    }
    //this.options.attr("mapping", !!this.options.mapping);
  }

  , after_preload : function(content) {
    var that = this;
    if (content) {
      this.element.html(content);
    }
    this.options.attr("$header", this.element.find(".modal-header"));
    this.options.attr("$content", this.element.find(".modal-body"));
    this.options.attr("$footer", this.element.find(".modal-footer"));
    this.on();
    this.fetch_all()
      .then(this.proxy("apply_object_params"))
      .then(this.proxy("serialize_form"))
      .then(function() {
        // If the modal is closed early, the element no longer exists
        that.element && that.element.trigger('preload')
      })
      .then(this.proxy("autocomplete"));
  }

  , apply_object_params : function() {
    var self = this;

    if (this.options.object_params)
      this.options.object_params.each(function(value, key) {
        self.set_value({ name: key, value: value });
      });
  }

  , "input[data-lookup] focus" : function(el, ev) {
    this.autocomplete(el);
  }
  , "input[data-lookup] keyup" : function(el, ev) {
    // Set the transient field for validation
    var name = el.attr('name').split('.'),
        instance = this.options.instance,
        value = el.val();

    name.pop(); //set the owner to null, not the email
    instance._transient || instance.attr("_transient", new can.Observe({}));
    can.reduce(name.slice(0, -1), function(current, next) {
      current = current + "." + next;
      instance.attr(current) || instance.attr(current, new can.Observe({}));
      return current;
    }, "_transient");
    instance.attr(["_transient"].concat(name).join("."), value);
  }

  , autocomplete : function(el) {
    $.cms_autocomplete.call(this, el);
  }

  , autocomplete_select : function(el, event, ui) {
    var original_event,
        that = this;

    $('#extended-info').trigger('mouseleave'); // Make sure the extra info tooltip closes
      var path = el.attr("name").split(".")
        , instance = this.options.instance
        , index = 0
        , prop = path.pop();

      if (/^\d+$/.test(path[path.length - 1])) {
        index = parseInt(path.pop(), 10);
        path = path.join(".");
        if (!this.options.instance.attr(path)) {
          this.options.instance.attr(path, []);
        }
        this.options.instance.attr(path).splice(index, 1, ui.item.stub());
      }
      else {
        path = path.join(".");

        // we (ab)use databinding to make the input field change for us
        // doing it three times like this ensures stability

        // handle emptying field and choosing same suggestion
        this.options.instance.attr(path, null);
        // the first change sets to null, 2nd works
        this.options.instance.attr(path, ui.item.stub());
        this.options.instance.attr(path, ui.item.stub());
        // settign el.val manually is still needed for autocompletes inside
        // deferred renderers such as control/object selector in audit
        setTimeout(function() {
          el.val(ui.item.title || ui.item.name || ui.item.email);
        }, 0);
      }
  }

  , immediate_find_or_create : function(el, ev, data) {
    var that = this
    , prop = el.data("drop")
    , model = CMS.Models[el.data("lookup")]
    , params = { context : that.options.instance.context && that.options.instance.context.serialize ? that.options.instance.context.serialize() : that.options.instance.context };

{'fraction_non_alphanumeric': 0.15815415993881193, 'fraction_numerical': 0.0027194697034078354, 'mean_word_length': 2.9384203480589024, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '6718712', 'n_tokens_mistral': 3789, 'n_tokens_neox': 3723, 'n_words': 729}