<commit_before>require 'delorean_lang'
require 'coderay'

class Marty::ScriptDetail < Marty::CmFormPanel
  DASH = 0x2012.chr('utf-8')

  css_configure do |c|
    c.require :codemirror
    c.require :delorean
  end

  js_configure do |c|
    c.require :"Ext.ux.form.field.CodeMirror"
    c.require :codemirror
    c.require "#{File.dirname(__FILE__)}/script_detail/javascripts/mode/delorean/delorean.js"

    c.set_action_modes = <<-JS
    	function(a) {
	   this.actions.apply.setDisabled(!a["save"]);
	   this.actions.checkin.setDisabled(!a["checkin"]);
	   this.actions.checkout.setDisabled(!a["checkout"]);
	   this.actions.discard.setDisabled(!a["discard"]);
	   // style input field text based on whether it is editable
	   this.getForm().findField('body').editor.setOption("readOnly", !a["save"]);
    	}
    	JS

    c.get_script_id = <<-JS
    	function() {
	   return this.getForm().findField('id').getValue();
    	}
    	JS

    c.get_script_body = <<-JS
    	function() {
	   return this.getForm().findField('body').getValue();
    	}
    	JS

    # Sets an editor line class (unset any previous line class).  For
    # now, only one line is classed at a time.
    c.set_line_error = <<-JS
    	function(line) {
	   line -= 1;
	   var editor = this.getForm().findField('body').editor;
	   if (editor.oldline) { editor.oldline.className = null; }
	   if (line > -1) {
	   	editor.oldline = editor.setLineClass(line, "errorline");
	   }
	   editor.refresh();
    	}
    	JS

    ######################################################################

    c.refresh_parent = <<-JS
    	function(script_id) {
	   this.netzkeGetParentComponent().scriptRefresh(script_id);
    	}
    	JS

    ######################################################################

    c.on_checkin = <<-JS
    	function() {
           var form_obj = this;
           Ext.Msg.show({
             title: 'Confirm Checkin',
             msg: 'Enter a checkin message:',
             width: 400,
             buttons: Ext.MessageBox.OKCANCEL,
             multiline: true,
             fn: function (btn, value, cfg) {
               (btn == "ok") && form_obj.serverCheckin(
             	{
     		body: form_obj.getScriptBody(),
     		logmsg: value,
     		script_id: form_obj.getScriptId(),
     		});
             },
           });
    	}
    	JS

    c.on_discard = <<-JS
    	function() {
           var form_obj = this;
           Ext.Msg.confirm('Confirm',
     	   'This action will remove the current DEV version.<br>' +
     	   'All changes since the last checkin <b>will be lost</b>. <br>' +
     	   'This action cannot be undone. Are your sure?',
                function (btn, value, cfg) {
                  (btn == "yes") && form_obj.serverDiscard({script_id: form_obj.getScriptId()});
                });
    	}
        JS

    c.on_checkout = <<-JS
    	function() {
	   this.serverCheckout({script_id: this.getScriptId()});
    	}
    	JS

    c.on_print = <<-JS
    	function() {
	  window.open("/marty/components/#{self.name}.html?script_id=" + this.getScriptId(),
		      "printing", 'width=800,height=700,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
    	}
    	JS
  end

  ######################################################################

  def get_title
    return unless @record

    status = @record.isdev? ? "Checked out by #{@record.group_dscript.user}" :
      "Checked in by #{@record.user}"

    [@record.name, "Ver: #{@record.version}", status].
      join(" #{Marty::ScriptDetail::DASH} ")
  end

  def isdev?
    @record && @record.isdev?
  end

  def istip?
    @record && @record.istip?
  end

  def ismodified?
    @record && @record.differs_from_dscript?
  end

  ######################################################################

  def can_save?(ds)
    config[:allow_edit] || (ds.user == Mcfly.whodunnit)
  end

  def can_checkin?(ds)
    ds.user == Mcfly.whodunnit
  end

  def can_discard?(ds)
    config[:allow_edit] || (ds.user == Mcfly.whodunnit)
  end

  def can_checkout?
    config[:allow_edit]
  end

  ######################################################################

  endpoint :netzke_load do |params, this|
    unless self.class.has_any_perm?
      this.netzke_feedback "Permission Denied"
      return
    end

    # logic copied from basepack's form_panel.service
    @record = Marty::Script.find_by_id(params[:id])

    this.set_form_values js_record_data
    this.set_title get_title
    this.set_readonly_mode !isdev?

    modes = {
      save: 	isdev? && can_save?(@record.group_dscript),
      checkin: 	isdev? && ismodified? && can_checkin?(@record.group_dscript),
      checkout: istip? && !@record.dscript && can_checkout?,<commit_msg>Reverted changes to script_detail for admin permissions<commit_after>require 'delorean_lang'
require 'coderay'

class Marty::ScriptDetail < Marty::CmFormPanel
  DASH = 0x2012.chr('utf-8')

  css_configure do |c|
    c.require :codemirror
    c.require :delorean
  end

  js_configure do |c|
    c.require :"Ext.ux.form.field.CodeMirror"
    c.require :codemirror
    c.require "#{File.dirname(__FILE__)}/script_detail/javascripts/mode/delorean/delorean.js"

    c.set_action_modes = <<-JS
    	function(a) {
	   this.actions.apply.setDisabled(!a["save"]);
	   this.actions.checkin.setDisabled(!a["checkin"]);
	   this.actions.checkout.setDisabled(!a["checkout"]);
	   this.actions.discard.setDisabled(!a["discard"]);
	   // style input field text based on whether it is editable
	   this.getForm().findField('body').editor.setOption("readOnly", !a["save"]);
    	}
    	JS

    c.get_script_id = <<-JS
    	function() {
	   return this.getForm().findField('id').getValue();
    	}
    	JS

    c.get_script_body = <<-JS
    	function() {
	   return this.getForm().findField('body').getValue();
    	}
    	JS

    # Sets an editor line class (unset any previous line class).  For
    # now, only one line is classed at a time.
    c.set_line_error = <<-JS
    	function(line) {
	   line -= 1;
	   var editor = this.getForm().findField('body').editor;
	   if (editor.oldline) { editor.oldline.className = null; }
	   if (line > -1) {
	   	editor.oldline = editor.setLineClass(line, "errorline");
	   }
	   editor.refresh();
    	}
    	JS

    ######################################################################

    c.refresh_parent = <<-JS
    	function(script_id) {
	   this.netzkeGetParentComponent().scriptRefresh(script_id);
    	}
    	JS

    ######################################################################

    c.on_checkin = <<-JS
    	function() {
           var form_obj = this;
           Ext.Msg.show({
             title: 'Confirm Checkin',
             msg: 'Enter a checkin message:',
             width: 400,
             buttons: Ext.MessageBox.OKCANCEL,
             multiline: true,
             fn: function (btn, value, cfg) {
               (btn == "ok") && form_obj.serverCheckin(
             	{
     		body: form_obj.getScriptBody(),
     		logmsg: value,
     		script_id: form_obj.getScriptId(),
     		});
             },
           });
    	}
    	JS

    c.on_discard = <<-JS
    	function() {
           var form_obj = this;
           Ext.Msg.confirm('Confirm',
     	   'This action will remove the current DEV version.<br>' +
     	   'All changes since the last checkin <b>will be lost</b>. <br>' +
     	   'This action cannot be undone. Are your sure?',
                function (btn, value, cfg) {
                  (btn == "yes") && form_obj.serverDiscard({script_id: form_obj.getScriptId()});
                });
    	}
        JS

    c.on_checkout = <<-JS
    	function() {
	   this.serverCheckout({script_id: this.getScriptId()});
    	}
    	JS

    c.on_print = <<-JS
    	function() {
	  window.open("/marty/components/#{self.name}.html?script_id=" + this.getScriptId(),
		      "printing", 'width=800,height=700,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
    	}
    	JS
  end

  ######################################################################

  def get_title
    return unless @record

    status = @record.isdev? ? "Checked out by #{@record.group_dscript.user}" :
      "Checked in by #{@record.user}"

    [@record.name, "Ver: #{@record.version}", status].
      join(" #{Marty::ScriptDetail::DASH} ")
  end

  def isdev?
    @record && @record.isdev?
  end

  def istip?
    @record && @record.istip?
  end

  def ismodified?
    @record && @record.differs_from_dscript?
  end

  ######################################################################

  def can_save?(ds)
    self.class.has_admin_perm? || (ds.user == Mcfly.whodunnit)
  end

  def can_checkin?(ds)
    ds.user == Mcfly.whodunnit
  end

  def can_discard?(ds)
    self.class.has_admin_perm? || (ds.user == Mcfly.whodunnit)
  end

  def can_checkout?
    config[:allow_edit]
  end

  ######################################################################

  endpoint :netzke_load do |params, this|
    unless self.class.has_any_perm?
      this.netzke_feedback "Permission Denied"
      return
    end

    # logic copied from basepack's form_panel.service
    @record = Marty::Script.find_by_id(params[:id])

    this.set_form_values js_record_data
    this.set_title get_title
    this.set_readonly_mode !isdev?

    modes = {
      save: 	isdev? && can_save?(@record.group_dscript),
      checkin: 	isdev? && ismodified? && can_checkin?(@record.group_dscript),
      checkout: istip? && !@record.dscript && can_checkout?,
{'fraction_non_alphanumeric': 0.2030043813895264, 'fraction_numerical': 0.0035468391404131026, 'mean_word_length': 2.7669941060903733, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 13, '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': '2475868', 'n_tokens_mistral': 3261, 'n_tokens_neox': 3076, 'n_words': 589}