Removed unnecessary <br>'s from Section list Made sections panel resizable Made current selection highlighted via CSS
197 lines
6.6 KiB
JavaScript
197 lines
6.6 KiB
JavaScript
/*global Survey, YAHOO */
|
|
if (typeof Survey === "undefined") {
|
|
var Survey = {};
|
|
}
|
|
|
|
Survey.Data = (function(){
|
|
|
|
var lastDataSet = {};
|
|
var focus;
|
|
var lastId = -1;
|
|
|
|
// Keep references to widgets here so that we can destory any instances before
|
|
// creating new ones (to avoid memory leaks)
|
|
var autoComplete;
|
|
var sButton, qButton, aButton;
|
|
|
|
return {
|
|
ddContainer:null,
|
|
dragDrop: function(did){
|
|
YAHOO.log('In drag drop');
|
|
var type = did.className.match("section") ? 'section'
|
|
: did.className.match("question") ? 'question'
|
|
: 'answer';
|
|
|
|
var first = {
|
|
id: did.id, // pre-drag index of item
|
|
type: type
|
|
};
|
|
var before = YAHOO.util.Dom.getPreviousSiblingBy( document.getElementById(did.id), function(node){
|
|
return node.id; // true iff node has a non-empty id
|
|
});
|
|
|
|
var data = {
|
|
id: '',
|
|
type: ''
|
|
};
|
|
|
|
if (before) {
|
|
type = before.className.match("section") ? 'section'
|
|
: before.className.match("question") ? 'question'
|
|
: 'answer';
|
|
data = {
|
|
id: before.id,
|
|
type: type
|
|
};
|
|
}
|
|
YAHOO.log(first.id + ' ' + data.id);
|
|
Survey.Comm.dragDrop(first, data);
|
|
},
|
|
|
|
clicked: function(){
|
|
Survey.Comm.loadSurvey(this.id);
|
|
},
|
|
|
|
loadData: function(d){
|
|
focus = d.address;//What is the current highlighted item.
|
|
var warnings = "";
|
|
for(var w in d.warnings){
|
|
warnings += "<div class='warning'>" + d.warnings[w] + "</div>";
|
|
}
|
|
if (document.getElementById('warnings')) {
|
|
if (warnings !== "") {
|
|
document.getElementById('warnings').innerHTML = warnings;
|
|
YAHOO.util.Dom.setStyle('warnings-outer', 'display', 'block');
|
|
}
|
|
else {
|
|
YAHOO.util.Dom.setStyle('warnings-outer', 'display', 'none');
|
|
}
|
|
}
|
|
var showEdit = 1;
|
|
if (lastId.toString() === d.address.toString()) {
|
|
showEdit = 0;
|
|
lastId = -1;
|
|
}
|
|
else {
|
|
lastId = d.address;
|
|
}
|
|
|
|
// First purge any event handlers bound to sections node..
|
|
YAHOO.util.Event.purgeElement('sections', true);
|
|
|
|
if (!Survey.Data.ddContainer) {
|
|
Survey.Data.ddContainer = new YAHOO.widget.Panel("sections", {
|
|
width: "400px",
|
|
height: "600px",
|
|
draggable: true,
|
|
visible: true,
|
|
bodyStyle: { 'margin-right' : '5px' }
|
|
});
|
|
|
|
Survey.Data.ddContainer.setHeader("Survey Objects...");
|
|
Survey.Data.ddContainer.setBody(d.ddhtml);
|
|
Survey.Data.ddContainer.setFooter(document.getElementById("buttons"));
|
|
Survey.Data.ddContainer.render();//document.body);
|
|
}
|
|
else {
|
|
Survey.Data.ddContainer.setBody(d.ddhtml);
|
|
Survey.Data.ddContainer.setFooter(document.getElementById("buttons"));
|
|
}
|
|
|
|
// (re)Add resize handler
|
|
Survey.Data.ddContainerResize && Survey.Data.ddContainerResize.destroy();
|
|
Survey.Data.ddContainerResize = new YAHOO.util.Resize('sections', {
|
|
proxy: true
|
|
});
|
|
Survey.Data.ddContainerResize.on('resize', function(args){
|
|
Survey.Data.ddContainer.cfg.setProperty("height", args.height + "px");
|
|
});
|
|
|
|
//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");
|
|
}
|
|
}
|
|
var selectedId = focus.join('-');
|
|
selectedId = selectedId === 'undefined' ? "0" : selectedId;
|
|
if (document.getElementById(selectedId)) {
|
|
YAHOO.util.Dom.addClass(selectedId, 'selected');
|
|
}
|
|
|
|
sButton && sButton.destroy();
|
|
sButton = new YAHOO.widget.Button({
|
|
label: "Add Section",
|
|
id: "addsection",
|
|
container: "addSection"
|
|
});
|
|
sButton.on("click", this.addSection);
|
|
|
|
qButton && qButton.destroy();
|
|
qButton = new YAHOO.widget.Button({
|
|
label: "Add Question",
|
|
id: "addquestion",
|
|
container: "addQuestion"
|
|
});
|
|
qButton.on("click", this.addQuestion, d.buttons.question);
|
|
|
|
if (d.buttons.answer) {
|
|
aButton && aButton.destroy();
|
|
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);
|
|
autoComplete = new YAHOO.widget.AutoComplete('goto', 'goto-yui-ac-container', ds);
|
|
}
|
|
}
|
|
else {
|
|
Survey.ObjectTemplate.unloadObject();
|
|
if (autoComplete) {
|
|
autoComplete.destroy();
|
|
autoComplete = null;
|
|
}
|
|
}
|
|
lastDataSet = d;
|
|
},
|
|
|
|
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();
|
|
});
|