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 = {};
}
Survey.Data = new function(){
Survey.Data = (function(){
var lastDataSet = {};
var focus;
var lastId = -1;
this.dragDrop = function(did){
var type;
YAHOO.log('In drag drop');
if(did.className.match("section")){type = 'section';}
else if(did.className.match("question")){type = 'question';}
else{ type = 'answer';}
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;
return {
dragDrop: function(did){
var type;
YAHOO.log('In drag drop');
if (did.className.match("section")) {
type = 'section';
}
var before = before.previousSibling;
}
var data = {id:'',type:''};
if(before != undefined && before.id != undefined && 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(){
Survey.Comm.loadSurvey(this.id);
}
this.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;
else
if (did.className.match("question")) {
type = 'question';
}
else {
type = 'answer';
}
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;
}
before = before.previousSibling;
}
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);
},
//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");
}
clicked: function(){
Survey.Comm.loadSurvey(this.id);
},
//add the add object buttons
// if(d.buttons['section']){
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");
}
}
//add the add object buttons
// if(d.buttons['section']){
document.getElementById('addSection').innerHTML = '';
document.getElementById('addQuestion').innerHTML = '';
document.getElementById('addAnswer').innerHTML = '';
var button = new YAHOO.widget.Button({ label:"Add Section", id:"addsection", container:"addSection" });
button.on("click", this.addSection);
// }
// if(d.buttons['question']){
var button = new YAHOO.widget.Button({ label:"Add Question", id:"addquestion", container:"addQuestion" });
button.on("click", this.addQuestion,d.buttons['question']);
// }
if(d.buttons['answer']){
var button = new YAHOO.widget.Button({ label:"Add Answer", id:"addanswer", container:"addAnswer" });
button.on("click", this.addAnswer,d.buttons['answer']);
}
if(showEdit == 1){
this.loadObjectEdit(d.edithtml,d.type);
// 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);
}
}else{
document.getElementById('edit').innerHTML = "";
}
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);
var sButton = new YAHOO.widget.Button({
label: "Add Section",
id: "addsection",
container: "addSection"
});
sButton.on("click", this.addSection);
// }
// if(d.buttons['question']){
var qButton = new YAHOO.widget.Button({
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) {
this.loadObjectEdit(d.edithtml, d.type);
// 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);
}
}
else {
document.getElementById('edit').innerHTML = "";
}
lastDataSet = d;
},
initHandler: function(){
new YAHOO.util.DDTarget("sections","sections");
Survey.Comm.loadSurvey();
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);
}
}
}();
};
})();
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 = {};
}
Survey.ObjectTemplate = new function(){
Survey.ObjectTemplate = (function(){
this.loadObject = function(html,type){
document.getElementById('edit').innerHTML = html;
var myTextarea;
var handleSubmit = function(){
myTextarea.saveHTML();
this.submit();
}
var butts = [
{ text:"Submit", handler:handleSubmit, isDefault:true },
{ text:"Copy", 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();}}
];
if (type !== 'answer') {
butts.push({
text: "Preview",
handler: jumpTo
});
}
var dialog = new YAHOO.widget.Dialog(type,
{
width : "600px",
context: [document.body, 'tr', 'tr'],
visible : false,
constraintoviewport : true,
buttons : butts
} );
dialog.callback = Survey.Comm.callback;
dialog.render();
function jumpTo() {
window.location.search = 'func=jumpTo;id=' + dialog.form.id.value;
}
var textareaId = type+'Text';
var textarea = YAHOO.util.Dom.get(textareaId);
var height = YAHOO.util.Dom.getStyle(textarea,'height');
if (height == ''){
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')) {
myTextarea.get('toolbar').titlebar = false;
}
myTextarea.render();
dialog.show();
}
}();
var editor;
var dialog;
return {
loadObject: function(html, type){
document.getElementById('edit').innerHTML = html;
var btns = [{
text: "Submit",
handler: function(){
editor.saveHTML();
this.submit();
},
isDefault: true
}, {
text: "Copy",
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();
}
}];
dialog = new YAHOO.widget.Dialog(type, {
width: "600px",
context: [document.body, 'tr', 'tr'],
visible: false,
constraintoviewport: true,
buttons: btns
});
if (type !== 'answer') {
btns.push({
text: "Preview",
handler: function(){
window.location.search = 'func=jumpTo;id=' + dialog.getData().id;
}
});
}
dialog.callback = Survey.Comm.callback;
dialog.render();
var textareaId = type + 'Text';
var textarea = YAHOO.util.Dom.get(textareaId);
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();
}
};
})();