Refactored editsurvey js

This commit is contained in:
Patrick Donelan 2009-01-09 07:13:35 +00:00
parent 63e74f14f4
commit 021bac39ec
2 changed files with 228 additions and 187 deletions

View file

@ -1,140 +1,162 @@
if (typeof Survey == "undefined") { /*global Survey, YAHOO */
if (typeof Survey === "undefined") {
var Survey = {}; var Survey = {};
} }
Survey.Data = new function(){ Survey.Data = (function(){
var lastDataSet = {}; var lastDataSet = {};
var focus; var focus;
var lastId = -1; var lastId = -1;
this.dragDrop = function(did){ return {
var type; dragDrop: function(did){
YAHOO.log('In drag drop'); var type;
if(did.className.match("section")){type = 'section';} YAHOO.log('In drag drop');
else if(did.className.match("question")){type = 'question';} if (did.className.match("section")) {
else{ type = 'answer';} type = 'section';
var first = {id:did.id,type:type};
var before = document.getElementById(did.id).previousSibling;
while(1){
if( before == undefined || (before.id != undefined && before.id != '') ){
break;
} }
var before = before.previousSibling; else
} if (did.className.match("question")) {
type = 'question';
var data = {id:'',type:''}; }
else {
if(before != undefined && before.id != undefined && before.id != ''){ type = 'answer';
if(before.className.match("section")){type = 'section';} }
else if(before.className.match("question")){type = 'question';}
else{ type = 'answer';} var first = {
data = {id:before.id,type:type}; id: did.id,
} type: type
YAHOO.log(first.id+' '+data.id); };
Survey.Comm.dragDrop(first,data); var before = document.getElementById(did.id).previousSibling;
}
while (1) {
if (before === undefined || (before.id !== undefined && before.id !== '')) {
break;
this.clicked = function(){ }
Survey.Comm.loadSurvey(this.id); before = before.previousSibling;
} }
var data = {
id: '',
this.loadData = function(d){ type: ''
focus = d.address;//What is the current highlighted item. };
var showEdit = 1;
if(lastId.toString() == d.address.toString()){ if (before && before.id !== '') {
showEdit = 0; if (before.className.match("section")) {
lastId = -1; type = 'section';
}else{ }
lastId = d.address; else
} if (before.className.match("question")) {
document.getElementById('sections').innerHTML=d.ddhtml; type = 'question';
}
else {
type = 'answer';
}
data = {
id: before.id,
type: type
};
}
YAHOO.log(first.id + ' ' + data.id);
Survey.Comm.dragDrop(first, data);
},
//add event handlers for if a tag is clicked clicked: function(){
for(var x in d.ids){ Survey.Comm.loadSurvey(this.id);
YAHOO.log('adding handler for '+ d.ids[x]); },
YAHOO.util.Event.addListener(d.ids[x], "click", this.clicked);
new Survey.DDList(d.ids[x],"sections");
}
//add the add object buttons loadData: function(d){
// if(d.buttons['section']){ focus = d.address;//What is the current highlighted item.
var showEdit = 1;
if (lastId.toString() === d.address.toString()) {
showEdit = 0;
lastId = -1;
}
else {
lastId = d.address;
}
document.getElementById('sections').innerHTML = d.ddhtml;
//add event handlers for if a tag is clicked
for (var x in d.ids) {
if (YAHOO.lang.hasOwnProperty(d.ids, x)) {
YAHOO.log('adding handler for ' + d.ids[x]);
YAHOO.util.Event.addListener(d.ids[x], "click", this.clicked);
var _s = new Survey.DDList(d.ids[x], "sections");
}
}
//add the add object buttons
// if(d.buttons['section']){
document.getElementById('addSection').innerHTML = ''; document.getElementById('addSection').innerHTML = '';
document.getElementById('addQuestion').innerHTML = ''; document.getElementById('addQuestion').innerHTML = '';
document.getElementById('addAnswer').innerHTML = ''; document.getElementById('addAnswer').innerHTML = '';
var button = new YAHOO.widget.Button({ label:"Add Section", id:"addsection", container:"addSection" }); var sButton = new YAHOO.widget.Button({
button.on("click", this.addSection); label: "Add Section",
// } id: "addsection",
// if(d.buttons['question']){ container: "addSection"
var button = new YAHOO.widget.Button({ label:"Add Question", id:"addquestion", container:"addQuestion" }); });
button.on("click", this.addQuestion,d.buttons['question']); sButton.on("click", this.addSection);
// } // }
if(d.buttons['answer']){ // if(d.buttons['question']){
var button = new YAHOO.widget.Button({ label:"Add Answer", id:"addanswer", container:"addAnswer" }); var qButton = new YAHOO.widget.Button({
button.on("click", this.addAnswer,d.buttons['answer']); label: "Add Question",
} id: "addquestion",
container: "addQuestion"
if(showEdit == 1){ });
this.loadObjectEdit(d.edithtml,d.type); qButton.on("click", this.addQuestion, d.buttons.question);
// }
// build the goto auto-complete widget if (d.buttons.answer) {
if (d.gotoTargets && document.getElementById('goto')) { var aButton = new YAHOO.widget.Button({
var ds = new YAHOO.util.LocalDataSource(d.gotoTargets); label: "Add Answer",
var ac = new YAHOO.widget.AutoComplete('goto', 'goto-yui-ac-container', ds); id: "addanswer",
} container: "addAnswer"
}else{ });
document.getElementById('edit').innerHTML = ""; aButton.on("click", this.addAnswer, d.buttons.answer);
} }
lastDataSet = d;
} if (showEdit == 1) {
this.loadObjectEdit(d.edithtml, d.type);
this.addSection = function(){
Survey.Comm.newSection(); // build the goto auto-complete widget
} if (d.gotoTargets && document.getElementById('goto')) {
var ds = new YAHOO.util.LocalDataSource(d.gotoTargets);
var ac = new YAHOO.widget.AutoComplete('goto', 'goto-yui-ac-container', ds);
this.addQuestion = function(e,id){ }
Survey.Comm.newQuestion(id); }
} else {
document.getElementById('edit').innerHTML = "";
this.addAnswer = function(e,id){ }
Survey.Comm.newAnswer(id); lastDataSet = d;
}
this.loadObjectEdit = function(edit,type){
if(edit){
Survey.ObjectTemplate.loadObject(edit,type);
}
}
this.loadLast = function(){
this.loadData(lastDataSet);
}
}();
//----------------------------------------------------------------
//
// Initialize survey
//
//----------------------------------------------------------------
Survey.OnLoad = function() {
var e = YAHOO.util.Event;
return {
init: function() {
e.onDOMReady(this.initHandler);
}, },
initHandler: function(){
new YAHOO.util.DDTarget("sections","sections"); addSection: function(){
Survey.Comm.loadSurvey(); Survey.Comm.newSection();
},
addQuestion: function(e, id){
Survey.Comm.newQuestion(id);
},
addAnswer: function(e, id){
Survey.Comm.newAnswer(id);
},
loadObjectEdit: function(edit, type){
if (edit) {
Survey.ObjectTemplate.loadObject(edit, type);
}
},
loadLast: function(){
this.loadData(lastDataSet);
} }
} };
}(); })();
Survey.OnLoad.init(); // Initialize survey
YAHOO.util.Event.onDOMReady(function(){
var ddTarget = new YAHOO.util.DDTarget("sections", "sections");
Survey.Comm.loadSurvey();
});

