337 lines
12 KiB
Text
337 lines
12 KiB
Text
if (typeof Survey == "undefined") {
|
|
var Survey = {};
|
|
}
|
|
|
|
Survey.Data = new function(){
|
|
var lastDataSet = {};
|
|
var focus;
|
|
|
|
this.dragDrop = function(did){
|
|
var type;
|
|
|
|
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;
|
|
}
|
|
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};
|
|
}
|
|
|
|
Survey.Comm.dragDrop(first,data);
|
|
}
|
|
|
|
|
|
|
|
this.clicked = function(){
|
|
Survey.Comm.loadSurvey(this.id);
|
|
}
|
|
|
|
|
|
|
|
this.loadData = function(d){
|
|
focus = d.focus;//What is the current highlighted item.
|
|
var lastType = '';//What was the last type created.
|
|
var lastId = {'section': '', 'question': '', 'answer': ''};//what is the last id of each type placed, so we know a child's parent.
|
|
var buttons = {'question':0,'answer':0,'section':0}; //array of bools on if buttons put down
|
|
document.getElementById('sections').innerHTML='';
|
|
|
|
for(var x in d.data){
|
|
//Now check to see if this is where an add button goes.
|
|
//Add addAnswer when we go from answer to question or section or end
|
|
//Add addQuestion when we go from question to section or end
|
|
|
|
if(lastType == 'answer' && (d.data[x].type == 'question' || d.data[x].type == 'section')){
|
|
this.addAnswerButton(lastId['question']);
|
|
buttons['answer'] = 1;
|
|
}
|
|
else if(lastType == 'question' && d.data[x].type == 'section'){
|
|
this.addQuestionButton(lastId['section']);
|
|
buttons['question'] = 1;
|
|
}
|
|
else if(d.data[x].type == 'section' && lastType == 'section' && lastId['section'] == focus){
|
|
this.addQuestionButton(lastId['section']);
|
|
buttons['question'] = 1;
|
|
}
|
|
|
|
var node = document.createElement('li');
|
|
|
|
if(d.data[x].id == focus){
|
|
node.className = "s"+d.data[x].type;
|
|
}else{
|
|
node.className = d.data[x].type;
|
|
}
|
|
|
|
node.innerHTML = d.data[x].text;
|
|
node.id = d.data[x].id;
|
|
new Survey.DDList(node.id,"sections");
|
|
document.getElementById('sections').appendChild(node);
|
|
YAHOO.util.Event.addListener(d.data[x].id, "click", this.clicked);
|
|
|
|
lastType = d.data[x].type;
|
|
lastId[d.data[x].type] = d.data[x].id;
|
|
}
|
|
if(lastType == 'answer' && ! buttons['answer']){
|
|
this.addAnswerButton(lastId['question']);
|
|
this.addQuestionButton(lastId['section']);
|
|
}else if(lastType == 'question' || lastType == 'section' && ! buttons['question']){
|
|
this.addQuestionButton(lastId['section']);
|
|
}
|
|
|
|
this.addSectionButton();
|
|
|
|
this.loadObjectEdit(d.edit);
|
|
lastDataSet = d;
|
|
}
|
|
|
|
|
|
this.addSection = function(){
|
|
Survey.Comm.newSection();
|
|
}
|
|
|
|
|
|
this.addQuestion = function(e,sid){
|
|
Survey.Comm.newQuestion(sid);
|
|
}
|
|
|
|
|
|
this.addSectionButton = function(){
|
|
var node = document.createElement('li');
|
|
node.innerHTML = "<span id='newSection'></span>";
|
|
document.getElementById('sections').appendChild(node);
|
|
var button = new YAHOO.widget.Button({ label:"Add Section", id:"addsection", container:"newSection" });
|
|
button.on("click", this.addSection);
|
|
}
|
|
|
|
|
|
this.addQuestionButton = function(qid){
|
|
var node = document.createElement('li');
|
|
node.className = 'newQuestion';
|
|
node.innerHTML = "<span id='newQuestion'></span>";
|
|
document.getElementById('sections').appendChild(node);
|
|
var button = new YAHOO.widget.Button({ label:"Add Question", id:'addquestion', container:"newQuestion"});//, onclick:{fn:this.addQuestion} });
|
|
button.on("click", this.addQuestion,qid);
|
|
}
|
|
|
|
|
|
this.addAnswerButton = function(aid){
|
|
var node = document.createElement('li');
|
|
node.id = 'newAnswer';
|
|
node.className = 'newAnswer';
|
|
document.getElementById('sections').appendChild(node);
|
|
var button = new YAHOO.widget.Button({ label:"Add Answer", id:aid, container:"newAnswer" });
|
|
button.on("click", this.addAnswer);
|
|
}
|
|
|
|
|
|
this.loadObjectEdit = function(edit){
|
|
if(edit){
|
|
console.log('was an edit'+edit.type);
|
|
if(edit.type == "loadSection"){
|
|
console.log('loadsection');
|
|
Survey.SectionTemplate.loadSection(edit.params);
|
|
}
|
|
else if(edit.type == "loadQuestion"){
|
|
console.log('loadquestion');
|
|
Survey.QuestionTemplate.loadQuestion(edit.params);
|
|
}
|
|
else if(edit.type == "loadAnswer"){
|
|
Survey.AnswerTemplate.loadAnswer(edit.params);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
this.loadLast = function(){
|
|
this.loadData(lastDataSet);
|
|
}
|
|
}();
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
|
|
/*Survey.SectionTemplate = new function(){
|
|
this.qpp;
|
|
this.randomizeQuestions;
|
|
this.questionsOnSectionPage;
|
|
|
|
this.returnData = function(){
|
|
var data = {};
|
|
data['type'] = "section";
|
|
data["sectionText"] = document.getElementById('text').value;
|
|
data['sectionName'] = document.getElementById('sectionName').value;
|
|
data['questionsPerPage'] = this.qpp.get('label');
|
|
data['randomizeQuestions'] = this.randomizeQuestions.get('checkedButton').get('value');
|
|
data['questionsOnSectionPage'] = this.questionsOnSectionPage.get('checkedButton').get('value');
|
|
data['Survey_sectionId'] = document.getElementById('Survey_sectionId').value;
|
|
Survey.Comm.submitEdit(data);
|
|
return data;
|
|
}
|
|
this.qppClick = function(p_sType, p_aArgs, p_oItem) {
|
|
Survey.SectionTemplate.qpp.set("label", p_oItem.cfg.getProperty("text"));
|
|
}
|
|
this.loadSection = function(params){
|
|
document.getElementById('edit').innerHTML = "\
|
|
<p>Section Number: "+params.sequenceNumber + "\
|
|
<input type='hidden' id='Survey_sectionId' value='"+params.Survey_sectionId+"'>\
|
|
<p>Section Name: <input id='sectionName' value='"+params.sectionName + "' type=text>\
|
|
<hr>\
|
|
<p>Randomize Questions: <span id='randomQuestions'></span>\
|
|
<p>Question per Page: <span id='questionsPerPage'></span>\
|
|
<p>Questions on Section Page: <span id='questionsOnSectionPage'></span>\
|
|
<hr>\
|
|
<p>Section Text:</p> <textarea id=text maxlength=2056 cols=30 rows=5>"+ params.sectionText +"</textarea>\
|
|
<p><input type='button' onclick='Survey.SectionTemplate.returnData()' value='Save Section'>\
|
|
";
|
|
|
|
this.randomizeQuestions = new YAHOO.widget.ButtonGroup({
|
|
id: "randomizeQuestions",
|
|
name: "randomizeQuestions",
|
|
container: "randomQuestions" });
|
|
|
|
this.randomizeQuestions.addButtons([
|
|
{ label: "Yes", value: "1" },
|
|
{ label: "No", value: "0" }
|
|
]);
|
|
|
|
if(params['randomizeQuestions'] == 1){this.randomizeQuestions.check(0);}
|
|
else{this.randomizeQuestions.check(1);}
|
|
|
|
var qppList = [];
|
|
|
|
for(var i = 1; i <= 10; i++){
|
|
qppList.push({ text: i.toString(), onclick: { fn: Survey.SectionTemplate.qppClick } });
|
|
}
|
|
|
|
this.qpp = new YAHOO.widget.Button({
|
|
type: "menu",
|
|
label: "1",
|
|
name: "mymenubutton",
|
|
menu: qppList,
|
|
container: "questionsPerPage" });
|
|
this.qpp.set("label", params['questionsPerPage']);
|
|
|
|
|
|
this.questionsOnSectionPage = new YAHOO.widget.ButtonGroup({
|
|
id: "questionsOnSectionPage",
|
|
name: "questionsOnSectionpage",
|
|
container: "questionsOnSectionPage" });
|
|
this.questionsOnSectionPage.addButtons([
|
|
{ label: "Yes", value: "1" },
|
|
{ label: "No", value: "0" }
|
|
]);
|
|
if(params['questionsOnSectionPage'] == 1){this.questionsOnSectionPage.check(0);}
|
|
else{this.questionsOnSectionPage.check(1);}
|
|
}
|
|
}();
|
|
*/
|
|
/*Survey.QuestionTemplate = new function(){
|
|
this.loadQuestion = function(params){
|
|
for(var i in params){
|
|
console.log(i+' '+params[i]);
|
|
}
|
|
document.getElementById('edit').innerHTML = "\
|
|
<p>Question Number: "+params.sequenceNumber + "\
|
|
<input type='hidden' id='Survey_sectionId' value='"+params.Survey_sectionId+"'>\
|
|
<input type='hidden' id='Survey_questionId' value='"+params.Survey_questionId+"'>\
|
|
|
|
<p>Randomize Questions: <span id='randomAnswers'></span>\
|
|
<p>Question per Page: <span id='questionsPerPage'></span>\
|
|
<p>Questions on Section Page: <span id='questionsOnSectionPage'></span>\
|
|
<hr>\
|
|
<p>Section Text:</p> <textarea id=text maxlength=2056 cols=30 rows=5>"+ params.sectionText +"</textarea>\
|
|
<p><input type='button' onclick='Survey.SectionTemplate.returnData()' value='Save Section'>\
|
|
";
|
|
}
|
|
}();
|
|
*/
|
|
Survey.AnswerTemplate = new function(){
|
|
this.loadAnswer = function(){
|
|
}
|
|
}();
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
Survey.Comm = new function(){
|
|
|
|
//p is a string that contains the id of the Survey that is of interest. To get Section 2 Question 3 Answer 4 pass '2-3-4'.
|
|
var request = function(sUrl,callback,postData){
|
|
YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
|
|
}
|
|
var callback = {
|
|
success:function(o){
|
|
Survey.Data.loadData(YAHOO.lang.JSON.parse(o.responseText));
|
|
},
|
|
failure: function(o){
|
|
alert("Last request failed");
|
|
Survey.Data.loadLast();
|
|
},
|
|
argument:["what","is","this","for"],
|
|
timeout: 1000
|
|
};
|
|
this.loadSurvey = function(p){
|
|
var postData = "data="+p;
|
|
var sUrl = "?func=loadSurvey";
|
|
request(sUrl,callback,postData);
|
|
}
|
|
this.dragDrop = function(target,before){
|
|
var p = {};
|
|
p['target'] = target;
|
|
p['before'] = before;
|
|
var postData = "data="+YAHOO.lang.JSON.stringify(p);
|
|
var sUrl = "?func=dragDrop";
|
|
request(sUrl,callback,postData);
|
|
}
|
|
this.submitEdit = function(p){
|
|
var postData = "data="+YAHOO.lang.JSON.stringify(p);
|
|
var sUrl = "?func=submitEdit";
|
|
request(sUrl,callback,postData);
|
|
}
|
|
this.newSection = function(){
|
|
var sUrl = "?func=newSection";
|
|
request(sUrl,callback);
|
|
}
|
|
this.newQuestion = function(sid){
|
|
var postData = "data="+sid;
|
|
var sUrl = "?func=newQuestion";
|
|
request(sUrl,callback,postData);
|
|
}
|
|
}();
|
|
|
|
//----------------------------------------------------------------
|
|
//
|
|
// Initialize survey
|
|
//
|
|
//----------------------------------------------------------------
|
|
Survey.OnLoad = function() {
|
|
var e = YAHOO.util.Event;
|
|
return {
|
|
init: function() {
|
|
//e.onDOMReady(this.initHandler, "The onDOMReady event fired. The DOM is now safe to modify via script.");
|
|
e.onDOMReady(this.initHandler);
|
|
},
|
|
initHandler: function(){
|
|
new YAHOO.util.DDTarget("sections","sections");
|
|
Survey.Comm.loadSurvey();
|
|
},
|
|
}
|
|
}();
|
|
|
|
Survey.OnLoad.init();
|