<commit_before>
/** 
* @class JSFormPage
* A Javascript SimpleFormPage for generating Ajax forms 
* @author <NAME> <<EMAIL>>
*
*This javascript object deals with dynamic printing  
*of static/editable forms
*
*JSFormPage.js object is instantiated from CXGN::Page::Form::JSFormPage.pm
*/

JSAN.use('jQuery');
JSAN.use('Prototype');

if (!CXGN) CXGN = function() {};
if (!CXGN.Page) CXGN.Page = function() {};
if (!CXGN.Page.Form) CXGN.Page.Form = function() {};

CXGN.Page.Form.JSFormPage = function(id,  name,  script, formId, jsObjectName, pageName) {
    //alert('In constructor.');
   
    this.setObjectName(name);
    this.setObjectId(id);
    this.setAjaxScript(script);
    this.setFormId(formId);
    this.setJsObjectName(jsObjectName);
    this.setPageName(pageName);
    this.defineButtons();
};


CXGN.Page.Form.JSFormPage.prototype = { 
    
    render: function(action) {
       //render the form here
	if (!action) action  = "view";
	if (!this.getObjectId()) action = "new";
	//	MochiKit.Logging.log("FormId = " + this.formId);
	this.printForm( action);
    },
    

        
    /**
     * store
     * store the information from the form into the database
     * -checks privileges
     * -form fields are validated 
     * (if fails- form shows again with an appropriate error message)
     * args: server_side_script name, and %args
     * -calls the appropriate server side script that calls the store function
     * in the relevant perl object.
     * onSuccess - the innerHTML div is updated, else, a JSON alert is issued.
     */
    store: function() {
	//var action = 'store';
	var form = this;
	var editableForm = $(this.getEditableFormId());
	MochiKit.Logging.log("Store function found editableFormId", this.getEditableFormId());
	
	new Ajax.Request(this.getAjaxScript(), {
		parameters: $(editableForm).serialize(true) ,
		    onSuccess: function(response) {
		    var json = response.responseText;
		    var x = jQuery.parseJSON( json ); 
		    //var x = eval("("+json+")");
		    //alert("ajax request succeeded: " + x);
				    
		    if (x.error) { 
			alert(x.error); 
		    } else if (x.refering_page) {  window.location = x.refering_page ; } 
		    else if (x.html) { $(form.getFormId() ).innerHTML = x.html + form.getFormButtons(); }
		    else { form.printForm("view"); }
		},
		    onFailure: function(response) {
		    //alert("Script " + form.getAjaxScript() + " failed!!!" ) ;
		    alert("Action '" + action +"'  failed for storing this " + this.getObjectName() + "!!" ) ;
		    var json = response.responseText;
		    var x = jQuery.parseJSON( json );
		},
		    });
    },
    
    printForm: function( action) {
	var form = this; //'this' cannot be used inside the inner onSuccess function
	if (!action) action = 'view';
	MochiKit.Logging.log("printForm: action = " , action);
	if (!action || !this.getObjectName() || !this.getAjaxScript() )  {
	    alert("Cannot print from without a objectName, action, and ajaxScript name ! ");
	} else if  (action == 'delete') { 
	    this.printDeleteDialog();
	}else {

	    new Ajax.Request(this.getAjaxScript(), {

		    parameters: {  object_id: this.getObjectId() , action: action },
		    onSuccess: function(response) {
			var json = response.responseText;
			var x = eval ("("+json+")");
			if (x.login) { 	
			    window.location =  '/solpeople/login.pl' ;
			    x.error = undef;
			}
			if (x.error) { alert("error: " +x.error); }
			else if (x.reload) { MochiKit.Logging.log("deleted locus...", x.reload); window.location.reload(); } 
			else {
			    form.setUserType(x.user_type);
			    form.setIsOwner(x.is_owner);
			    form.setEditableFormId(x.editable_form_id);
			    //alert('Editable form: '+x.editable_form_id);
			    form.printEditLinks(action);
			    form.printFormButtons();
			    MochiKit.Logging.log("this editable_form_id is ... " , form.getEditableFormId() );
			    
			    $(form.getFormId() ).innerHTML = x.html + form.getFormButtons();
			}
		    },
			onFailure: function(response) {
			alert("Action '" + action +"'  failed for storing this " + this.getObjectName() + "!!" ) ;
		    },
			});
	}
    },

    printDeleteDialog: function() {
	var deleteDialog =  
	'<b>Delete this ' + this.getObjectName()  + '?</b> '; 
	deleteDialog += '<input type =\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.printForm(\'confirm_delete\')\" value=\"Confirm delete\"/><br><br>';
	this.printEditLinks('delete');
	$(this.getFormId() ).innerHTML = deleteDialog;
	
    },
    
    defineButtons: function() { 
	this.setNewButton('<a href= \"javascript:onClick=' +  this.getJsObjectName() + '.reloadNewPage()  \">[New]</a> ');

	this.setGhostedNewButton(' <span class="ghosted">[New]</span> ');

	this.setEditButton(' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm(\'edit\')\">[Edit]</a> ');
	
	this.setGhostedEditButton(' <span class=\"ghosted\">[Edit]</span> ');
	
	this.setCancelEditButton(' <a href= \"javascript:onClick='+this.getJsObjectName()+'.render() \">[Cancel]</a> ');

	this.setDeleteButton(' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm(\'delete\')\">[Delete]</a> ');

	this.setCancelDeleteButton(' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.render()\">[Cancel Delete]</a> ');

	this.setGhostedDeleteButton(' <span class=\"ghosted\">[Delete]</span> ');
    },

    printFormButtons: function() {
	var action = this.getAction();
	var buttons='';
	//if ((this.getUserType() == "curator") || this.getIsOwner() == 1 ) {
	if (action == 'edit' ||  action == 'new' ) { 
	    buttons = '<input type=\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.store()\" value=\"Store\"/>';
	    buttons +='<input type=\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.render( \'edit\' )\" value=\"Reset form\"/>';
	    
	    //}
	}
	this.setFormButtons(buttons);
    },
    
    
    printEditLinks: function(action, newButton, editButton, deleteButton) {
	this.setAction(action);
	MochiKit.Logging.log("printEditLinks action = " , action );

	buttonHTML = '';

	if (action == 'edit') { 
	    buttonHTML = this.getGhostedNewButton() + this.getCancelEditButton() + this.getGhostedDeleteButton();
	}

	if (action == 'new' || action == 'view') { 
	    buttonHTML = this.getGhostedNewButton() + this.getGhostedEditButton() + this.getGhostedDeleteButton();
	}


	if (action == 'view' &&  ((this.getUserType() == 'curator') || (this.getIsOwner == 1))) { 
	    buttonHTML = this.getNewButton() + this.getEditButton() + this.getDeleteButton();
	}

	if (action == 'delete') { 
	    buttonHTML = this.getGhostedNewButton() + this.getGhostedEditButton() + this.getCancelDeleteButton();

	}

	//if (!newButton)   this.printNewButton();
	//else this.setNewButton(newButton);<commit_msg>fixed issue with printing edit links for owners<commit_after>
/** 
* @class JSFormPage
* A Javascript SimpleFormPage for generating Ajax forms 
* @author <NAME> <<EMAIL>>
*
*This javascript object deals with dynamic printing  
*of static/editable forms
*
*JSFormPage.js object is instantiated from CXGN::Page::Form::JSFormPage.pm
*/

JSAN.use('jQuery');
JSAN.use('Prototype');

if (!CXGN) CXGN = function() {};
if (!CXGN.Page) CXGN.Page = function() {};
if (!CXGN.Page.Form) CXGN.Page.Form = function() {};

CXGN.Page.Form.JSFormPage = function(id,  name,  script, formId, jsObjectName, pageName) {
    //alert('In constructor.');
   
    this.setObjectName(name);
    this.setObjectId(id);
    this.setAjaxScript(script);
    this.setFormId(formId);
    this.setJsObjectName(jsObjectName);
    this.setPageName(pageName);
    this.defineButtons();
};


CXGN.Page.Form.JSFormPage.prototype = { 
    
    render: function(action) {
       //render the form here
	if (!action) action  = "view";
	if (!this.getObjectId()) action = "new";
	//	MochiKit.Logging.log("FormId = " + this.formId);
	this.printForm( action);
    },
    

        
    /**
     * store
     * store the information from the form into the database
     * -checks privileges
     * -form fields are validated 
     * (if fails- form shows again with an appropriate error message)
     * args: server_side_script name, and %args
     * -calls the appropriate server side script that calls the store function
     * in the relevant perl object.
     * onSuccess - the innerHTML div is updated, else, a JSON alert is issued.
     */
    store: function() {
	//var action = 'store';
	var form = this;
	var editableForm = $(this.getEditableFormId());
	MochiKit.Logging.log("Store function found editableFormId", this.getEditableFormId());
	
	new Ajax.Request(this.getAjaxScript(), {
		parameters: $(editableForm).serialize(true) ,
		    onSuccess: function(response) {
		    var json = response.responseText;
		    var x = jQuery.parseJSON( json ); 
		    //var x = eval("("+json+")");
		    //alert("ajax request succeeded: " + x);
				    
		    if (x.error) { 
			alert(x.error); 
		    } else if (x.refering_page) {  window.location = x.refering_page ; } 
		    else if (x.html) { $(form.getFormId() ).innerHTML = x.html + form.getFormButtons(); }
		    else { form.printForm("view"); }
		},
		    onFailure: function(response) {
		    //alert("Script " + form.getAjaxScript() + " failed!!!" ) ;
		    alert("Action '" + action +"'  failed for storing this " + this.getObjectName() + "!!" ) ;
		    var json = response.responseText;
		    var x = jQuery.parseJSON( json );
		},
		    });
    },
    
    printForm: function( action) {
	var form = this; //'this' cannot be used inside the inner onSuccess function
	if (!action) action = 'view';
	MochiKit.Logging.log("printForm: action = " , action);
	if (!action || !this.getObjectName() || !this.getAjaxScript() )  {
	    alert("Cannot print from without a objectName, action, and ajaxScript name ! ");
	} else if  (action == 'delete') { 
	    this.printDeleteDialog();
	}else {

	    new Ajax.Request(this.getAjaxScript(), {

		    parameters: {  object_id: this.getObjectId() , action: action },
		    onSuccess: function(response) {
			var json = response.responseText;
			var x = eval ("("+json+")");
			if (x.login) { 	
			    window.location =  '/solpeople/login.pl' ;
			    x.error = undef;
			}
			if (x.error) { alert("error: " +x.error); }
			else if (x.reload) { MochiKit.Logging.log("deleted locus...", x.reload); window.location.reload(); } 
			else {
			    form.setUserType(x.user_type);
			    form.setIsOwner(x.is_owner);
			    form.setEditableFormId(x.editable_form_id);
			    //alert('Editable form: '+x.editable_form_id);
			    form.printEditLinks(action);
			    form.printFormButtons();
			    MochiKit.Logging.log("this editable_form_id is ... " , form.getEditableFormId() );
			    
			    $(form.getFormId() ).innerHTML = x.html + form.getFormButtons();
			}
		    },
			onFailure: function(response) {
			alert("Action '" + action +"'  failed for storing this " + this.getObjectName() + "!!" ) ;
		    },
			});
	}
    },

    printDeleteDialog: function() {
	var deleteDialog =  
	'<b>Delete this ' + this.getObjectName()  + '?</b> '; 
	deleteDialog += '<input type =\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.printForm(\'confirm_delete\')\" value=\"Confirm delete\"/><br><br>';
	this.printEditLinks('delete');
	$(this.getFormId() ).innerHTML = deleteDialog;
	
    },
    
    defineButtons: function() { 
	this.setNewButton('<a href= \"javascript:onClick=' +  this.getJsObjectName() + '.reloadNewPage()  \">[New]</a> ');

	this.setGhostedNewButton(' <span class="ghosted">[New]</span> ');

	this.setEditButton(' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm(\'edit\')\">[Edit]</a> ');
	
	this.setGhostedEditButton(' <span class=\"ghosted\">[Edit]</span> ');
	
	this.setCancelEditButton(' <a href= \"javascript:onClick='+this.getJsObjectName()+'.render() \">[Cancel]</a> ');

	this.setDeleteButton(' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm(\'delete\')\">[Delete]</a> ');

	this.setCancelDeleteButton(' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.render()\">[Cancel Delete]</a> ');

	this.setGhostedDeleteButton(' <span class=\"ghosted\">[Delete]</span> ');
    },

    printFormButtons: function() {
	var action = this.getAction();
	var buttons='';
	//if ((this.getUserType() == "curator") || this.getIsOwner() == 1 ) {
	if (action == 'edit' ||  action == 'new' ) { 
	    buttons = '<input type=\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.store()\" value=\"Store\"/>';
	    buttons +='<input type=\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.render( \'edit\' )\" value=\"Reset form\"/>';
	    
	    //}
	}
	this.setFormButtons(buttons);
    },
    
    
    printEditLinks: function(action, newButton, editButton, deleteButton) {
	this.setAction(action);
	MochiKit.Logging.log("printEditLinks action = " , action );

	buttonHTML = '';

	if (action == 'edit') { 
	    buttonHTML = this.getGhostedNewButton() + this.getCancelEditButton() + this.getGhostedDeleteButton();
	}

	if (action == 'new' || action == 'view') { 
	    buttonHTML = this.getGhostedNewButton() + this.getGhostedEditButton() + this.getGhostedDeleteButton();
	}
	
	if (action == 'view' &&  ((this.getUserType() == 'curator') || (this.getIsOwner() == 1))) { 
	    buttonHTML = this.getNewButton() + this.getEditButton() + this.getDeleteButton();
	}

	if (action == 'delete') { 
	    buttonHTML = this.getGhostedNewButton() + this.getGhostedEditButton() + this.getCancelDeleteButton();

	}

	//if (!newButton)   this.printNewButton();
	//else this.setNewButton(newButton);
{'fraction_non_alphanumeric': 0.17804590935844614, 'fraction_numerical': 0.0002942907592701589, 'mean_word_length': 3.439255388634879, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, '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': '644532', 'n_tokens_mistral': 4785, 'n_tokens_neox': 4448, 'n_words': 912}