瀏覽代碼

feat: persistent toasts can either be destroyed or shown/hid

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 4 年之前
父節點
當前提交
23aad44263
共有 4 個文件被更改,包括 38 次插入17 次删除
  1. 0 1
      README.md
  2. 0 0
      dist/toasters.js
  3. 12 2
      example/index.js
  4. 26 14
      src/toasters.js

+ 0 - 1
README.md

@@ -38,7 +38,6 @@ new Toast({
 - Design improvements
 - Design improvements
 - Error handling (i.e. you haven't provided any content for the toast)
 - Error handling (i.e. you haven't provided any content for the toast)
 - Ability to parse markdown or html
 - Ability to parse markdown or html
-- Ability to change to persistent
 
 
 <!--See [example](https://github.com/jonathan-grah/vue-roaster/tree/master/example) folder for more details.-->
 <!--See [example](https://github.com/jonathan-grah/vue-roaster/tree/master/example) folder for more details.-->
 
 

文件差異過大導致無法顯示
+ 0 - 0
dist/toasters.js


+ 12 - 2
example/index.js

@@ -1,5 +1,15 @@
 import Toast from "../dist/toasters.js";
 import Toast from "../dist/toasters.js";
 
 
-new Toast({
-	content: "Hello World"
+const TestNotification = new Toast({
+	content: "Hello World",
+	persistent: true,
+	interactable: false
 });
 });
+
+setTimeout(() => {
+	TestNotification.hide();
+
+	setTimeout(() => {
+		TestNotification.show();
+	}, 2000);
+}, 2000);

+ 26 - 14
src/toasters.js

@@ -4,19 +4,20 @@ let incrementer = 0;
 
 
 export default class Toast {
 export default class Toast {
 	// should add type checking
 	// should add type checking
-	constructor({ content, persistent, timeout }) {
+	constructor({ content, persistent, interactable, timeout }) {
 		this.identifier = `toast-${incrementer}`;
 		this.identifier = `toast-${incrementer}`;
 		incrementer++;
 		incrementer++;
 
 
 		this.visible = false;
 		this.visible = false;
 
 
 		this.content = content;
 		this.content = content;
-		this.persistent = persistent;
-		this.timeout = timeout;
+		this.persistent = persistent !== undefined ? persistent : false;
+		this.timeout = timeout ? timeout : 6000;
+		this.interactable = interactable !== undefined ? interactable : true;
 
 
 		if (!this.persistent) this.startTimer();
 		if (!this.persistent) this.startTimer();
 
 
-		this.dragListener();
+		if (this.interactable) this.dragListener();
 	}
 	}
 
 
 	get content() {
 	get content() {
@@ -25,14 +26,7 @@ export default class Toast {
 
 
 	set content(value) {
 	set content(value) {
 		this._content = value;
 		this._content = value;
-		this.display();
-	}
-
-	startTimer() {
-		setTimeout(this.clear.bind(this), this.timeout ? this.timeout : 6000);
-	}
-
-	display() {
+		
 		if (!this.visible) {
 		if (!this.visible) {
 			container.insertAdjacentHTML("beforeend", `<div class="toast ${this.identifier}">${this.content}</div>`);
 			container.insertAdjacentHTML("beforeend", `<div class="toast ${this.identifier}">${this.content}</div>`);
 			this.find();
 			this.find();
@@ -42,6 +36,10 @@ export default class Toast {
 		this.visible = true;
 		this.visible = true;
 	}
 	}
 
 
+	startTimer() {
+		setTimeout(this.destroy.bind(this), this.timeout);
+	}
+
 	dragListener() {
 	dragListener() {
 		/** handles mouse drag **/
 		/** handles mouse drag **/
 		this.element.addEventListener("mousedown", event => {
 		this.element.addEventListener("mousedown", event => {
@@ -88,7 +86,7 @@ export default class Toast {
 	}
 	}
 
 
 	handleInputLoss() {
 	handleInputLoss() {
-		if (this.element.style.opacity < 0.15) return this.clear();
+		if (this.element.style.opacity < 0.15) return this.destroy();
 		else {
 		else {
 			this.element.style.opacity = 1;
 			this.element.style.opacity = 1;
 			this.element.style.left = 0;
 			this.element.style.left = 0;
@@ -105,7 +103,21 @@ export default class Toast {
 				return this.element = container.childNodes[i];
 				return this.element = container.childNodes[i];
 	}
 	}
 
 
-	clear() {
+	destroy() {
 		this.element.remove();
 		this.element.remove();
 	}
 	}
+
+	show() {
+		if (!this.visible) {
+			this.element.style.display = "block";
+			this.visible = true;
+		}
+	}
+
+	hide() {
+		if (this.visible) {
+			this.element.style.display = "none";
+			this.visible = false;
+		}
+	}
 }
 }

部分文件因文件數量過多而無法顯示