2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const noop = function () {};
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2020-07-21 05:46:22 +02:00
|
|
|
class Event {
|
|
|
|
constructor(type, props) {
|
|
|
|
this.type = type;
|
|
|
|
Object.assign(this, props);
|
|
|
|
}
|
|
|
|
preventDefault() {}
|
|
|
|
stopPropagation() {}
|
|
|
|
}
|
|
|
|
|
2018-04-16 21:45:29 +02:00
|
|
|
exports.make_event_store = (selector) => {
|
|
|
|
/*
|
|
|
|
|
|
|
|
This function returns an event_store object that
|
|
|
|
simulates the behavior of .on and .off from jQuery.
|
|
|
|
|
|
|
|
It also has methods to retrieve handlers that have
|
|
|
|
been set via .on (or similar methods), which can
|
|
|
|
be useful for tests that want to test the actual
|
|
|
|
handlers.
|
|
|
|
|
|
|
|
*/
|
2019-12-29 13:23:46 +01:00
|
|
|
const on_functions = new Map();
|
|
|
|
const child_on_functions = new Map();
|
2020-07-21 06:16:05 +02:00
|
|
|
let focused = false;
|
2017-05-25 01:49:29 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const self = {
|
2020-07-20 22:18:43 +02:00
|
|
|
get_on_handler(name, child_selector) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let handler;
|
2018-04-16 21:45:29 +02:00
|
|
|
|
|
|
|
if (child_selector === undefined) {
|
2019-04-18 19:28:25 +02:00
|
|
|
handler = on_functions.get(name);
|
|
|
|
if (!handler) {
|
2020-07-15 01:29:15 +02:00
|
|
|
throw Error("no " + name + " handler for " + selector);
|
2019-04-18 19:28:25 +02:00
|
|
|
}
|
|
|
|
return handler;
|
2018-04-16 21:45:29 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const child_on = child_on_functions.get(child_selector);
|
2019-04-18 19:28:25 +02:00
|
|
|
if (child_on) {
|
|
|
|
handler = child_on.get(name);
|
2018-04-16 21:45:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 19:28:25 +02:00
|
|
|
if (!handler) {
|
2020-07-15 01:29:15 +02:00
|
|
|
throw Error("no " + name + " handler for " + selector + " " + child_selector);
|
2019-04-18 19:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return handler;
|
2018-04-16 21:45:29 +02:00
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
off(event_name, ...args) {
|
2020-02-12 01:35:16 +01:00
|
|
|
if (args.length === 0) {
|
2019-12-29 13:23:46 +01:00
|
|
|
on_functions.delete(event_name);
|
2019-04-18 19:28:25 +02:00
|
|
|
return;
|
2018-04-16 21:45:29 +02:00
|
|
|
}
|
2019-04-18 19:28:25 +02:00
|
|
|
|
|
|
|
// In the Zulip codebase we never use this form of
|
|
|
|
// .off in code that we test: $(...).off('click', child_sel);
|
|
|
|
//
|
|
|
|
// So we don't support this for now.
|
2020-07-15 01:29:15 +02:00
|
|
|
throw Error("zjquery does not support this call sequence");
|
2018-04-16 21:45:29 +02:00
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
on(event_name, ...args) {
|
2018-04-16 21:45:29 +02:00
|
|
|
// parameters will either be
|
|
|
|
// (event_name, handler) or
|
|
|
|
// (event_name, sel, handler)
|
2020-02-12 01:35:16 +01:00
|
|
|
if (args.length === 1) {
|
|
|
|
const [handler] = args;
|
2019-04-18 19:28:25 +02:00
|
|
|
if (on_functions.has(event_name)) {
|
2020-07-15 01:29:15 +02:00
|
|
|
console.info("\nEither the app or the test can be at fault here..");
|
|
|
|
console.info("(sometimes you just want to call $.clear_all_elements();)\n");
|
|
|
|
throw Error("dup " + event_name + " handler for " + selector);
|
2019-04-18 19:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
on_functions.set(event_name, handler);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
if (args.length !== 2) {
|
2020-07-15 01:29:15 +02:00
|
|
|
throw Error("wrong number of arguments passed in");
|
2019-04-18 19:28:25 +02:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
const [sel, handler] = args;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(typeof sel, "string", "String selectors expected here.");
|
|
|
|
assert.equal(typeof handler, "function", "An handler function expected here.");
|
2019-12-29 13:23:46 +01:00
|
|
|
|
|
|
|
if (!child_on_functions.has(sel)) {
|
|
|
|
child_on_functions.set(sel, new Map());
|
|
|
|
}
|
|
|
|
|
|
|
|
const child_on = child_on_functions.get(sel);
|
2019-04-18 19:28:25 +02:00
|
|
|
|
|
|
|
if (child_on.has(event_name)) {
|
2020-07-15 01:29:15 +02:00
|
|
|
throw Error("dup " + event_name + " handler for " + selector + " " + sel);
|
2018-04-16 21:45:29 +02:00
|
|
|
}
|
2019-04-18 19:28:25 +02:00
|
|
|
|
|
|
|
child_on.set(event_name, handler);
|
2018-04-16 21:45:29 +02:00
|
|
|
},
|
|
|
|
|
2020-07-21 06:16:05 +02:00
|
|
|
one(event_name, handler) {
|
|
|
|
self.on(event_name, function (ev) {
|
|
|
|
self.off(event_name);
|
|
|
|
return handler.call(this, ev);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
trigger($element, ev, data) {
|
2020-07-21 05:46:22 +02:00
|
|
|
if (typeof ev === "string") {
|
|
|
|
ev = new Event(ev, data);
|
|
|
|
}
|
|
|
|
const func = on_functions.get(ev.type);
|
2019-04-18 19:28:25 +02:00
|
|
|
|
2020-07-21 06:16:05 +02:00
|
|
|
if (func) {
|
2019-04-18 19:28:25 +02:00
|
|
|
// It's possible that test code will trigger events
|
|
|
|
// that haven't been set up yet, but we are trying to
|
|
|
|
// eventually deprecate trigger in our codebase, so for
|
|
|
|
// now we just let calls to trigger silently do nothing.
|
|
|
|
// (And I think actual jQuery would do the same thing.)
|
2020-07-21 06:16:05 +02:00
|
|
|
func.call($element, ev, data);
|
2019-04-18 19:28:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-21 06:16:05 +02:00
|
|
|
if (ev.type === "focus" || ev.type === "focusin") {
|
|
|
|
focused = true;
|
|
|
|
} else if (ev.type === "blur" || ev.type === "focusout") {
|
|
|
|
focused = false;
|
|
|
|
}
|
2018-04-16 21:45:29 +02:00
|
|
|
},
|
2020-07-21 08:14:03 +02:00
|
|
|
|
2020-07-21 06:16:05 +02:00
|
|
|
is_focused() {
|
|
|
|
return focused;
|
|
|
|
},
|
2018-04-16 21:45:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.make_new_elem = function (selector, opts) {
|
2020-07-15 01:29:15 +02:00
|
|
|
let html = "never-been-set";
|
|
|
|
let text = "never-been-set";
|
2019-11-02 00:06:25 +01:00
|
|
|
let value;
|
|
|
|
let css;
|
|
|
|
let shown = false;
|
2019-12-29 13:23:46 +01:00
|
|
|
const find_results = new Map();
|
2019-11-02 00:06:25 +01:00
|
|
|
let my_parent;
|
2019-12-29 13:23:46 +01:00
|
|
|
const parents_result = new Map();
|
|
|
|
const properties = new Map();
|
|
|
|
const attrs = new Map();
|
|
|
|
const classes = new Map();
|
2019-11-02 00:06:25 +01:00
|
|
|
const event_store = exports.make_event_store(selector);
|
2018-04-16 21:45:29 +02:00
|
|
|
|
2019-10-26 00:26:37 +02:00
|
|
|
const self = {
|
2020-07-20 22:18:43 +02:00
|
|
|
addClass(class_name) {
|
2018-04-16 20:43:55 +02:00
|
|
|
classes.set(class_name, true);
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
append(arg) {
|
2020-04-04 22:14:34 +02:00
|
|
|
html = html + arg;
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
attr(name, val) {
|
2018-04-16 20:43:55 +02:00
|
|
|
if (val === undefined) {
|
|
|
|
return attrs.get(name);
|
|
|
|
}
|
|
|
|
attrs.set(name, val);
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
data(name, val) {
|
2020-01-03 17:10:59 +01:00
|
|
|
if (val === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const data_val = attrs.get("data-" + name);
|
2020-01-03 17:10:59 +01:00
|
|
|
if (data_val === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-16 08:16:35 +02:00
|
|
|
return data_val;
|
2020-01-03 17:10:59 +01:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
attrs.set("data-" + name, val);
|
2020-01-03 17:10:59 +01:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
delay() {
|
2018-08-22 00:31:48 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
debug() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
value,
|
|
|
|
shown,
|
|
|
|
selector,
|
2018-04-16 20:43:55 +02:00
|
|
|
};
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
empty(arg) {
|
2018-07-03 11:03:24 +02:00
|
|
|
if (arg === undefined) {
|
|
|
|
find_results.clear();
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
eq() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
expectOne() {
|
2018-04-16 20:43:55 +02:00
|
|
|
// silently do nothing
|
|
|
|
return self;
|
|
|
|
},
|
|
|
|
fadeTo: noop,
|
2020-07-20 22:18:43 +02:00
|
|
|
find(child_selector) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const child = find_results.get(child_selector);
|
2018-04-16 20:43:55 +02:00
|
|
|
if (child) {
|
|
|
|
return child;
|
|
|
|
}
|
2020-05-21 05:19:57 +02:00
|
|
|
if (child === false) {
|
|
|
|
// This is deliberately set to simulate missing find results.
|
|
|
|
// Return an empty array, the most common check is
|
|
|
|
// if ($.find().length) { //success }
|
|
|
|
return [];
|
|
|
|
}
|
2018-04-16 20:43:55 +02:00
|
|
|
if (opts.silent) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
throw Error("Cannot find " + child_selector + " in " + selector);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
get(idx) {
|
2018-04-16 20:43:55 +02:00
|
|
|
// We have some legacy code that does $('foo').get(0).
|
|
|
|
assert.equal(idx, 0);
|
|
|
|
return selector;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
get_on_handler(name, child_selector) {
|
2018-04-16 21:45:29 +02:00
|
|
|
return event_store.get_on_handler(name, child_selector);
|
2018-04-16 20:43:55 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
hasClass(class_name) {
|
2018-04-16 20:43:55 +02:00
|
|
|
return classes.has(class_name);
|
|
|
|
},
|
|
|
|
height: noop,
|
2020-07-20 22:18:43 +02:00
|
|
|
hide() {
|
2018-04-16 20:43:55 +02:00
|
|
|
shown = false;
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
html(arg) {
|
2018-04-16 20:43:55 +02:00
|
|
|
if (arg !== undefined) {
|
|
|
|
html = arg;
|
2017-07-08 17:41:53 +02:00
|
|
|
return self;
|
2018-04-16 20:43:55 +02:00
|
|
|
}
|
|
|
|
return html;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
is(arg) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (arg === ":visible") {
|
2018-04-16 20:43:55 +02:00
|
|
|
return shown;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (arg === ":focus") {
|
2020-07-21 06:16:05 +02:00
|
|
|
return self.is_focused();
|
2018-04-21 14:59:03 +02:00
|
|
|
}
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
is_focused() {
|
2018-04-16 20:43:55 +02:00
|
|
|
// is_focused is not a jQuery thing; this is
|
|
|
|
// for our testing
|
2020-07-21 06:16:05 +02:00
|
|
|
return event_store.is_focused();
|
2018-04-16 20:43:55 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
off(...args) {
|
2020-02-12 01:35:16 +01:00
|
|
|
event_store.off(...args);
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
on(...args) {
|
2020-02-12 01:35:16 +01:00
|
|
|
event_store.on(...args);
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
one(...args) {
|
2020-07-21 06:16:05 +02:00
|
|
|
event_store.one(...args);
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
parent() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return my_parent;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
parents(parents_selector) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const result = parents_result.get(parents_selector);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert(
|
|
|
|
result,
|
|
|
|
"You need to call set_parents_result for " + parents_selector + " in " + selector,
|
|
|
|
);
|
2018-04-16 20:43:55 +02:00
|
|
|
return result;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
prepend(arg) {
|
2020-04-04 22:14:34 +02:00
|
|
|
html = arg + html;
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
prop(name, val) {
|
2018-04-16 20:43:55 +02:00
|
|
|
if (val === undefined) {
|
|
|
|
return properties.get(name);
|
|
|
|
}
|
|
|
|
properties.set(name, val);
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
removeAttr(name) {
|
2019-12-29 13:23:46 +01:00
|
|
|
attrs.delete(name);
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
removeClass(class_names) {
|
2020-07-15 01:29:15 +02:00
|
|
|
class_names = class_names.split(" ");
|
2020-07-02 01:45:54 +02:00
|
|
|
class_names.forEach((class_name) => {
|
2019-12-29 13:23:46 +01:00
|
|
|
classes.delete(class_name);
|
2018-04-18 08:53:14 +02:00
|
|
|
});
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
remove() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
|
|
|
removeData: noop,
|
2020-07-20 22:18:43 +02:00
|
|
|
replaceWith() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
scrollTop() {
|
2019-02-27 12:52:05 +01:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
set_find_results(find_selector, jquery_object) {
|
2018-04-16 20:43:55 +02:00
|
|
|
find_results.set(find_selector, jquery_object);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
show() {
|
2018-04-16 20:43:55 +02:00
|
|
|
shown = true;
|
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
serializeArray() {
|
2019-02-27 12:52:05 +01:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
set_parent(parent_elem) {
|
2018-04-16 20:43:55 +02:00
|
|
|
my_parent = parent_elem;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
set_parents_result(selector, result) {
|
2018-04-16 20:43:55 +02:00
|
|
|
parents_result.set(selector, result);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
stop() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
text(...args) {
|
2020-02-12 11:27:02 +01:00
|
|
|
if (args.length !== 0) {
|
|
|
|
if (args[0] !== undefined) {
|
|
|
|
text = args[0].toString();
|
|
|
|
}
|
2018-03-25 15:28:49 +02:00
|
|
|
return self;
|
2018-04-16 20:43:55 +02:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
trigger(ev) {
|
2020-07-21 07:32:54 +02:00
|
|
|
event_store.trigger(self, ev);
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
val(...args) {
|
2020-02-12 01:35:16 +01:00
|
|
|
if (args.length === 0) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return value || "";
|
2018-04-16 20:43:55 +02:00
|
|
|
}
|
2020-02-12 01:35:16 +01:00
|
|
|
[value] = args;
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
css(...args) {
|
2020-02-12 01:35:16 +01:00
|
|
|
if (args.length === 0) {
|
2018-07-23 00:25:21 +02:00
|
|
|
return css || {};
|
|
|
|
}
|
2020-02-12 01:35:16 +01:00
|
|
|
[css] = args;
|
2018-07-23 00:25:21 +02:00
|
|
|
return self;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
visible() {
|
2018-04-16 20:43:55 +02:00
|
|
|
return shown;
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
slice() {
|
2020-05-29 12:03:22 +02:00
|
|
|
return self;
|
|
|
|
},
|
2018-04-16 20:43:55 +02:00
|
|
|
};
|
2017-07-06 00:18:15 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (selector[0] === "<") {
|
2018-04-16 20:43:55 +02:00
|
|
|
self.html(selector);
|
|
|
|
}
|
2017-07-06 15:47:25 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
self[0] = "you-must-set-the-child-yourself";
|
2017-06-14 14:17:03 +02:00
|
|
|
|
2018-04-16 20:43:55 +02:00
|
|
|
self.selector = selector;
|
|
|
|
|
2019-05-22 01:54:53 +02:00
|
|
|
self.length = 1;
|
|
|
|
|
2018-04-16 20:43:55 +02:00
|
|
|
return self;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.make_zjquery = function (opts) {
|
|
|
|
opts = opts || {};
|
2017-06-14 14:17:03 +02:00
|
|
|
|
2020-02-12 08:35:40 +01:00
|
|
|
const elems = new Map();
|
2018-04-16 20:43:55 +02:00
|
|
|
|
|
|
|
// Our fn structure helps us simulate extending jQuery.
|
2019-11-02 00:06:25 +01:00
|
|
|
const fn = {};
|
2017-05-25 01:49:29 +02:00
|
|
|
|
2018-04-16 20:43:55 +02:00
|
|
|
function new_elem(selector) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem = exports.make_new_elem(selector, {
|
2018-04-16 20:43:55 +02:00
|
|
|
silent: opts.silent,
|
|
|
|
});
|
2020-02-06 03:52:12 +01:00
|
|
|
Object.assign(elem, fn);
|
2019-04-18 21:11:30 +02:00
|
|
|
|
|
|
|
// Create a proxy handler to detect missing stubs.
|
|
|
|
//
|
|
|
|
// For context, zjquery doesn't implement every method/attribute
|
|
|
|
// that you'd find on a "real" jQuery object. Sometimes we
|
|
|
|
// expects devs to create their own stubs.
|
2019-11-02 00:06:25 +01:00
|
|
|
const handler = {
|
2019-04-18 21:11:30 +02:00
|
|
|
get: (target, key) => {
|
|
|
|
// Handle the special case of equality checks, which
|
|
|
|
// we can infer by assert.equal trying to access the
|
|
|
|
// "stack" key.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (key === "stack") {
|
2020-07-15 00:34:28 +02:00
|
|
|
const error =
|
|
|
|
"\nInstead of doing equality checks on a full object, " +
|
2019-04-18 21:11:30 +02:00
|
|
|
'do `assert_equal(foo.selector, ".some_class")\n';
|
|
|
|
throw Error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
const val = target[key];
|
|
|
|
|
|
|
|
if (val === undefined) {
|
|
|
|
// For undefined values, we'll throw errors to devs saying
|
|
|
|
// they need to create stubs. We ignore certain keys that
|
|
|
|
// are used for simply printing out the object.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (typeof key === "symbol") {
|
2019-04-18 21:11:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (key === "inspect") {
|
2019-04-18 21:11:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Error('You must create a stub for $("' + selector + '").' + key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const proxy = new Proxy(elem, handler);
|
2019-04-18 21:11:30 +02:00
|
|
|
|
|
|
|
return proxy;
|
2017-05-25 01:49:29 +02:00
|
|
|
}
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const zjquery = function (arg, arg2) {
|
2017-05-25 01:49:29 +02:00
|
|
|
if (typeof arg === "function") {
|
|
|
|
// If somebody is passing us a function, we emulate
|
|
|
|
// jQuery's behavior of running this function after
|
|
|
|
// page load time. But there are no pages to load,
|
|
|
|
// so we just call it right away.
|
|
|
|
arg();
|
|
|
|
return;
|
2018-04-12 18:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If somebody is passing us an element, we return
|
|
|
|
// the element itself if it's been created with
|
|
|
|
// zjquery.
|
|
|
|
// This may happen in cases like $(this).
|
2020-02-12 08:35:40 +01:00
|
|
|
if (arg.selector && elems.has(arg.selector)) {
|
|
|
|
return arg;
|
2017-05-25 01:49:29 +02:00
|
|
|
}
|
2017-05-24 03:34:18 +02:00
|
|
|
|
2018-04-12 18:06:39 +02:00
|
|
|
// We occasionally create stub objects that know
|
|
|
|
// they want to be wrapped by jQuery (so they can
|
|
|
|
// in turn return stubs). The convention is that
|
|
|
|
// they provide a to_$ attribute.
|
|
|
|
if (arg.to_$) {
|
|
|
|
assert(typeof arg.to_$ === "function");
|
|
|
|
return arg.to_$();
|
|
|
|
}
|
|
|
|
|
2018-04-05 16:46:45 +02:00
|
|
|
if (arg2 !== undefined) {
|
|
|
|
throw Error("We only use one-argument variations of $(...) in Zulip code.");
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const selector = arg;
|
2017-07-08 15:16:19 +02:00
|
|
|
|
2018-04-12 18:06:39 +02:00
|
|
|
if (typeof selector !== "string") {
|
|
|
|
console.info(arg);
|
|
|
|
throw Error("zjquery does not know how to wrap this object yet");
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const valid_selector =
|
2020-07-15 01:29:15 +02:00
|
|
|
"<#.".includes(selector[0]) ||
|
|
|
|
selector === "window-stub" ||
|
|
|
|
selector === "document-stub" ||
|
|
|
|
selector === "body" ||
|
|
|
|
selector === "html" ||
|
2018-06-06 18:19:09 +02:00
|
|
|
selector.location ||
|
2020-07-15 01:29:15 +02:00
|
|
|
selector.includes("#") ||
|
|
|
|
selector.includes(".") ||
|
2020-07-15 00:34:28 +02:00
|
|
|
(selector.includes("[") && selector.indexOf("]") >= selector.indexOf("["));
|
2017-07-08 15:16:19 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert(valid_selector, "Invalid selector: " + selector + " Use $.create() maybe?");
|
2017-07-08 15:16:19 +02:00
|
|
|
|
2020-02-12 08:35:40 +01:00
|
|
|
if (!elems.has(selector)) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem = new_elem(selector);
|
2020-02-12 08:35:40 +01:00
|
|
|
elems.set(selector, elem);
|
2017-05-24 03:34:18 +02:00
|
|
|
}
|
2020-02-12 08:35:40 +01:00
|
|
|
return elems.get(selector);
|
2017-05-25 01:49:29 +02:00
|
|
|
};
|
2017-05-24 03:34:18 +02:00
|
|
|
|
2020-07-16 23:29:01 +02:00
|
|
|
zjquery.create = function (name) {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert(!elems.has(name), "You already created an object with this name!!");
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem = new_elem(name);
|
2020-02-12 08:35:40 +01:00
|
|
|
elems.set(name, elem);
|
|
|
|
return elem;
|
2017-07-08 15:16:19 +02:00
|
|
|
};
|
2017-05-24 03:05:29 +02:00
|
|
|
|
2017-05-25 01:49:29 +02:00
|
|
|
zjquery.stub_selector = function (selector, stub) {
|
2020-02-12 08:35:40 +01:00
|
|
|
elems.set(selector, stub);
|
2017-05-25 01:49:29 +02:00
|
|
|
};
|
2017-05-24 22:31:12 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
zjquery.trim = function (s) {
|
|
|
|
return s;
|
|
|
|
};
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2017-05-25 01:49:29 +02:00
|
|
|
zjquery.state = function () {
|
|
|
|
// useful for debugging
|
2020-07-02 01:39:34 +02:00
|
|
|
let res = Array.from(elems.values(), (v) => v.debug());
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
res = res.map((v) => [v.selector, v.value, v.shown]);
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2017-05-25 01:49:29 +02:00
|
|
|
res.sort();
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2017-05-25 01:49:29 +02:00
|
|
|
return res;
|
|
|
|
};
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2020-07-21 05:46:22 +02:00
|
|
|
zjquery.Event = (type, props) => new Event(type, props);
|
2017-05-23 03:20:15 +02:00
|
|
|
|
2020-05-22 08:16:08 +02:00
|
|
|
fn.after = function (s) {
|
|
|
|
return s;
|
|
|
|
};
|
|
|
|
fn.before = function (s) {
|
|
|
|
return s;
|
|
|
|
};
|
|
|
|
|
2018-04-12 14:45:06 +02:00
|
|
|
zjquery.fn = fn;
|
|
|
|
|
2018-07-05 14:58:14 +02:00
|
|
|
zjquery.clear_all_elements = function () {
|
2020-02-12 08:35:40 +01:00
|
|
|
elems.clear();
|
2018-07-05 14:58:14 +02:00
|
|
|
};
|
2020-05-22 08:16:08 +02:00
|
|
|
zjquery.escapeSelector = function (s) {
|
|
|
|
return s;
|
|
|
|
};
|
2018-07-05 14:58:14 +02:00
|
|
|
|
2017-05-25 01:49:29 +02:00
|
|
|
return zjquery;
|
|
|
|
};
|