diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/README.markdown b/README.md similarity index 96% rename from README.markdown rename to README.md index 281b298..2503534 100644 --- a/README.markdown +++ b/README.md @@ -16,7 +16,7 @@ Creating a form is easy: fields: [f1, f2] }); -You can call the constructor for Forms directly with $form: +You can call the constructor for Forms directly with \$form: $form({ ... diff --git a/lib/strophe.x.d.ts b/lib/strophe.x.d.ts new file mode 100644 index 0000000..59d7a36 --- /dev/null +++ b/lib/strophe.x.d.ts @@ -0,0 +1,150 @@ +declare type FormType = "form" | "submit" | "cancel" | "result"; +declare type FormOptions = { + type?: FormType; + title?: string; + instructions?: string; + fields?: Field[]; + items?: Item[]; +}; +declare class Form { + type: FormType; + title: string | undefined; + instructions: string | undefined; + fields: Field[]; + items: Item[]; + reported: string[]; + constructor(opt?: FormOptions); + updateReported(): void; + toXML(): Element; + toJSON(): { + type: FormType; + title: string | undefined; + instructions: string | undefined; + fields: { + type: FieldType; + var: string; + required: boolean; + desc: string | undefined; + label: string | undefined; + values: string[]; + options: { + label: string; + value: string; + }[]; + }[]; + items: { + fields: { + type: FieldType; + var: string; + required: boolean; + desc: string | undefined; + label: string | undefined; + values: string[]; + options: { + label: string; + value: string; + }[]; + }[]; + }[]; + reported: string[]; + }; + toHTML(): HTMLElement; + static fromXML(xmlIn: Element): Form; + static fromHTML(htmlIn: HTMLElement): Form; +} +declare type FieldType = "boolean" | "fixed" | "hidden" | "jid-multi" | "jid-single" | "list-multi" | "list-single" | "text-multi" | "text-private" | "text-single"; +declare type FieldValue = string | number; +interface FieldOptions { + type: FieldType; + desc?: string; + label?: string; + var?: string; + required?: boolean | "true"; + options?: FieldOption[]; + value?: FieldValue; + values?: FieldValue[]; +} +declare class Field { + type: FieldType; + desc: string | undefined; + label: string | undefined; + var: string; + required: boolean; + options: FieldOption[]; + values: string[]; + constructor(opt: FieldOptions); + addValue(val: FieldValue): this; + addValues(vals: FieldValue[]): this; + addOption(opt: FieldOption): this; + addOptions(opts: FieldOption[]): this; + toJSON(): { + type: FieldType; + var: string; + required: boolean; + desc: string | undefined; + label: string | undefined; + values: string[]; + options: { + label: string; + value: string; + }[]; + }; + toXML(): Element; + toHTML(): HTMLElement; + static fromXML(xmlIn: Element): Field; + static _htmlElementToFieldType(html: HTMLElement): FieldType; + static fromHTML(htmlIn: HTMLElement): Field; +} +declare type FieldOptionOptions = { + label?: string; + value?: FieldValue; +}; +declare class FieldOption { + label: string; + value: string; + constructor(opt?: FieldOptionOptions); + toXML(): Element; + toJSON(): { + label: string; + value: string; + }; + toHTML(): HTMLElement; + static fromXML(xml: Element): FieldOption; + static fromHTML(html: HTMLElement): FieldOption; +} +declare type ItemOptions = { + fields?: Field[]; +}; +declare class Item { + fields: Field[]; + constructor(opts?: ItemOptions); + toXML(): Element; + toJSON(): { + fields: { + type: FieldType; + var: string; + required: boolean; + desc: string | undefined; + label: string | undefined; + values: string[]; + options: { + label: string; + value: string; + }[]; + }[]; + }; + toHTML(): HTMLElement; + static fromXML(xmlIn: Element): Item; + static fromHTML(html: HTMLElement): Item; +} +declare type StropheDataforms = { + Form: typeof Form; + Field: typeof Field; + Option: typeof FieldOption; + Item: typeof Item; +}; +declare const $form: (opt: FormOptions) => Form; +declare const $field: (opt: FieldOptions) => Field; +declare const $opt: (opt: FieldOptionOptions) => FieldOption; +declare const $item: (opt: ItemOptions) => Item; +export { $form, $field, $opt, $item, StropheDataforms }; diff --git a/lib/strophe.x.js b/lib/strophe.x.js new file mode 100644 index 0000000..8f12ce2 --- /dev/null +++ b/lib/strophe.x.js @@ -0,0 +1,556 @@ +"use strict"; +// Copyright (c) Markus Kohlhase, 2011 +var __spreadArrays = (this && this.__spreadArrays) || function () { + for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; + for (var r = Array(s), k = 0, i = 0; i < il; i++) + for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) + r[k] = a[j]; + return r; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var strophe_js_1 = require("strophe.js"); +// a little helper +var helper = { + fill: function (src, target, klass) { + return __spreadArrays(target, src.map(function (f) { return (f instanceof klass ? f : new klass(f)); })); + }, + createHtmlFieldCouple: function (f) { + var div = $("