Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No public description #4034

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions google/colab/html/js/_html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

(function() {

let el = undefined;

function safeCopy(obj) {
const result = {};
for (const prop in obj) {
Expand All @@ -23,21 +21,21 @@ function safeCopy(obj) {

const callbacks = new Map();

function addPythonEventListener(type, callbackName) {
function addPythonEventListener(element, type, callbackName) {
const callback = (evt) => {
google.colab.kernel.invokeFunction(callbackName, [safeCopy(evt)], {});
};
callbacks.set(callbackName, callback);
el.addEventListener(type, callback);
element.addEventListener(type, callback);
}

function addJsEventListener(type, callbackSrc) {
function addJsEventListener(element, type, callbackSrc) {
const fn = new Function('event', callbackSrc);
const callback = (evt) => {
fn(evt);
};
callbacks.set(callbackSrc, callback);
el.addEventListener(type, callback);
element.addEventListener(type, callback);
}

function dotAccess(target, accessor) {
Expand All @@ -54,6 +52,9 @@ function dotAccess(target, accessor) {

async function initialize(config) {
el = document.getElementById(config.guid);
if (!el) {
throw new Error(`No element found with id: ${guid}`);
}
if (config.tag.includes('-')) {
await customElements.whenDefined(config.tag);
}
Expand All @@ -73,26 +74,26 @@ async function initialize(config) {
const jsl = config.js_listeners;
Object.keys(jsl).forEach((k) => {
jsl[k].forEach((cb) => {
addJsEventListener(k, cb);
addJsEventListener(el, k, cb);
});
});
}
if (config.py_listeners) {
const pyl = config.py_listeners;
Object.keys(pyl).forEach((k) => {
pyl[k].forEach((cb) => {
addPythonEventListener(k, cb);
addPythonEventListener(el, k, cb);
});
});
}
}


function processMessage(msg) {
function processMessage(element, msg) {
let obj;
let name;
if (['call', 'setProperty', 'getProperty'].includes(msg.method)) {
[obj, name] = dotAccess(el, msg.name);
[obj, name] = dotAccess(element, msg.name);
}
switch (msg.method) {
case 'setProperty':
Expand All @@ -101,22 +102,22 @@ function processMessage(msg) {
case 'getProperty':
return obj[name];
case 'setAttribute':
el.setAttribute(msg.name, msg.value);
element.setAttribute(msg.name, msg.value);
return true;
case 'getAttribute':
return el.getAttribute(msg.name);
return element.getAttribute(msg.name);
case 'call':
return obj[name](...msg.value);
case 'addPythonEventListener':
addPythonEventListener(msg.name, msg.value);
addPythonEventListener(element, msg.name, msg.value);
return true;
case 'addJsEventListener':
addJsEventListener(msg.name, msg.value);
addJsEventListener(element, msg.name, msg.value);
return true;
case 'removeEventListener':
if (callbacks.has(msg.value)) {
const cb = callbacks.get(msg.value);
el.removeEventListener(msg.name, cb);
element.removeEventListener(msg.name, cb);
callbacks.delete(msg.value);
return true;
} else {
Expand All @@ -133,12 +134,16 @@ function createElement(config) {
window.google.colab.output.pauseOutputUntil(initialize(config));
const guid = config.guid;
const tag = config.tag;
const el = document.getElementById(guid);
if (!el) {
throw new Error(`No element found with id: ${guid}`);
}
window.google.colab.html.elements[guid] = {
call: async (msg) => {
if (tag.includes('-')) {
await customElements.whenDefined(tag);
}
return processMessage(msg);
return processMessage(el, msg);
}
};
}
Expand Down
Loading