Ver código fonte

Different template if toast has no title

Sebastián Katzer 10 anos atrás
pai
commit
cb3e2e8057
1 arquivos alterados com 46 adições e 28 exclusões
  1. 46 28
      src/windows/LocalNotificationUtil.js

+ 46 - 28
src/windows/LocalNotificationUtil.js

@@ -93,38 +93,14 @@ exports.parseSound = function (path) {
  * @param {Object} options
  *      Local notification properties
  *
- * @return {String}
- *      Windows.Data.Xml.Dom.XmlDocument
+ * @return Windows.Data.Xml.Dom.XmlDocument
  */
 exports.build = function (options) {
-    var title = options.title,
-        message = options.text || '',
-        sound = '';
-
-    if (!title || title === '') {
-        title = 'Notification';
-    }
-
-    if (options.sound) {
-        sound = this.parseSound(options.sound);
-    }
-
-    var payload =
-        "<toast> " +
-            "<visual version='2'>" +
-                "<binding template='ToastText02'>" +
-                    "<text id='2'>" + message + "</text>" +
-                    "<text id='1'>" + title + "</text>" +
-                "</binding>" +
-            "</visual>" +
-            sound +
-            "<json>" + JSON.stringify(options) + "</json>" +
-        "</toast>";
-
-    var notification = new Windows.Data.Xml.Dom.XmlDocument();
+    var template = this.buildToastTemplate(options),
+        notification = new Windows.Data.Xml.Dom.XmlDocument();
 
     try {
-        notification.loadXml(payload);
+        notification.loadXml(template);
     } catch (e) {
         console.error(
             'LocalNotification#schedule',
@@ -141,6 +117,48 @@ exports.build = function (options) {
     return notification;
 };
 
+/**
+ * Builds the toast template with the right style depend on the options.
+ *
+ * @param {Object} options
+ *      Local notification properties
+ *
+ * @return String
+ */
+exports.buildToastTemplate = function (options) {
+    var title = options.title,
+        message = options.text || '',
+        json = JSON.stringify(options),
+        sound = '';
+
+    if (options.sound && options.sound !== '') {
+        sound = this.parseSound(options.sound);
+    }
+
+    if (title && title !== '') {
+        return  "<toast>" +
+                    "<visual>" +
+                        "<binding template='ToastText02'>" +
+                            "<text id='1'>" + title + "</text>" +
+                            "<text id='2'>" + message + "</text>" +
+                        "</binding>" +
+                    "</visual>" +
+                    sound +
+                    "<json>" + json + "</json>" +
+                "</toast>";
+    } else {
+        return  "<toast>" +
+                    "<visual>" +
+                        "<binding template='ToastText01'>" +
+                            "<text id='1'>" + message + "</text>" +
+                        "</binding>" +
+                    "</visual>" +
+                    sound +
+                    "<json>" + json + "</json>" +
+                "</toast>";
+    }
+};
+
 /**
  * Short-hand method for the toast notification history.
  */