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';
}
else {
type = 'answer';
}
var data = {id:'',type:''}; var first = {
id: did.id,
type: type
};
var before = document.getElementById(did.id).previousSibling;
if(before != undefined && before.id != undefined && before.id != ''){ while (1) {
if(before.className.match("section")){type = 'section';} if (before === undefined || (before.id !== undefined && before.id !== '')) {
else if(before.className.match("question")){type = 'question';} break;
else{ type = 'answer';} }
data = {id:before.id,type:type}; before = before.previousSibling;
} }
YAHOO.log(first.id+' '+data.id);
Survey.Comm.dragDrop(first,data);
}
var data = {
id: '',
type: ''
};
if (before && before.id !== '') {
if (before.className.match("section")) {
type = 'section';
}
else
if (before.className.match("question")) {
type = 'question';
}
else {
type = 'answer';
}
data = {
id: before.id,
type: type
};
}
YAHOO.log(first.id + ' ' + data.id);
Survey.Comm.dragDrop(first, data);
},
this.clicked = function(){ clicked: function(){
Survey.Comm.loadSurvey(this.id); Survey.Comm.loadSurvey(this.id);
} },
loadData: function(d){
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");
}
}
this.loadData = function(d){ //add the add object buttons
focus = d.address;//What is the current highlighted item. // if(d.buttons['section']){
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){
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
// 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"
});
qButton.on("click", this.addQuestion, d.buttons.question);
// }
if (d.buttons.answer) {
var aButton = new YAHOO.widget.Button({
label: "Add Answer",
id: "addanswer",
container: "addAnswer"
});
aButton.on("click", this.addAnswer, d.buttons.answer);
}
if(showEdit == 1){ if (showEdit == 1) {
this.loadObjectEdit(d.edithtml,d.type); this.loadObjectEdit(d.edithtml, d.type);
// build the goto auto-complete widget // build the goto auto-complete widget
if (d.gotoTargets && document.getElementById('goto')) { if (d.gotoTargets && document.getElementById('goto')) {
var ds = new YAHOO.util.LocalDataSource(d.gotoTargets); var ds = new YAHOO.util.LocalDataSource(d.gotoTargets);
var ac = new YAHOO.widget.AutoComplete('goto', 'goto-yui-ac-container', ds); var ac = new YAHOO.widget.AutoComplete('goto', 'goto-yui-ac-container', ds);
} }
}else{ }
document.getElementById('edit').innerHTML = ""; else {
} document.getElementById('edit').innerHTML = "";
lastDataSet = d; }
} lastDataSet = d;
this.addSection = function(){
Survey.Comm.newSection();
}
this.addQuestion = function(e,id){
Survey.Comm.newQuestion(id);
}
this.addAnswer = function(e,id){
Survey.Comm.newAnswer(id);
}
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");
Survey.Comm.loadSurvey();
}
}
}();
Survey.OnLoad.init(); addSection: function(){
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);
}
};
})();
// 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(){ document.getElementById('edit').innerHTML = html;
myTextarea.saveHTML();
this.submit();
}
var butts = [ var btns = [{
{ text:"Submit", handler:handleSubmit, isDefault:true }, text: "Submit",
{ text:"Copy", handler:function(){document.getElementById('copy').value = 1; this.submit();}}, handler: function(){
{ text:"Cancel", handler:function(){this.cancel();}}, editor.saveHTML();
{ text:"Delete", handler:function(){document.getElementById('delete').value = 1; this.submit();}} this.submit();
]; },
if (type !== 'answer') { isDefault: true
butts.push({ }, {
text: "Preview", text: "Copy",
handler: jumpTo handler: function(){
}); document.getElementById('copy').value = 1;
} this.submit();
}
}, {
text: "Cancel",
handler: function(){
this.cancel();
}
}, {
text: "Delete",
handler: function(){
document.getElementById('delete').value = 1;
this.submit();
}
}];
var dialog = new YAHOO.widget.Dialog(type, dialog = new YAHOO.widget.Dialog(type, {
{ width: "600px",
width : "600px", context: [document.body, 'tr', 'tr'],
context: [document.body, 'tr', 'tr'], visible: false,
visible : false, constraintoviewport: true,
constraintoviewport : true, buttons: btns
buttons : butts });
} );
dialog.callback = Survey.Comm.callback; if (type !== 'answer') {
dialog.render(); btns.push({
function jumpTo() { text: "Preview",
window.location.search = 'func=jumpTo;id=' + dialog.form.id.value; handler: function(){
} window.location.search = 'func=jumpTo;id=' + dialog.getData().id;
}
});
}
var textareaId = type+'Text'; dialog.callback = Survey.Comm.callback;
var textarea = YAHOO.util.Dom.get(textareaId); dialog.render();
var height = YAHOO.util.Dom.getStyle(textarea,'height'); var textareaId = type + 'Text';
if (height == ''){ var textarea = YAHOO.util.Dom.get(textareaId);
height = '300px';
}
myTextarea = new YAHOO.widget.SimpleEditor(textareaId, {
height: height,
width: '100%',
dompath: false //Turns on the bar at the bottom
});
if (myTextarea.get('toolbar')) { var height = YAHOO.util.Dom.getStyle(textarea, 'height');
myTextarea.get('toolbar').titlebar = false; if (!height) {
} height = '300px';
myTextarea.render(); }
editor = new YAHOO.widget.SimpleEditor(textareaId, {
height: height,
width: '100%',
dompath: false //Turns on the bar at the bottom
});
dialog.show(); if (editor.get('toolbar')) {
} editor.get('toolbar').titlebar = false;
}(); }
editor.render();
dialog.show();
}
};
})();