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
);
32 CXGN
.Page
.Form
.JSFormPage
.prototype = {
34 render: function(action
) {
35 //render the form here
36 if (!action
) action
= "view";
37 if (!this.getObjectId()) action
= "new";
38 this.printForm( action
);
45 * store the information from the form into the database
47 * -form fields are validated
48 * (if fails- form shows again with an appropriate error message)
49 * args: server_side_script name, and %args
50 * -calls the appropriate server side script that calls the store function
51 * in the relevant perl object.
52 * onSuccess - the innerHTML div is updated, else, a JSON alert is issued.
55 //var action = 'store';
57 var editableForm
= $(this.getEditableFormId());
58 MochiKit
.Logging
.log("Store function found editableFormId", this.getEditableFormId());
60 new Ajax
.Request(this.getAjaxScript(), {
61 parameters
: $(editableForm
).serialize(true) ,
62 onSuccess: function(response
) {
63 var json
= response
.responseText
;
64 var x
= eval ("("+json
+")");
67 } else if (x
.referring_page
) { window
.location
= x
.referring_page
; }
68 else if (x
.html
) { $(form
.getFormId() ).innerHTML
= x
.html
+ form
.getFormButtons(); }
69 else { form
.printForm("view"); }
71 onFailure: function(response
) {
72 alert("Script " + form
.getAjaxScript() + " failed!!!" ) ;
77 printForm: function( action
) {
78 var form
= this; //'this' cannot be used inside the inner onSuccess function
79 if (!action
) action
= 'view';
80 MochiKit
.Logging
.log("printForm: action = " , action
);
81 if (!action
|| !this.getObjectName() || !this.getAjaxScript() ) {
82 alert("Cannot print from without a objectName, action, and ajaxScript name ! ");
83 } else if (action
== 'delete') {
84 this.printDeleteDialog();
86 new Ajax
.Request(this.getAjaxScript(), {
87 parameters
: { object_id
: this.getObjectId() , action
: action
},
88 onSuccess: function(response
) {
89 var json
= response
.responseText
;
90 var x
= eval ("("+json
+")");
92 window
.location
= '/solpeople/login.pl' ;
95 if (x
.error
) { alert("error: " +x
.error
); }
96 else if (x
.reload
) { MochiKit
.Logging
.log("deleted locus...", x
.reload
); window
.location
.reload(); }
98 form
.setUserType(x
.user_type
);
99 form
.setIsOwner(x
.is_owner
);
100 form
.setEditableFormId(x
.editable_form_id
);
101 form
.printEditLinks(action
);
102 form
.printFormButtons();
103 MochiKit
.Logging
.log("this editable_form_id is ... " , form
.getEditableFormId() );
105 $(form
.getFormId() ).innerHTML
= x
.html
+ form
.getFormButtons();
108 onFailure: function(response
) {
109 alert("Script " + form
.getAjaxScript() + " failed!!!" ) ;
115 printDeleteDialog: function() {
117 '<b>Delete this ' + this.getObjectName() + '?</b> ';
118 deleteDialog
+= '<input type =\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.printForm(\'confirm_delete\')\" value=\"Confirm delete\"/><br><br>';
119 this.printEditLinks('delete');
120 $(this.getFormId() ).innerHTML
= deleteDialog
;
124 printFormButtons: function() {
125 var action
= this.getAction();
127 //if ((this.getUserType() == "curator") || this.getIsOwner() == 1 ) {
128 if (action
== 'edit' || action
== 'new' ) {
129 buttons
= '<input type=\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.store()\" value=\"Store\"/>';
130 buttons
+='<input type=\"button\" onClick=\"javascript:' + this.getJsObjectName() + '.render( \'edit\' )\" value=\"Reset form\"/>';
134 this.setFormButtons(buttons
);
138 printEditLinks: function(action
, newButton
, editButton
, deleteButton
) {
139 this.setAction(action
);
140 MochiKit
.Logging
.log("printEditLinks action = " , action
);
142 if (!newButton
) this.printNewButton();
143 else this.setNewButton(newButton
);
145 if (!editButton
) this.printEditButton();
146 else this.setEditButton(editButton
);
148 if (!deleteButton
) this.printDeleteButton();
149 else this.setDeleteButton(deleteButton
);
152 $(this.formId
+ "_buttons").innerHTML
= this.getNewButton() + this.getEditButton() + this.getDeleteButton();
154 this.setEditLinks(this.getNewButton() + this.getEditButton() + this.getDeleteButton() );
159 printNewButton: function() {
161 var action
= this.getAction();
163 //var newLink = ' <span class="ghosted">[New]</span> ' ;
164 var newLink
= '<a href= \"javascript:onClick=' + this.getJsObjectName() + '.reloadNewPage() \">[New]</a>';
166 if (action
== "edit" || action
== "delete") {
167 newLink
= ' <span class="ghosted">[New]</span> ';
169 if (action
== "new" && (( this.getUserType() == "curator") || this.getIsOwner() ==1 )) {
170 newLink
= '<a href= \"javascript:history.back(1) \">[Cancel]</a> ';
172 this.setNewButton(newLink
);
176 printEditButton: function() {
178 var action
= this.getAction();
180 if ((this.getUserType() == "curator") || this.getIsOwner() ==1 ) {
181 editLink
= ' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm(\'edit\')\">[Edit]</a>' ;
184 editLink
= ' <span class=\"ghosted\">[Edit]</span> ';
188 if (action
== "edit") {
189 editLink
= ' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm( \'view\')\">[Cancel edit]</a> ';
192 if (action
== "new" || action
== "delete") {
193 editLink
= ' <span class=\"ghosted\">[Edit]</span> ';
195 this.setEditButton(editLink
);
199 printDeleteButton: function() {
201 var action
= this.getAction();
203 // if ((this.getUserType() == "curator") || this.getIsOwner() ) {
204 if ((this.getUserType() == "curator") || this.getIsOwner() ==1 ) {
205 deleteLink
= ' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.printForm(\'delete\')\">[Delete]</a>' ;
208 deleteLink
= ' <span class=\"ghosted\">[Delete]</span> ';
211 if (action
== "edit" || action
== "new" ) {
212 deleteLink
= ' <span class=\"ghosted\">[Delete]</span> ';
214 if (action
== "delete" )
215 deleteLink
= ' <a href=\"javascript:onClick=' + this.getJsObjectName() + '.render()\">[Cancel Delete]</a>';
216 // ////////////////////
217 this.setDeleteButton(deleteLink
);
222 reloadNewPage: function() {
223 MochiKit
.Logging
.log("reloadNewPage found page: " , this.getPageName());
224 window
.location
= this.getPageName() + '?action=new' ;
228 //////////////////////////////////////////////////////
229 //accessors for object_id and object_name
230 //every form object should first set the object_name and object_id.
231 //These 2 vars will be used in every server side script called
232 setObjectId: function(objectId
) {
233 this.objectId
= objectId
;
236 getObjectId: function() {
237 return this.objectId
;
240 setObjectName: function(objectName
) {
241 this.objectName
= objectName
;
244 getObjectName: function() {
245 return this.objectName
;
248 ////////////////////////////////////////////////
249 //accessors for the server side script with will handle the form components
250 //and return these as a JSON object
251 getAjaxScript: function() {
252 return this.ajaxScript
;
255 setAjaxScript: function(ajaxScript
) {
256 this.ajaxScript
= ajaxScript
;
258 //////////////////////////
260 getPageName: function() {
261 return this.pageName
;
264 setPageName: function(pageName
) {
265 this.page_name
= pageName
;
269 getFormName: function() {
270 return this.formName
;
273 setFormName: function(formName
) {
274 this.form_name
= formName
;
279 getFormId: function() {
283 setFormId: function(formId
) {
284 this.formId
= formId
;
287 getJsObjectName: function() {
288 return this.jsObjectName
;
291 setJsObjectName: function(jsObjectName
) {
292 this.jsObjectName
= jsObjectName
;
296 getAction: function() {
300 setAction: function(action
) {
301 this.action
= action
;
304 getIsOwner: function() {
308 setIsOwner: function(isOwner
) {
309 this.isOwner
= isOwner
;
312 getUserType: function() {
313 return this.userType
;
316 setUserType: function(userType
) {
317 this.userType
= userType
;
321 getPrimaryKey: function() {
322 return this.primaryKey
;
325 setPrimaryKey: function(primaryKey
) {
326 this.primaryKey
= primaryKey
;
330 getEditLinks: function() {
331 return this.editLinks
;
334 setEditLinks: function(editLinks
) {
335 this.editLinks
= editLinks
;
338 getNewButton: function() {
339 return this.newButton
;
342 setNewButton: function(newButton
) {
343 this.newButton
= newButton
;
347 getEditButton: function() {
348 return this.editButton
;
351 setEditButton: function(editButton
) {
352 this.editButton
= editButton
;
356 getDeleteButton: function() {
357 return this.deleteButton
;
360 setDeleteButton: function(deleteButton
) {
361 this.deleteButton
= deleteButton
;
364 getPageName: function() {
365 return this.pageName
;
368 setPageName: function(pageName
) {
369 this.pageName
= pageName
;
373 getFormButtons: function() {
374 return this.formButtons
;
377 setFormButtons: function(formButtons
) {
378 this.formButtons
= formButtons
;
381 getEditableFormId: function() {
382 return this.editableFormId
;
385 setEditableFormId: function(editableFormId
) {
386 this.editableFormId
= editableFormId
;