<commit_before>/*
Each View, Group, and Block element needs an action menu
that does many element-specfic actions based on a User's
selection for the element's corresponding Action Menu.


todo:
   - user triggering of some event ('hover'?), activated by 'main element hover', 
     to activate showing of action menu controls (that live in main elements 'header' element).
     * How do headers and elements know about each other?

*/

INTERACTIONS_OBJECT = {};
INTERACTIONS_OBJECT.block_interactions = ['More Info'];
INTERACTIONS_OBJECT.group_interactions = ['More Info', /*'Submenu', 'Edit'*/];
INTERACTIONS_OBJECT.view_interactions = ['Subscribe', 'Lifecycle', 'Edit', /*'Submenu',*/ 'Command', 'Download', 'Report Issue', 'Refresh Page'];
INTERACTIONS_OBJECT.dashboard_interactions = ['Create Resource', 'Light Theme'];
INTERACTIONS_OBJECT.event_interactions = ['Add Event'];
INTERACTIONS_OBJECT.attachment_interactions = ['Upload Attachment'];
INTERACTIONS_OBJECT.deployment_interactions = ['Activate as Primary Deployment', 'Deactivate as Primary Deployment'];
INTERACTIONS_OBJECT.negotiation_interactions = {owner: ['View Requests'], nonmember: ['Enroll']};
INTERACTIONS_OBJECT.persistence_interactions = ['Activate Persistence', 'Suspend Persistence'];


IONUX.Views.ActionMenu = Backbone.View.extend({
    modal_template: '<div id="action-modal" class="modal hide fade modal-ooi">',
    events:  {
        "click .dropdown-menu li": "action_control_click"
    },
    
    dropdown_button_tmpl: '<div class="action-menu btn-group pull-right">\
    <a class="btn dropdown-toggle" data-toggle="dropdown"><span class="hamburger">&nbsp;</span></a>\
    <ul class="dropdown-menu"><% _.each(dropdown_items, function(item) { %> <li><%= item %></li> <% }); %></ul>\
    </div>',
    
    action_controls_onhover: function(evt){
        if (evt.type == 'mouseenter') {
            var btn = $(this.el).find(".btn-group");
            if (btn.length) {
                btn.show();
                return;
            }
        } 
        if (evt.type == 'mouseleave') {
            $(this.el).find(".btn-group").hide();
            return;
        }
        this.create_actionmenu();
    },

    create_actionmenu: function(){
        var html = _.template(this.dropdown_button_tmpl, {"dropdown_items":this.interaction_items});
        $(this.el).prepend(html);
    },
    
    // Prepends menu at the view level
    create_view_actionmenu: function() {
      var html = _.template(this.dropdown_button_tmpl, {"dropdown_items":this.interaction_items});
      this.$el.prepend(html);
      $('#search-production').prepend(html);
      // Better way to bind? Issue is removing element to unbind events
      // depending on context (dashboard vs. face page, etc.)
      this.setElement('#search-production .action-menu');
    },

    action_control_click: function(evt) {
        evt.preventDefault();
        var action_name = $(evt.target).text();
        var action_event = "action__" + action_name.replace(/ /g, "_").toLowerCase();
        this.trigger(action_event, $(evt.target));
    }
});

IONUX.Views.DashboardActions = IONUX.Views.ActionMenu.extend({
    initialize: function() {
        _.bindAll(this);
        this.interaction_items = INTERACTIONS_OBJECT.dashboard_interactions.slice(0); // ensure clone 
        this.create_view_actionmenu();
        this.on("action__create_resource", this.create_resource);
        this.on("action__light_theme", this.light_theme);
        this.on("action__dark_theme", this.dark_theme);
    },
        
    create_resource: function(){
      new IONUX.Views.CreateResourceView().render();
    },
    
    light_theme: function(target){;
      target.text('Dark Theme');
      $('.pepper').prop('disabled', true);
      $('.salt').prop('disabled', false);
    },
    
    dark_theme: function(target) {
      target.text('Light Theme');
      $('.salt').prop('disabled', true);
      $('.pepper').prop('disabled', false);
    },
});

