4 * A Javascript SimpleFormPage for generating Ajax forms
5 * @author Naama Menda <nm249@cornell.edu>
7 *This javascript object deals with dynamic printing
8 *of static/editable forms
10 *JSFormPage.js object is instantiated from CXGN::Page::Form::JSFormPage.pm
14 JSAN.use('Prototype');
16 if (!CXGN) CXGN = function() {};
17 if (!CXGN.Page) CXGN.Page = function() {};
18 if (!CXGN.Page.Form) CXGN.Page.Form = function() {};
20 CXGN.Page.Form.JSFormPage = function(id, name, script, formId, jsObjectName, pageName) {
21 //alert('In constructor.');
23 this.setObjectName(name);
25 this.setAjaxScript(script);
26 this.setFormId(formId);
27 this.setJsObjectName(jsObjectName);
28 this.setPageName(pageName);
33 CXGN.Page.Form.JSFormPage.prototype = {
35 render: function(action) {
36 //render the form here
37 if (!action) action = "view";
38 if (!this.getObjectId()) action = "new";
39 // MochiKit.Logging.log("FormId = " + this.formId);
40 this.printForm( action);
47 * store the information from the form into the database
49 * -form fields are validated
50 * (if fails- form shows again with an appropriate error message)
51 * args: server_side_script name, and %args
52 * -calls the appropriate server side script that calls the store function
53 * in the relevant perl object.
54 * onSuccess - the innerHTML div is updated, else, a JSON alert is issued.
57 //var action = 'store';
59 var editableForm = $(this.getEditableFormId());
60 MochiKit.Logging.log("Store function found editableFormId", this.getEditableFormId());
62 new Ajax.Request(this.getAjaxScript(), {
63 parameters: $(editableForm).serialize(true) ,
64 onSuccess: function(response) {
65 var json = response.responseText;
66 var x = eval ("("+json+")");
69 } else if (x.refering_page) { window.location = x.refering_page ; }
70 else if (x.html) { $(form.getFormId() ).innerHTML = x.html + form.getFormButtons(); }
71 else { form.printForm("view"); }
73 onFailure: function(response) {
74 alert("Script " + form.getAjaxScript() + " failed!!!" ) ;
79 printForm: function( action) {
80 var form = this; //'this' cannot be used inside the inner onSuccess function
81 if (!action) action = 'view';
82 MochiKit.Logging.log("printForm: action = " , action);
83 if (!action || !this.getObjectName() || !this.getAjaxScript() ) {
84 alert("Cannot print from without a objectName, action, and ajaxScript name ! ");
85 } else if (action == 'delete') {
86 this.printDeleteDialog();
89 new Ajax.Request(this.getAjaxScript(), {
91 parameters: { object_id: this.getObjectId() , action: action },
92 onSuccess: function(response) {
93 var json = response.responseText;
94 var x = eval ("("+json+")");
96 window.location = '/solpeople/login.pl' ;
99 if (x.error) { alert("error: " +x.error); }
100 else if (x.reload) { MochiKit.Logging.log("deleted locus...", x.reload); window.location.reload(); }
102 form.setUserType(x.user_type);
103 form.setIsOwner(x.is_owner);
104 form.setEditableFormId(x.editable_form_id);
105 //alert('Editable form: '+x.editable_form_id);
106 form.printEditLinks(action);
107 form.printFormButtons();
108 MochiKit.Logging.log("this editable_form_id is ... " , form.getEditableFormId() );
110 $(form.getFormId() ).innerHTML = x.html + form.getFormButtons();
113 onFailure: function(response) {
114 alert("Script " + form.getAjaxScript() + " failed!!!" ) ;
120 printDeleteDialog: function() {
122 '<b>Delete this ' + this.getObjectName() + '?</b> ';
123 deleteDialog += '<input type =\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.printForm(\'confirm_delete\')\" value=\"Confirm delete\"/><br><br>';
124 this.printEditLinks('delete');
125 $(this.getFormId() ).innerHTML = deleteDialog;
129 defineButtons: function() {
130 this.setNewButton('<a href= \"javascript:onClick=' + this.getJsObjectName() + '.reloadNewPage() \">[New]</a> ');
132 this.setGhostedNewButton(' <span class="ghosted">[New]</span> ');
134 this.setEditButton(' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm(\'edit\')\">[Edit]</a> ');
136 this.setGhostedEditButton(' <span class=\"ghosted\">[Edit]</span> ');
138 this.setCancelEditButton(' <a href= \"javascript:onClick='+this.getJsObjectName()+'.render() \">[Cancel]</a> ');
140 this.setDeleteButton(' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm(\'delete\')\">[Delete]</a> ');
142 this.setCancelDeleteButton(' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.render()\">[Cancel Delete]</a> ');
144 this.setGhostedDeleteButton(' <span class=\"ghosted\">[Delete]</span> ');
147 printFormButtons: function() {
148 var action = this.getAction();
150 //if ((this.getUserType() == "curator") || this.getIsOwner() == 1 ) {
151 if (action == 'edit' || action == 'new' ) {
152 buttons = '<input type=\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.store()\" value=\"Store\"/>';
153 buttons +='<input type=\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.render( \'edit\' )\" value=\"Reset form\"/>';
157 this.setFormButtons(buttons);
161 printEditLinks: function(action, newButton, editButton, deleteButton) {
162 this.setAction(action);
163 MochiKit.Logging.log("printEditLinks action = " , action );
167 if (action == 'edit') {
168 buttonHTML = this.getGhostedNewButton() + this.getCancelEditButton() + this.getGhostedDeleteButton();
171 if (action == 'new' || action == 'view') {
172 buttonHTML = this.getGhostedNewButton() + this.getGhostedEditButton() + this.getGhostedDeleteButton();
176 if (action == 'view' && ((this.getUserType() == 'curator') || (this.getIsOwner == 1))) {
177 buttonHTML = this.getNewButton() + this.getEditButton() + this.getDeleteButton();
180 if (action == 'delete') {
181 buttonHTML = this.getGhostedNewButton() + this.getGhostedEditButton() + this.getCancelDeleteButton();
185 //if (!newButton) this.printNewButton();
186 //else this.setNewButton(newButton);
188 //if (!editButton) this.printEditButton();
189 //else this.setEditButton(editButton);
191 //if (!deleteButton) this.printDeleteButton();
192 //else this.setDeleteButton(deleteButton);
195 $(this.formId+ "_buttons").innerHTML = buttonHTML;
197 this.setEditLinks(buttonHTML);
202 printNewButton: function() {
204 var action = this.getAction();
207 //var newLink = ' <span class="ghosted">[New]</span> ' ;
208 var newLink = '<a href= \"javascript:onClick=' + this.getJsObjectName() + '.reloadNewPage() \">[New]</a>';
210 if (action == "edit" || action == "delete") {
211 newLink = ' <span class="ghosted">[New]</span> ';
213 if (action == "new" && (( this.getUserType() == "curator") || this.getIsOwner() ==1 )) {
214 newLink = '<a href= \"javascript:history.back(1) \">[Cancel]</a> ';
216 this.setNewButton(newLink);
220 printEditButton: function() {
222 var action = this.getAction();
224 if ((this.getUserType() == "curator") || this.getIsOwner() ==1 ) {
225 editLink = ' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm(\'edit\')\">[Edit]</a>' ;
228 editLink = ' <span class=\"ghosted\">[Edit]</span> ';
232 if (action == "edit") {
233 editLink = ' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm( \'view\')\">[Cancel edit]</a> ';
236 if (action == "new" || action == "delete") {
237 editLink = ' <span class=\"ghosted\">[Edit]</span> ';
239 this.setEditButton(editLink);
243 printDeleteButton: function() {
245 var action = this.getAction();
247 // if ((this.getUserType() == "curator") || this.getIsOwner() ) {
248 if ((this.getUserType() == "curator") || this.getIsOwner() ==1 ) {
249 deleteLink = ' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm(\'delete\')\">[Delete]</a>' ;
252 deleteLink = ' <span class=\"ghosted\">[Delete]</span> ';
255 if (action == "edit" || action == "new" ) {
256 deleteLink = ' <span class=\"ghosted\">[Delete]</span> ';
258 if (action == "delete" )
259 deleteLink = ' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.render()\">[Cancel Delete]</a>';
260 // ////////////////////
261 this.setDeleteButton(deleteLink);
266 reloadNewPage: function() {
267 MochiKit.Logging.log("reloadNewPage found page: " , this.getPageName());
268 window.location = this.getPageName() + '?action=new' ;
272 //////////////////////////////////////////////////////
273 //accessors for object_id and object_name
274 //every form object should first set the object_name and object_id.
275 //These 2 vars will be used in every server side script called
276 setObjectId: function(objectId) {
277 this.objectId = objectId;
280 getObjectId: function() {
281 return this.objectId;
284 setObjectName: function(objectName) {
285 this.objectName = objectName;
288 getObjectName: function() {
289 return this.objectName;
292 ////////////////////////////////////////////////
293 //accessors for the server side script with will handle the form components
294 //and return these as a JSON object
295 getAjaxScript: function() {
296 return this.ajaxScript;
299 setAjaxScript: function(ajaxScript) {
300 this.ajaxScript = ajaxScript;
302 //////////////////////////
304 getPageName: function() {
305 return this.pageName;
308 setPageName: function(pageName) {
309 this.page_name = pageName;
313 getFormName: function() {
314 return this.formName;
317 setFormName: function(formName) {
318 this.form_name = formName;
323 getFormId: function() {
327 setFormId: function(formId) {
328 this.formId = formId;
331 getJsObjectName: function() {
332 return this.jsObjectName;
335 setJsObjectName: function(jsObjectName) {
336 this.jsObjectName = jsObjectName;
340 getAction: function() {
344 setAction: function(action) {
345 this.action = action;
348 getIsOwner: function() {
352 setIsOwner: function(isOwner) {
353 this.isOwner = isOwner;
356 getUserType: function() {
357 return this.userType;
360 setUserType: function(userType) {
361 this.userType = userType;
365 getPrimaryKey: function() {
366 return this.primaryKey;
369 setPrimaryKey: function(primaryKey) {
370 this.primaryKey = primaryKey;
374 getEditLinks: function() {
375 return this.editLinks;
378 setEditLinks: function(editLinks) {
379 this.editLinks = editLinks;
382 getNewButton: function() {
383 return this.newButton;
386 setNewButton: function(newButton) {
387 this.newButton = newButton;
390 setGhostedNewButton: function(ghostedNewButton) {
391 this.ghostedNewButton = ghostedNewButton;
394 getGhostedNewButton: function() {
395 return this.ghostedNewButton;
399 setCancelEditButton: function(cancelEditButton) {
400 this.cancelEditButton = cancelEditButton;
403 getCancelEditButton: function() {
404 return this.cancelEditButton;
407 setGhostedEditButton: function(ghostedEditButton) {
408 this.ghostedEditButton = ghostedEditButton;
411 getGhostedEditButton: function() {
412 return this.ghostedEditButton;
415 getEditButton: function() {
416 return this.editButton;
419 setEditButton: function(editButton) {
420 this.editButton = editButton;
424 getDeleteButton: function() {
425 return this.deleteButton;
428 setDeleteButton: function(deleteButton) {
429 this.deleteButton = deleteButton;
432 setGhostedDeleteButton: function(ghostedDeleteButton) {
433 this.ghostedDeleteButton = ghostedDeleteButton;
436 getGhostedDeleteButton: function() {
437 return this.ghostedDeleteButton;
441 setCancelDeleteButton: function(cancelDeleteButton) {
442 this.cancelDeleteButton = cancelDeleteButton;
445 getCancelDeleteButton: function() {
446 return this.cancelDeleteButton;
451 getPageName: function() {
452 return this.pageName;
455 setPageName: function(pageName) {
456 this.pageName = pageName;
460 getFormButtons: function() {
461 return this.formButtons;
464 setFormButtons: function(formButtons) {
465 this.formButtons = formButtons;
468 getEditableFormId: function() {
469 return this.editableFormId;
472 setEditableFormId: function(editableFormId) {
473 this.editableFormId = editableFormId;