Fixed a few js memory leaks in EditSurvey page (SimpleEditor appears to

have a memory leak of its own which we either need to get fixed or
work-around).
This commit is contained in:
Patrick Donelan 2009-01-15 09:12:06 +00:00
parent a02cbf95a7
commit d5f6d8603e
2 changed files with 71 additions and 31 deletions

View file

@ -8,6 +8,16 @@ 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;
function purgeNode(node) {
YAHOO.util.Event.purgeElement(node, true);
document.getElementById(node).innerHTML = '';
}
return {
dragDrop: function(did){
@ -57,6 +67,11 @@ Survey.Data = (function(){
else {
lastId = d.address;
}
// First purge any event handlers bound to sections node..
YAHOO.util.Event.purgeElement('sections', true);
// Now we can re-write its innerHTML without fear of memory leaks
document.getElementById('sections').innerHTML = d.ddhtml;
//add event handlers for if a tag is clicked
@ -68,28 +83,25 @@ Survey.Data = (function(){
}
}
//add the add object buttons
// if(d.buttons['section']){
document.getElementById('addSection').innerHTML = '';
document.getElementById('addQuestion').innerHTML = '';
document.getElementById('addAnswer').innerHTML = '';
var sButton = new YAHOO.widget.Button({
sButton && sButton.destroy();
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({
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) {
var aButton = new YAHOO.widget.Button({
aButton && aButton.destroy();
aButton = new YAHOO.widget.Button({
label: "Add Answer",
id: "addanswer",
container: "addAnswer"
@ -102,12 +114,16 @@ Survey.Data = (function(){
// 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);
var ds = new YAHOO.util.LocalDataSource(d.gotoTargets);
autoComplete = new YAHOO.widget.AutoComplete('goto', 'goto-yui-ac-container', ds);
}
}
else {
document.getElementById('edit').innerHTML = "";
Survey.ObjectTemplate.unloadObject();
if (autoComplete) {
autoComplete.destroy();
autoComplete = null;
}
}
lastDataSet = d;
},

View file

@ -6,15 +6,32 @@ if (typeof Survey === "undefined") {
Survey.ObjectTemplate = (function(){
var editor;
// Keep references to widgets here so that we can destory any instances before
// creating new ones (to avoid memory leaks)
var dialog;
var editor;
return {
unloadObject: function(){
// First destory the editor..
if (editor) {
editor.destroy();
editor = null;
}
// And then the Dialog that contains it.
if (dialog) {
dialog.destroy();
dialog = null;
}
},
loadObject: function(html, type){
// Make sure we purge any event listeners before overwrite innerHTML..
YAHOO.util.Event.purgeElement('edit', true);
document.getElementById('edit').innerHTML = html;
var btns = [{
text: "Submit",
handler: function(){
@ -39,8 +56,23 @@ Survey.ObjectTemplate = (function(){
document.getElementById('delete').value = 1;
this.submit();
}
}, {
text: "Preview",
handler: function(){
if (type === 'answer') {
alert('Sorry, preview is only supported for Sections and Questions, not Answers');
}
else {
var msg = 'This will delete any Survey responses you have made under this ' +
'user account and redirect you to the Take Survey page starting at the selected item. ' +
"\n\nAre you sure you want to continue?";
if (confirm(msg)) {
window.location.search = 'func=jumpTo;id=' + dialog.getData().id;
}
}
}
}];
dialog = new YAHOO.widget.Dialog(type, {
width: "600px",
context: [document.body, 'tr', 'tr'],
@ -48,26 +80,19 @@ Survey.ObjectTemplate = (function(){
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';
}
// N.B. SimpleEditor has a memory leak so this eats memory on every instantiation
editor = new YAHOO.widget.SimpleEditor(textareaId, {
height: height,
width: '100%',
@ -83,4 +108,3 @@ Survey.ObjectTemplate = (function(){
}
};
})();