2020-07-19 17:05:06 +00:00
|
|
|
/* setup jsrenderer */
|
2020-07-23 18:00:25 +00:00
|
|
|
import "jsrender";
|
2020-11-29 13:42:02 +00:00
|
|
|
import { tr } from "./i18n/localize";
|
2020-11-07 12:16:07 +00:00
|
|
|
|
2020-07-23 18:00:25 +00:00
|
|
|
if(__build.target === "web") {
|
|
|
|
(window as any).$ = require("jquery");
|
|
|
|
(window as any).jQuery = $;
|
2020-07-21 10:59:02 +00:00
|
|
|
|
2020-07-23 18:00:25 +00:00
|
|
|
require("jsrender")($);
|
|
|
|
}
|
2020-07-19 17:05:06 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
declare global {
|
2020-07-19 14:34:08 +00:00
|
|
|
function setInterval(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
|
|
|
|
function setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
|
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
interface Array<T> {
|
|
|
|
remove(elem?: T): boolean;
|
|
|
|
last?(): T;
|
2018-02-27 16:20:49 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
pop_front(): T | undefined;
|
2020-06-10 16:13:56 +00:00
|
|
|
toggle(entry: T) : boolean;
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
2018-02-27 16:20:49 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
interface JSON {
|
|
|
|
map_to<T>(object: T, json: any, variables?: string | string[], validator?: (map_field: string, map_value: string) => boolean, variable_direction?: number) : number;
|
|
|
|
map_field_to<T>(object: T, value: any, field: string) : boolean;
|
|
|
|
}
|
2018-08-10 19:30:58 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
type JQueryScrollType = "height" | "width";
|
|
|
|
interface JQuery<TElement = HTMLElement> {
|
|
|
|
renderTag(values?: any) : JQuery<TElement>;
|
|
|
|
hasScrollBar(direction?: JQueryScrollType) : boolean;
|
2019-02-17 15:08:10 +00:00
|
|
|
|
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
visible_height() : number;
|
|
|
|
visible_width() : number;
|
2019-02-17 15:08:10 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
/* first element which matches the selector, could be the element itself or a parent */
|
|
|
|
firstParent(selector: string) : JQuery;
|
|
|
|
}
|
2018-02-27 16:20:49 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
interface JQueryStatic<TElement extends Node = HTMLElement> {
|
|
|
|
spawn<K extends keyof HTMLElementTagNameMap>(tagName: K): JQuery<HTMLElementTagNameMap[K]>;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Window {
|
2020-07-23 22:29:36 +00:00
|
|
|
__REACT_DEVTOOLS_GLOBAL_HOOK__: any;
|
|
|
|
|
2020-04-01 13:40:45 +00:00
|
|
|
readonly webkitAudioContext: typeof AudioContext;
|
|
|
|
readonly AudioContext: typeof OfflineAudioContext;
|
2020-03-30 11:44:18 +00:00
|
|
|
readonly OfflineAudioContext: typeof OfflineAudioContext;
|
2020-04-18 17:37:30 +00:00
|
|
|
readonly webkitOfflineAudioContext: typeof OfflineAudioContext;
|
2020-03-30 11:44:18 +00:00
|
|
|
readonly RTCPeerConnection: typeof RTCPeerConnection;
|
|
|
|
readonly Pointer_stringify: any;
|
|
|
|
|
2020-04-11 09:43:41 +00:00
|
|
|
readonly require: typeof require;
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
2020-04-11 09:43:41 +00:00
|
|
|
const __non_webpack_require__: typeof require;
|
2020-03-30 11:44:18 +00:00
|
|
|
|
|
|
|
interface Navigator {
|
|
|
|
browserSpecs: {
|
|
|
|
name: string,
|
|
|
|
version: string
|
|
|
|
};
|
|
|
|
|
|
|
|
mozGetUserMedia(constraints: MediaStreamConstraints, successCallback: NavigatorUserMediaSuccessCallback, errorCallback: NavigatorUserMediaErrorCallback): void;
|
|
|
|
webkitGetUserMedia(constraints: MediaStreamConstraints, successCallback: NavigatorUserMediaSuccessCallback, errorCallback: NavigatorUserMediaErrorCallback): void;
|
|
|
|
}
|
2020-11-07 12:16:07 +00:00
|
|
|
|
|
|
|
interface ObjectConstructor {
|
|
|
|
isSimilar(a: any, b: any): boolean;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Object.isSimilar) {
|
|
|
|
Object.isSimilar = function (a, b) {
|
|
|
|
const aType = typeof a;
|
|
|
|
const bType = typeof b;
|
|
|
|
if (aType !== bType) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aType === "object") {
|
|
|
|
const aKeys = Object.keys(a);
|
|
|
|
const bKeys = Object.keys(b);
|
|
|
|
if(aKeys.length != bKeys.length) { return false; }
|
|
|
|
if(aKeys.findIndex(key => bKeys.indexOf(key) !== -1) !== -1) { return false; }
|
|
|
|
return aKeys.findIndex(key => !Object.isSimilar(a[key], b[key])) === -1;
|
|
|
|
} else {
|
|
|
|
return a === b;
|
|
|
|
}
|
|
|
|
};
|
2018-02-27 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 19:30:58 +00:00
|
|
|
if(!JSON.map_to) {
|
2019-09-12 21:59:35 +00:00
|
|
|
JSON.map_to = function <T>(object: T, json: any, variables?: string | string[], validator?: (map_field: string, map_value: string) => boolean, variable_direction?: number): number {
|
2020-07-19 16:49:00 +00:00
|
|
|
if (!validator)
|
|
|
|
validator = () => true;
|
2018-08-10 19:30:58 +00:00
|
|
|
|
2018-09-30 19:50:59 +00:00
|
|
|
if (!variables) {
|
2018-08-10 19:30:58 +00:00
|
|
|
variables = [];
|
|
|
|
|
2018-09-30 19:50:59 +00:00
|
|
|
if (!variable_direction || variable_direction == 0) {
|
2020-07-19 16:49:00 +00:00
|
|
|
for (let field of Object.keys(json))
|
2018-08-10 19:30:58 +00:00
|
|
|
variables.push(field);
|
2018-09-30 19:50:59 +00:00
|
|
|
} else if (variable_direction == 1) {
|
2020-07-19 16:49:00 +00:00
|
|
|
for (let field of Object.keys(json))
|
2018-08-10 19:30:58 +00:00
|
|
|
variables.push(field);
|
|
|
|
}
|
2018-09-30 19:50:59 +00:00
|
|
|
} else if (!Array.isArray(variables)) {
|
2018-08-10 19:30:58 +00:00
|
|
|
variables = [variables];
|
|
|
|
}
|
|
|
|
|
2019-09-12 21:59:35 +00:00
|
|
|
let updates = 0;
|
2018-09-30 19:50:59 +00:00
|
|
|
for (let field of variables) {
|
2020-02-02 14:05:36 +00:00
|
|
|
if (typeof json[field] === "undefined") {
|
2018-12-05 19:46:33 +00:00
|
|
|
console.trace(tr("Json does not contains %s"), field);
|
2018-08-10 19:30:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-09-30 19:50:59 +00:00
|
|
|
if (!validator(field, json[field])) {
|
2018-12-05 19:46:33 +00:00
|
|
|
console.trace(tr("Validator results in false for %s"), field);
|
2018-08-10 19:30:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-12 21:59:35 +00:00
|
|
|
if(JSON.map_field_to(object, json[field], field))
|
|
|
|
updates++;
|
2018-08-10 19:30:58 +00:00
|
|
|
}
|
2019-09-12 21:59:35 +00:00
|
|
|
return updates;
|
2018-08-10 19:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!JSON.map_field_to) {
|
2019-09-12 21:59:35 +00:00
|
|
|
JSON.map_field_to = function<T>(object: T, value: any, field: string) : boolean {
|
2020-12-12 13:18:50 +00:00
|
|
|
let fieldType = typeof object[field];
|
|
|
|
let newValue;
|
|
|
|
if(fieldType == "string" || fieldType == "object" || fieldType == "undefined") {
|
|
|
|
newValue = value;
|
|
|
|
} else if(fieldType == "number") {
|
|
|
|
newValue = parseFloat(value);
|
|
|
|
} else if(fieldType == "boolean") {
|
|
|
|
newValue = typeof value === "boolean" && value || value === "1" || value === "true";
|
|
|
|
} else {
|
|
|
|
console.warn(tr("Invalid object type %s for entry %s"), fieldType, field);
|
2019-09-12 21:59:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:18:50 +00:00
|
|
|
if(newValue === object[field]) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 19:30:58 +00:00
|
|
|
|
2020-12-12 13:18:50 +00:00
|
|
|
object[field] = newValue;
|
2019-09-12 21:59:35 +00:00
|
|
|
return true;
|
2018-08-10 19:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 16:20:49 +00:00
|
|
|
if (!Array.prototype.remove) {
|
|
|
|
Array.prototype.remove = function<T>(elem?: T): boolean {
|
2018-03-24 22:38:01 +00:00
|
|
|
const index = this.indexOf(elem, 0);
|
2018-02-27 16:20:49 +00:00
|
|
|
if (index > -1) {
|
|
|
|
this.splice(index, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.prototype.pop_front) {
|
|
|
|
Array.prototype.pop_front = function<T>(): T {
|
|
|
|
if(this.length == 0) return undefined;
|
|
|
|
return this.splice(0, 1)[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 16:13:56 +00:00
|
|
|
if (!Array.prototype.toggle) {
|
|
|
|
Array.prototype.toggle = function<T>(element: T): boolean {
|
|
|
|
const index = this.findIndex(e => e === element);
|
|
|
|
if(index === -1) {
|
|
|
|
this.push(element);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
this.splice(index, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 18:38:35 +00:00
|
|
|
|
2018-02-27 16:20:49 +00:00
|
|
|
if (!Array.prototype.last){
|
|
|
|
Array.prototype.last = function(){
|
|
|
|
if(this.length == 0) return undefined;
|
|
|
|
return this[this.length - 1];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:56:09 +00:00
|
|
|
if(typeof ($) !== "undefined") {
|
|
|
|
if(!$.spawn) {
|
|
|
|
$.spawn = function<K extends keyof HTMLElementTagNameMap>(tagName: K): JQuery<HTMLElementTagNameMap[K]> {
|
2018-10-08 23:27:14 +00:00
|
|
|
return $(document.createElement(tagName) as any);
|
2018-04-11 15:56:09 +00:00
|
|
|
}
|
2018-02-27 16:20:49 +00:00
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
|
2018-12-23 16:41:14 +00:00
|
|
|
if(!$.fn.renderTag) {
|
2019-04-25 18:22:13 +00:00
|
|
|
$.fn.renderTag = function (this: JQuery, values?: any) : JQuery {
|
2018-10-03 20:04:29 +00:00
|
|
|
let result;
|
|
|
|
if(this.render) {
|
|
|
|
result = $(this.render(values));
|
|
|
|
} else {
|
2020-07-19 17:05:06 +00:00
|
|
|
const template = jsrender.render[this.attr("id")];
|
2019-04-25 18:22:13 +00:00
|
|
|
if(!template) {
|
|
|
|
console.error("Tried to render template %o, but template is not available!", this.attr("id"));
|
|
|
|
throw "missing template " + this.attr("id");
|
|
|
|
}
|
2018-10-14 11:27:48 +00:00
|
|
|
/*
|
|
|
|
result = window.jsrender.templates("tmpl_permission_entry", $("#tmpl_permission_entry").html());
|
|
|
|
result = window.jsrender.templates("xxx", this.html());
|
|
|
|
*/
|
|
|
|
result = template(values);
|
2018-10-03 20:04:29 +00:00
|
|
|
result = $(result);
|
|
|
|
}
|
2018-09-30 20:36:17 +00:00
|
|
|
result.find("node").each((index, element) => {
|
2018-11-03 23:39:29 +00:00
|
|
|
$(element).replaceWith(values[$(element).attr("key")] || (values[0] || [])[$(element).attr("key")]);
|
2018-09-30 20:36:17 +00:00
|
|
|
});
|
2018-09-30 19:50:59 +00:00
|
|
|
return result;
|
2018-06-20 17:06:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-23 16:41:14 +00:00
|
|
|
if(!$.fn.hasScrollBar)
|
2019-04-04 19:47:52 +00:00
|
|
|
$.fn.hasScrollBar = function(direction?: "height" | "width") {
|
|
|
|
if(this.length <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const scroll_height = this.get(0).scrollHeight > this.height();
|
|
|
|
const scroll_width = this.get(0).scrollWidth > this.width();
|
|
|
|
|
|
|
|
if(typeof(direction) === "string") {
|
|
|
|
if(direction === "height")
|
|
|
|
return scroll_height;
|
|
|
|
if(direction === "width")
|
|
|
|
return scroll_width;
|
|
|
|
}
|
|
|
|
return scroll_width || scroll_height;
|
|
|
|
};
|
2018-12-23 16:41:14 +00:00
|
|
|
|
2019-02-17 15:08:10 +00:00
|
|
|
if(!$.fn.visible_height)
|
|
|
|
$.fn.visible_height = function (this: JQuery<HTMLElement>) {
|
|
|
|
const original_style = this.attr("style");
|
|
|
|
this.css({
|
|
|
|
position: 'absolute!important',
|
|
|
|
visibility: 'hidden!important',
|
|
|
|
display: 'block!important'
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = this.height();
|
|
|
|
this.attr("style", original_style || "");
|
|
|
|
return result;
|
2020-03-30 11:44:18 +00:00
|
|
|
};
|
|
|
|
|
2019-02-17 15:08:10 +00:00
|
|
|
if(!$.fn.visible_width)
|
|
|
|
$.fn.visible_width = function (this: JQuery<HTMLElement>) {
|
|
|
|
const original_style = this.attr("style");
|
|
|
|
this.css({
|
|
|
|
position: 'absolute!important',
|
|
|
|
visibility: 'hidden!important',
|
|
|
|
display: 'block!important'
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = this.width();
|
|
|
|
this.attr("style", original_style || "");
|
|
|
|
return result;
|
2020-03-30 11:44:18 +00:00
|
|
|
};
|
|
|
|
|
2019-08-21 08:00:01 +00:00
|
|
|
if(!$.fn.firstParent)
|
|
|
|
$.fn.firstParent = function (this: JQuery<HTMLElement>, selector: string) {
|
|
|
|
if(this.is(selector))
|
|
|
|
return this;
|
|
|
|
return this.parent(selector);
|
|
|
|
}
|
2018-02-27 16:20:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 18:37:06 +00:00
|
|
|
if(!Object.values) {
|
2020-06-15 14:56:05 +00:00
|
|
|
Object.values = object => Object.keys(object).map(e => object[e]);
|
2020-12-07 18:37:06 +00:00
|
|
|
}
|
2020-06-15 14:56:05 +00:00
|
|
|
|
2020-07-19 16:49:00 +00:00
|
|
|
export = {};
|