View file

@ -1,67 +1,86 @@
if (typeof Survey == "undefined") {
/*global Survey, YAHOO */
if (typeof Survey === "undefined") {
var Survey = {}; var Survey = {};
} }
Survey.ObjectTemplate = new function(){ Survey.ObjectTemplate = (function(){
this.loadObject = function(html,type){ var editor;
var dialog;
document.getElementById('edit').innerHTML = html;
return {
var myTextarea;
loadObject: function(html, type){
var handleSubmit = function(){
myTextarea.saveHTML(); document.getElementById('edit').innerHTML = html;
this.submit();
} var btns = [{
text: "Submit",
var butts = [ handler: function(){
{ text:"Submit", handler:handleSubmit, isDefault:true }, editor.saveHTML();
{ text:"Copy", handler:function(){document.getElementById('copy').value = 1; this.submit();}}, this.submit();
{ text:"Cancel", handler:function(){this.cancel();}}, },
{ text:"Delete", handler:function(){document.getElementById('delete').value = 1; this.submit();}} isDefault: true
]; }, {
if (type !== 'answer') { text: "Copy",
butts.push({ handler: function(){
text: "Preview", document.getElementById('copy').value = 1;
handler: jumpTo this.submit();
}); }
} }, {
text: "Cancel",
var dialog = new YAHOO.widget.Dialog(type, handler: function(){
{ this.cancel();
width : "600px", }
context: [document.body, 'tr', 'tr'], }, {
visible : false, text: "Delete",
constraintoviewport : true, handler: function(){
buttons : butts document.getElementById('delete').value = 1;
} ); this.submit();
}
dialog.callback = Survey.Comm.callback; }];
dialog.render();
function jumpTo() { dialog = new YAHOO.widget.Dialog(type, {
window.location.search = 'func=jumpTo;id=' + dialog.form.id.value; width: "600px",
} context: [document.body, 'tr', 'tr'],
visible: false,
var textareaId = type+'Text'; constraintoviewport: true,
var textarea = YAHOO.util.Dom.get(textareaId); buttons: btns
});
var height = YAHOO.util.Dom.getStyle(textarea,'height');
if (height == ''){ if (type !== 'answer') {
height = '300px'; btns.push({
} text: "Preview",
myTextarea = new YAHOO.widget.SimpleEditor(textareaId, { handler: function(){
height: height, window.location.search = 'func=jumpTo;id=' + dialog.getData().id;
width: '100%', }
dompath: false //Turns on the bar at the bottom });
}); }
if (myTextarea.get('toolbar')) { dialog.callback = Survey.Comm.callback;
myTextarea.get('toolbar').titlebar = false; dialog.render();
}
myTextarea.render(); var textareaId = type + 'Text';
var textarea = YAHOO.util.Dom.get(textareaId);
dialog.show();
} var height = YAHOO.util.Dom.getStyle(textarea, 'height');
}(); if (!height) {
height = '300px';
}
editor = new YAHOO.widget.SimpleEditor(textareaId, {
height: height,
width: '100%',
dompath: false //Turns on the bar at the bottom
});
if (editor.get('toolbar')) {
editor.get('toolbar').titlebar = false;
}
editor.render();
dialog.show();
}
};
})();