sendInsertUI now uses sendMessage instead of sendCustomMessage

This commit is contained in:
Barbara Borges Ribeiro
2016-04-29 04:46:09 +01:00
parent 8c12e3ab90
commit 9e91b265ce
8 changed files with 162 additions and 131 deletions

View File

@@ -7,62 +7,3 @@ $(document).on('keydown', function(e) {
e.preventDefault();
});
// Turns out that Firefox does not support insertAdjacentElement().
// So we have to implement our own version for insertUI.
// Code adapted from here: http://forums.mozillazine.org/viewtopic.php?t=445587
HTMLElement.prototype.insertAdjacentElement = function(where, parsedNode) {
switch (where) {
case 'beforeBegin':
this.parentNode.insertBefore(parsedNode, this);
break;
case 'afterBegin':
this.insertBefore(parsedNode, this.firstChild);
break;
case 'beforeEnd':
this.appendChild(parsedNode);
break;
case 'afterEnd':
if (this.nextSibling) {
this.parentNode.insertBefore(parsedNode, this.nextSibling);
}
else {
this.parentNode.appendChild(parsedNode);
}
break;
}
};
exports.addCustomMessageHandler("shiny-insert-ui",
function(message) {
let targets = $(message.selector);
if (targets.length === 0) {
// render the HTML and deps to a null target, so
// the side-effect of rendering the deps, singletons,
// and <head> still occur
exports.renderHtml(
$([]),
message.content.html,
message.content.deps
);
} else {
targets.each((i, target) => {
let container = document.createElement(message.container);
target.insertAdjacentElement(message.where, container);
exports.renderContent(container, message.content);
$(container).trigger("shown");
return message.multiple;
});
}
}
);
exports.addCustomMessageHandler("shiny-remove-ui",
function(message) {
let els = $(message.selector);
els.each((i, el) => {
$(el).remove();
return message.multiple;
});
}
);

View File

@@ -631,6 +631,67 @@ var ShinyApp = function() {
window.location.reload();
});
// Helper function for addMessageHandler('shiny-insert-ui').
// Turns out that Firefox does not support insertAdjacentElement().
// So we have to implement our own version for insertUI.
// Code adapted from here: http://forums.mozillazine.org/viewtopic.php?t=445587
HTMLElement.prototype.insertAdjacentElement = function(where, parsedNode) {
switch (where) {
case 'beforeBegin':
this.parentNode.insertBefore(parsedNode, this);
break;
case 'afterBegin':
this.insertBefore(parsedNode, this.firstChild);
break;
case 'beforeEnd':
this.appendChild(parsedNode);
break;
case 'afterEnd':
if (this.nextSibling) {
this.parentNode.insertBefore(parsedNode, this.nextSibling);
}
else {
this.parentNode.appendChild(parsedNode);
}
break;
}
};
addMessageHandler('shiny-insert-ui', function(message) {
let targets = $(message.selector);
if (targets.length === 0) {
// render the HTML and deps to a null target, so
// the side-effect of rendering the deps, singletons,
// and <head> still occur
exports.renderHtml(
$([]),
message.content.html,
message.content.deps
);
} else {
targets.each((i, target) => {
let container = document.createElement(message.container);
target.insertAdjacentElement(message.where, container);
exports.renderContent(container, message.content);
$(container).trigger('shown');
return message.multiple;
});
}
});
addMessageHandler('shiny-remove-ui',
function(message) {
let els = $(message.selector);
els.each((i, el) => {
$(el).remove();
// If `multiple` is false, returning false terminates the function
// and no other elements are removed; if `multiple` is true,
// returning true continues removing all remaining elements.
return message.multiple;
});
}
);
// Progress reporting ====================================================