From 4af4487846ea2d3cbef9d310d6d7b3503e96ee9c Mon Sep 17 00:00:00 2001 From: tigerbot Date: Mon, 24 Apr 2017 10:53:28 -0600 Subject: [PATCH] made the notifications startable/stoppable --- drag-drop-render.js | 5 +++-- index.html | 11 ++++++---- notifications.js | 50 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/drag-drop-render.js b/drag-drop-render.js index 642ee05..ad71f36 100644 --- a/drag-drop-render.js +++ b/drag-drop-render.js @@ -6,11 +6,12 @@ function handleFiles(files) { files = [ files ]; } - document.body.appendChild(document.createElement('br')); + var container = document.getElementsByClassName('file-container')[0]; + container.innerHTML = ''; files.forEach(function (name) { var div = document.createElement('div'); div.innerHTML = name; - document.body.appendChild(div); + container.appendChild(div); }); } diff --git a/index.html b/index.html index 714f1b2..1faf0dc 100644 --- a/index.html +++ b/index.html @@ -9,10 +9,13 @@
-

Hello World!

- We are using node , - Chrome , - and Electron . +

Notifications

+ + +
+
+

Drag and Drop Area

+
diff --git a/notifications.js b/notifications.js index 8a8c050..0346ecb 100644 --- a/notifications.js +++ b/notifications.js @@ -1,11 +1,21 @@ -;(function () { - var electron = require('electron'); - var ipc = electron.ipcRenderer; - // This file runs in a render thread of the Electron app. It must be required (directly or - // indirectly) from one of the html files. +var electron = require('electron'); +var ipc = electron.ipcRenderer; +// This file runs in a render thread of the Electron app. It must be required (directly or +// indirectly) from one of the html files. + +var timeoutId; +var stopped; +var count; + +function startNotifications() { + stopped = false; + count = 0; + if (timeoutId) { + clearTimeout(timeoutId); + } - var count = 0; function notifyUser() { + timeoutId = null; count += 1; var notif = new window.Notification('Annoying Alert ' + count, { body: 'See what happens when you try to click on it.', @@ -18,11 +28,31 @@ notif.close(); }; notif.onclose = function () { - if (count < 10) { - setTimeout(notifyUser, 5000); + if (!stopped && count < 10) { + timeoutId = setTimeout(notifyUser, 5000); } }; ipc.send('notification', count); } - setTimeout(notifyUser, 1000); -}()); + + timeoutId = setTimeout(notifyUser, 1000); +} + +function stopNotifications() { + stopped = true; + if (timeoutId) { + clearTimeout(timeoutId); + timeoutId = null; + } + // Let the progress bar know that we are finished. + ipc.send('notification', 0); +} + +document.body.addEventListener('click', function (ev) { + if (ev.target.classList.contains('start-notify')) { + startNotifications(); + } + else if (ev.target.classList.contains('stop-notify')) { + stopNotifications(); + } +});