IONUX.Views.ViewActions = IONUX.Views.ActionMenu.extend({
    // modal_template: '<div id="action-modal" class="modal hide fade modal-ooi">',
    initialize: function() {
        _.bindAll(this);
        this.interaction_items = INTERACTIONS_OBJECT.view_interactions.slice(0);  // ensure clone

        // append resource-specific items here
        if (window.MODEL_DATA.resource_type == 'Org') {

          // ENROLLMENT
          if (IONUX.is_logged_in()) {
            if (_.contains(IONUX.SESSION_MODEL.get('roles')[window.MODEL_DATA.resource.org_governance_name], 'ORG_MANAGER')) {
              // INVITE USER
              this.interaction_items.push("Invite User");
              this.on("action__invite_user", this.action_org__invite_user);

              // INVITE ROLE
              this.interaction_items.push("Offer User Role");
              this.on("action__offer_user_role", this.action_org__offer_user_role);

            } else {
              if (!_.some(window.MODEL_DATA.members, function(x) { return x._id == IONUX.SESSION_MODEL.get("user_id") })) {
                // REQUEST ENROLLMENT
                this.interaction_items.push("Enroll");
                this.on("action__enroll", this.action_org__enroll);
              } else {
                // REQUEST ROLE
                this.interaction_items.push("Request Role");
                this.on("action__request_role", this.action_org__request_role);
              }
            }
          }
        } else if (_.contains(['PlatformDevice', 'InstrumentDevice', 'DataProduct'], window.MODEL_DATA.resource_type)) {

          // activate/suspend persistence are specific to dataproduct only
          // @TODO who is allowed to make these calls?
          if (IONUX.is_logged_in() && window.MODEL_DATA.resource_type == 'DataProduct') {
            if (!window.MODEL_DATA.computed.is_persisted.value) {<commit_msg>Check for ORG_MANAGER role in SESSION_MODEL instead of looking at "members" in the extended resource.<commit_after>/*
Each View, Group, and Block element needs an action menu
that does many element-specfic actions based on a User's
selection for the element's corresponding Action Menu.


todo:
   - user triggering of some event ('hover'?), activated by 'main element hover', 
     to activate showing of action menu controls (that live in main elements 'header' element).
     * How do headers and elements know about each other?

*/

INTERACTIONS_OBJECT = {};
INTERACTIONS_OBJECT.block_interactions = ['More Info'];
INTERACTIONS_OBJECT.group_interactions = ['More Info', /*'Submenu', 'Edit'*/];
INTERACTIONS_OBJECT.view_interactions = ['Subscribe', 'Lifecycle', 'Edit', /*'Submenu',*/ 'Command', 'Download', 'Report Issue', 'Refresh Page'];
INTERACTIONS_OBJECT.dashboard_interactions = ['Create Resource', 'Light Theme'];
INTERACTIONS_OBJECT.event_interactions = ['Add Event'];
INTERACTIONS_OBJECT.attachment_interactions = ['Upload Attachment'];
INTERACTIONS_OBJECT.deployment_interactions = ['Activate as Primary Deployment', 'Deactivate as Primary Deployment'];
INTERACTIONS_OBJECT.negotiation_interactions = {owner: ['View Requests'], nonmember: ['Enroll']};
INTERACTIONS_OBJECT.persistence_interactions = ['Activate Persistence', 'Suspend Persistence'];


IONUX.Views.ActionMenu = Backbone.View.extend({
    modal_template: '<div id="action-modal" class="modal hide fade modal-ooi">',
    events:  {
        "click .dropdown-menu li": "action_control_click"
    },
    
    dropdown_button_tmpl: '<div class="action-menu btn-group pull-right">\
    <a class="btn dropdown-toggle" data-toggle="dropdown"><span class="hamburger">&nbsp;</span></a>\
    <ul class="dropdown-menu"><% _.each(dropdown_items, function(item) { %> <li><%= item %></li> <% }); %></ul>\
    </div>',
    
    action_controls_onhover: function(evt){
        if (evt.type == 'mouseenter') {
            var btn = $(this.el).find(".btn-group");
            if (btn.length) {
                btn.show();
                return;
            }
        } 
        if (evt.type == 'mouseleave') {
            $(this.el).find(".btn-group").hide();
            return;
        }
        this.create_actionmenu();
    },

    create_actionmenu: function(){
        var html = _.template(this.dropdown_button_tmpl, {"dropdown_items":this.interaction_items});
        $(this.el).prepend(html);
    },
    
    // Prepends menu at the view level
    create_view_actionmenu: function() {
      var html = _.template(this.dropdown_button_tmpl, {"dropdown_items":this.interaction_items});
      this.$el.prepend(html);
      $('#search-production').prepend(html);
      // Better way to bind? Issue is removing element to unbind events
      // depending on context (dashboard vs. face page, etc.)
      this.setElement('#search-production .action-menu');
    },

    action_control_click: function(evt) {
        evt.preventDefault();
        var action_name = $(evt.target).text();
        var action_event = "action__" + action_name.replace(/ /g, "_").toLowerCase();
        this.trigger(action_event, $(evt.target));
    }
});

IONUX.Views.DashboardActions = IONUX.Views.ActionMenu.extend({
    initialize: function() {
        _.bindAll(this);
        this.interaction_items = INTERACTIONS_OBJECT.dashboard_interactions.slice(0); // ensure clone 
        this.create_view_actionmenu();
        this.on("action__create_resource", this.create_resource);
        this.on("action__light_theme", this.light_theme);
        this.on("action__dark_theme", this.dark_theme);
    },
        
    create_resource: function(){
      new IONUX.Views.CreateResourceView().render();
    },
    
    light_theme: function(target){;
      target.text('Dark Theme');
      $('.pepper').prop('disabled', true);
      $('.salt').prop('disabled', false);
    },
    
    dark_theme: function(target) {
      target.text('Light Theme');
      $('.salt').prop('disabled', true);
      $('.pepper').prop('disabled', false);
    },
});

IONUX.Views.ViewActions = IONUX.Views.ActionMenu.extend({
    // modal_template: '<div id="action-modal" class="modal hide fade modal-ooi">',
    initialize: function() {
        _.bindAll(this);
        this.interaction_items = INTERACTIONS_OBJECT.view_interactions.slice(0);  // ensure clone
        
        // append resource-specific items here
        if (window.MODEL_DATA.resource_type == 'Org') {
          
          // ENROLLMENT
          if (IONUX.is_logged_in()) {
            if (_.contains(IONUX.SESSION_MODEL.get('roles')[window.MODEL_DATA.resource.org_governance_name], 'ORG_MANAGER')) {
              // INVITE USER
              this.interaction_items.push("Invite User");
              this.on("action__invite_user", this.action_org__invite_user);
              
              // INVITE ROLE
              this.interaction_items.push("Offer User Role");
              this.on("action__offer_user_role", this.action_org__offer_user_role);
              
            } else {
              if (! _.contains(IONUX.SESSION_MODEL.get('roles')[window.MODEL_DATA.resource.org_governance_name], 'ORG_MEMBER')) {
                // REQUEST ENROLLMENT
                this.interaction_items.push("Enroll");
                this.on("action__enroll", this.action_org__enroll);
              } else {
                // REQUEST ROLE
                this.interaction_items.push("Request Role");
                this.on("action__request_role", this.action_org__request_role);
              }
            }
          }
        } else if (_.contains(['PlatformDevice', 'InstrumentDevice', 'DataProduct'], window.MODEL_DATA.resource_type)) {

          // activate/suspend persistence are specific to dataproduct only
          // @TODO who is allowed to make these calls?
          if (IONUX.is_logged_in() && window.MODEL_DATA.resource_type == 'DataProduct') {
            if (!window.MODEL_DATA.computed.is_persisted.value) {
{'fraction_non_alphanumeric': 0.14063289008584415, 'fraction_numerical': 0.00033664366268305, 'mean_word_length': 2.9360715468698246, 'pattern_counts': {'":': 6, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1189424', 'n_tokens_mistral': 3487, 'n_tokens_neox': 3446, 'n_words': 699}