From aa3aa469d98471ddbc8829cc0214515ec49388c6 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Fri, 6 Nov 2015 19:34:59 -0500
Subject: [PATCH] fix regression with saveas/open callbacks not getting called,
 plus a minor fix for showing the file extension in the dialog

---
 pd/nw/index.js     | 16 +++++++---------
 pd/nw/pd_canvas.js |  4 ++--
 pd/nw/pdgui.js     | 14 ++++++++++++--
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/pd/nw/index.js b/pd/nw/index.js
index b801ff1ca..7643a18b2 100644
--- a/pd/nw/index.js
+++ b/pd/nw/index.js
@@ -38,15 +38,6 @@ function open_external_doc(target) {
     gui.Shell.openExternal(target);
 }
 
-var chooser = document.querySelector("#fileDialog");
-chooser.addEventListener("change", function(evt) {
-    var file_array = this.value;
-    // reset value so that we can open the same file twice
-    this.value = null;
-    pdgui.menu_open(file_array);
-    console.log("tried to open something");
-}, false);
-
 document.getElementById("dsp_control").addEventListener("click",
     function(evt) {
         var dsp_state = this.checked ? 1 : 0;
@@ -357,6 +348,13 @@ function nw_create_pd_window_menus () {
             });
             span.innerHTML = input;
             var chooser = document.querySelector("#fileDialog");
+            chooser.onchange = function() {
+                var file_array = this.value;
+                // reset value so that we can open the same file twice
+                this.value = null;
+                pdgui.menu_open(file_array);
+                console.log("tried to open something");
+            };
             chooser.click();
         }
     }));
diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js
index cd3e93a32..fdb6aa376 100644
--- a/pd/nw/pd_canvas.js
+++ b/pd/nw/pd_canvas.js
@@ -32,7 +32,6 @@ function text_to_fudi(text) {
     text = text.replace(/(?!\\)(,|;)/g, " \\$1 "); // escape "," and ";"
     text = text.replace(/\{|\}/g, "");             // filter "{" and "}"
     text = text.replace(/\s+/g, " ");              // filter consecutive /s
-
     return text;
 }
 
@@ -348,7 +347,8 @@ var canvas_events = (function() {
         }
     ;
 
-    // Dialog events
+    // Dialog events -- these are set elsewhere now because of a bug
+    // with nwworkingdir
     document.querySelector("#saveDialog").addEventListener("change",
         function(evt) {
             pdgui.saveas_callback(name, this.value);
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 0911125af..a86be9b21 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -356,18 +356,28 @@ function gui_canvas_saveas (name, initfile, initdir) {
     if (!fs.existsSync(initdir)) {
         initdir = pwd;
     }
+    // If we don't have a ".pd" file extension (e.g., "Untitled-1", add one)
+    if (initfile.slice(-3) !== ".pd") {
+        initfile += ".pd";
+    }
     // This is complicated because of a bug... see above
     input = build_file_dialog_string({
         style: "display: none;",
         type: "file",
         id: "saveDialog",
-        nwsaveas: initdir + '/' +  initfile + ".pd",
+        nwsaveas: initdir + '/' +  initfile,
         nwworkingdir: initdir,
         accept: ".pd"
     });
     post("input is " + input);
     span.innerHTML = input;
     chooser = patchwin[name].window.document.querySelector("#saveDialog"); 
+    chooser.onchange = function() {
+        saveas_callback(name, this.value);
+        // reset value so that we can open the same file twice
+        this.value = null;
+        console.log("tried to save something");
+    }
     chooser.click();
 }
 
@@ -930,7 +940,7 @@ function connect () {
     client = new net.Socket();
     client.setNoDelay(true);
     // uncomment the next line to use fast_parser (then set its callback below)
-    // client.setEncoding("utf8");
+    //client.setEncoding("utf8");
     client.connect(PORT, HOST, function() {
         console.log("CONNECTED TO: " + HOST + ":" + PORT);
     });
-- 
GitLab