Skip to content

Commit

Permalink
[ENH] more flexible collection helper
Browse files Browse the repository at this point in the history
  • Loading branch information
n3o77 committed Jul 27, 2021
1 parent 5f90458 commit d64a3c5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@types/jquery": "^3.5.6",
"es-cookie": "^1.2.0"
}
}
27 changes: 18 additions & 9 deletions src/helper/CollectionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export class CollectionHelper {

constructor(element: JQuery|any) {
this.container = jQuery(element);
if (this.container.data('collectionHelper') instanceof CollectionHelper) {
throw new Error('Collection Helper already initialized for this container');
}
this.container.data('collectionHelper', this);

this.allowAdd = this.container.data('allow-add');
this.allowDelete = this.container.data('allow-delete');
this.prototype = this.container.data('prototype');
Expand All @@ -38,20 +43,24 @@ export class CollectionHelper {
}

protected loadEvents() {
if (this.allowAdd) this.addButton.on('click', this.addRow.bind(this));
if (this.allowAdd) this.addButton.on('click', (e) => {
e.preventDefault();
this.addRow();
});
this.loadDeleteBtnEvents();
}

protected loadDeleteBtnEvents() {
if (this.allowDelete) this.deleteButtons.on('click', this.deleteRow.bind(this));
if (this.allowDelete) this.deleteButtons.on('click', e => {
e.preventDefault();
this.deleteRow(e.target);
});
}

protected init() {
}

protected addRow(e: JQuery.Event) {
e.preventDefault();

public addRow(): JQuery {
let row:string = this.prototype.replace(/__name__label__/g, this.currentCnt.toString());
row = row.replace(/__name__/g, this.currentCnt.toString());

Expand All @@ -63,12 +72,12 @@ export class CollectionHelper {

this.container.trigger('unl.row_added', el);
this.currentCnt++;
}

protected deleteRow(e: JQuery.TriggeredEvent) {
e.preventDefault();
return el;
}

let target = jQuery(e.target);
public deleteRow(target: HTMLElement|JQuery) {
target = jQuery(target);
target.parents('[role="' + this.collectionRowRole + '"]').remove();
this.container.trigger('unl.row_deleted', target);
}
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@types/jquery@^3.5.6":
version "3.5.6"
resolved "https://registry.yarnpkg.com/@types/jquery/-/jquery-3.5.6.tgz#97ac8e36dccd8ad8ed3f3f3b48933614d9fd8cf0"
integrity sha512-SmgCQRzGPId4MZQKDj9Hqc6kSXFNWZFHpELkyK8AQhf8Zr6HKfCzFv9ZC1Fv3FyQttJZOlap3qYb12h61iZAIg==
dependencies:
"@types/sizzle" "*"

"@types/sizzle@*":
version "2.3.3"
resolved "https://registry.yarnpkg.com/@types/sizzle/-/sizzle-2.3.3.tgz#ff5e2f1902969d305225a047c8a0fd5c915cebef"
integrity sha512-JYM8x9EGF163bEyhdJBpR2QX1R5naCJHC8ucJylJ3w9/CVBaskdQ8WqBf8MmQrd1kRvp/a4TS8HJ+bxzR7ZJYQ==

es-cookie@^1.2.0:
version "1.3.2"
resolved "https://registry.yarnpkg.com/es-cookie/-/es-cookie-1.3.2.tgz#80e831597f72a25721701bdcb21d990319acd831"
integrity sha512-UTlYYhXGLOy05P/vKVT2Ui7WtC7NiRzGtJyAKKn32g5Gvcjn7KAClLPWlipCtxIus934dFg9o9jXiBL0nP+t9Q==

0 comments on commit d64a3c5

Please sign in to comment.