Skip to content

Commit db29707

Browse files
authored
Merge pull request #128 from PropGit/master
v1.0.4 changes
2 parents 3a72ac0 + 418d55c commit db29707

File tree

2 files changed

+77
-56
lines changed

2 files changed

+77
-56
lines changed

index.js

Lines changed: 76 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,16 @@ function log(text = "", type = mStat, socket = null, direction = 0) {
116116
if (type & mdLog) {
117117
//Send to Launcher log view
118118
let logView = $('log');
119-
//Note scroll position (to see if user has scrolled up), append message, then auto-scroll (down) if bottom was previously in view
120-
// console.log(logView.scrollTop);
121-
let scrollTop = logView.scrollTop;
122-
let scroll = (scrollTop+1 >= logView.scrollHeight-logView.clientHeight);
119+
//Note scroll position to maintain current view (user-set or auto-scroll)
120+
let scrollPosition = logView.scrollTop;
121+
let autoScroll = (scrollPosition+1 >= logView.scrollHeight-logView.clientHeight);
122+
//Add log message, delete oldest (if necessary), update display, and reposition view (if necessary)
123123
logLines.push(stamp(verboseLogging) + text + '<br>');
124-
if (logLines.length > 250) {logLines.shift()}
124+
let logIsMax = logLines.length > 250;
125+
//TODO adjust scrollPosition by calculated line height
126+
if (logIsMax) {logLines.shift(); scrollPosition -= 12;}
125127
logView.innerHTML = logLines.join('');
126-
if (scroll) {logView.scrollTo(0, logView.scrollHeight)} //else {if (scrollTop !== logView.ScrollTop) {logView.scrollTo(0, scrollTop-(logView.scrollTop-scrollTop))}}
128+
if (autoScroll) {logView.scrollTo(0, logView.scrollHeight)} else {if (logIsMax) {logView.scrollTo(0, scrollPosition)}}
127129
}
128130
//Send to Launcher console window
129131
if (type & mdConsole) {console.log(stamp(true) + text)}
@@ -174,7 +176,7 @@ var wxEnableDelay = null;
174176

175177
// Default logging and preferred port (could be overridden by stored setting)
176178
var verboseLogging = defaultVerboseLogging;
177-
var preferredPort = [{name: '', exists: false}];
179+
var storedPreferredPort = '';
178180

179181
document.addEventListener('DOMContentLoaded', function() {
180182

@@ -206,14 +208,15 @@ document.addEventListener('DOMContentLoaded', function() {
206208
$('wx-allow').checked = (result.en_wx !== undefined) ? result.en_wx : defaultWX;
207209
verboseLogging = (result.en_vlog !== undefined) ? result.en_vlog : defaultVerboseLogging;
208210
$('verbose-logging').checked = verboseLogging;
209-
preferredPort.name = (result.pref_port !== undefined) ? result.pref_port : '';
211+
storedPreferredPort = (result.pref_port !== undefined) ? result.pref_port : '';
210212
// Save subnet mask for future comparison (must be done here because chrome.storage.sync is asynchronous)
211213
sm = sm32bit();
212214
} else {
213215
storageError();
214216
}
215217
})
216218
} else {
219+
log('Launcher settings unavailable - using defaults', mDbug);
217220
$('bpc-port').value = defaultPort;
218221
$('bpc-url').value = defaultURL;
219222
$('sm0').value = defaultSM0;
@@ -222,7 +225,7 @@ document.addEventListener('DOMContentLoaded', function() {
222225
$('sm3').value = defaultSM3;
223226
$('wx-allow').checked = defaultWX;
224227
$('verbose-logging').checked = defaultVerboseLogging;
225-
preferredPort.name = '';
228+
storedPreferredPort = '';
226229
// Save subnet mask for future comparison
227230
sm = sm32bit();
228231
}
@@ -319,16 +322,29 @@ document.addEventListener('DOMContentLoaded', function() {
319322
setTimeout(connect, 500);
320323
});
321324

322-
function updatePreferredPort(port) {
323-
// Remember new preferred port (if not null)
324-
if (port && port !== preferredPort.name) {
325-
preferredPort.name = port;
326-
if (chrome.storage) {
327-
chrome.storage.sync.set({'pref_port': preferredPort.name}, function () {if (chrome.runtime.lastError) {storageError()}});
328-
}
329-
// A "new" port selection was made; set all existing ports to non-new status
330-
clearNewPortStatus();
331-
}
325+
function updatePreferredPort(port, socket) {
326+
/* Remember new preferred port (if not null).
327+
Returns true if port is new (to this socket); false otherwise.
328+
socket (required) is the sender associated with this request. */
329+
let isNewPort = false;
330+
if (port) {
331+
// First, store in settings if new
332+
if (port !== storedPreferredPort) {
333+
storedPreferredPort = port;
334+
if (chrome.storage) {
335+
chrome.storage.sync.set({'pref_port': storedPreferredPort}, function () {if (chrome.runtime.lastError) {storageError()}});
336+
}
337+
}
338+
// Next, set as preferred for this socket, if new (for this socket)
339+
let lister = portLister.find(function(p) {return p.socket === socket}); //Find the portLister object that belongs to this socket
340+
isNewPort = (lister) && (lister.prefPort.name !== port);
341+
if (isNewPort) {
342+
lister.prefPort = {name: port, exists: true};
343+
// Since a new port selection was made, set all existing ports to non-new status - applies globally to all connected browsers (sockets)
344+
clearNewPortStatus();
345+
}
346+
}
347+
return isNewPort;
332348
}
333349

334350
function sm32bit() {
@@ -338,7 +354,7 @@ function sm32bit() {
338354

339355
function storageError() {
340356
// Log Chrome Storage error
341-
log("Settings Error: " + chrome.runtime.lastError, mDbug);
357+
log("Launcher Settings Error: " + chrome.runtime.lastError, mDbug);
342358
}
343359

344360
function connect() {
@@ -401,16 +417,16 @@ function connect_ws(ws_port, url_path) {
401417
if (ws_msg.type === "load-prop") {
402418
// load the propeller
403419
log('Received Propeller Application for ' + ws_msg.action, mDbug, socket, 1);
404-
updatePreferredPort(ws_msg.portPath);
420+
updatePreferredPort(ws_msg.portPath, socket);
405421
setTimeout(function() {loadPropeller(socket, ws_msg.portPath, ws_msg.action, ws_msg.payload, ws_msg.debug)}, 10); // success is a JSON that the browser generates and expects back to know if the load was successful or not
406422
} else if (ws_msg.type === "serial-terminal") {
407423
// open or close the serial port for terminal/debug
408-
updatePreferredPort(ws_msg.portPath);
424+
updatePreferredPort(ws_msg.portPath, socket);
409425
serialTerminal(socket, ws_msg.action, ws_msg.portPath, ws_msg.baudrate, ws_msg.msg); // action is "open", "close" or "msg"
410426
} else if (ws_msg.type === "pref-port") {
411427
// user selected a new preferred port
412-
updatePreferredPort(ws_msg.portPath);
413428
log('User selected preferred port: ' + ws_msg.portPath, mDbug, socket, 1);
429+
if (updatePreferredPort(ws_msg.portPath, socket)) {sendPortList(socket)};
414430
} else if (ws_msg.type === "port-list-request") {
415431
// send an updated port list (and continue on scheduled interval)
416432
log('Site requested port list', mDbug, socket, 1);
@@ -448,9 +464,9 @@ function connect_ws(ws_port, url_path) {
448464
// Port Lister management functions
449465
// The Port Lister items are timers (and sockets) to automatically send wired/wireless port updates to connected browser sockets
450466
function addPortLister(socket) {
451-
//Create new port lister (to send port lists to browser on a timed interval).
467+
//Create new port lister (to send port lists to browser on a timed interval) using last saved preferred port as the default.
452468
//socket is the browser socket to send updates to.
453-
startPortListerScanner(portLister.push({socket: socket})-1);
469+
startPortListerScanner(portLister.push({socket: socket, prefPort: {name: storedPreferredPort, exists: false}})-1);
454470
}
455471

456472
function startPortListerScanner(idx) {
@@ -577,10 +593,10 @@ function scanWXPorts() {
577593
}
578594

579595
function sendPortList(socket) {
580-
/* Send current list of communication ports to browser via socket.
581-
(See "Launcher Communication Port Rules," above, for detailed rules and scenarios this function (and ports.js and index.js) implements.)
582-
List is ordered as: blank (rarely) or preferred (if any) followed by sorted... new wired, old wired, new wireless, then old wireless ports.
583-
New means newly-arrived (since last port selection changed); old means existing since before last port selection changed.*/
596+
/* Send list of current communication ports to browser via socket.
597+
(See "Launcher Communication Port Rules," above, for detailed rules and scenarios that this function (and ports.js and index.js) implements.)
598+
List is ordered as: blank (rarely) or preferred (if any) followed by individually sorted groups of new wired, old wired, new wireless, then old wireless ports.
599+
"New" means newly-arrived (since last port selection changed); "old" means existing since before last port selection changed.*/
584600
let bp = []; // Either empty (common) or blank string (rarely)
585601
let pp = []; // Peferred port name (qty 0 or 1)
586602
let nwp = []; // New wired port name list (often qty 0 or 1)
@@ -589,34 +605,39 @@ function sendPortList(socket) {
589605
let owlp = []; // Old Wireless port (=> 0)
590606
let qty = 0; // Quantity of ports found
591607

592-
// gather separated port lists (preferred port (if any), new wired/wireless ports (if any), old wired/wireless ports, then sort them)
593-
ports.forEach(function(p) {
594-
if (p.name === preferredPort.name) {
595-
pp.push(p.name)
596-
} else {
597-
if (p.isWired) {
598-
if (p.new) {nwp.push(p.name)} else {owp.push(p.name)}
608+
//Find the portLister object that belongs to this socket
609+
let lister = portLister.find(function(p) {return p.socket === socket});
610+
if (lister) {
611+
// Found our required lister object
612+
// gather separated port lists (preferred port (if any), new wired/wireless ports (if any), old wired/wireless ports, then sort them)
613+
ports.forEach(function(p) {
614+
if (p.name === lister.prefPort.name) {
615+
pp.push(p.name)
599616
} else {
600-
if (p.new) {nwlp.push(p.name)} else {owlp.push(p.name)}
617+
if (p.isWired) {
618+
if (p.new) {nwp.push(p.name)} else {owp.push(p.name)}
619+
} else {
620+
if (p.new) {nwlp.push(p.name)} else {owlp.push(p.name)}
621+
}
601622
}
602-
}
603-
});
604-
nwp.sort();
605-
owp.sort();
606-
nwlp.sort();
607-
owlp.sort();
608-
qty = pp.length+nwp.length+owp.length+nwlp.length+owlp.length;
609-
610-
// Remember when the preferredPort exists; otherwise if preferredPort just disappeared, clear all "new" port statuses - we only care about new-arrivals since last preferred port selection
611-
if (pp.length) {preferredPort.exists = true} else {if (preferredPort.exists) {preferredPort.exists = false; clearNewPortStatus();}}
612-
613-
// report back to editor; blank (rarely), preferred port first (if any), new wired ports (if any), old wired ports, new wireless ports (if any), and finally old wireless ports
614-
if (qty && !pp.length && !nwp.length) {bp.push("")} // Send leading blank port only if > 0 ports found, none match the preferred port, and there are no new wired ports
615-
var msg_to_send = {type:'port-list',ports:bp.concat(pp.concat(nwp.concat(owp.concat(nwlp.concat(owlp)))))};
616-
617-
log('Sending port list' + (!verboseLogging ? ' (qty '+qty+')' : ': ' + msg_to_send.ports.toString()), mDbug, socket, -1);
618-
socket.send(JSON.stringify(msg_to_send));
619-
if (chrome.runtime.lastError) {log(chrome.runtime.lastError, mDbug)}
623+
});
624+
nwp.sort();
625+
owp.sort();
626+
nwlp.sort();
627+
owlp.sort();
628+
qty = pp.length+nwp.length+owp.length+nwlp.length+owlp.length;
629+
630+
// Remember when the preferred port exists; otherwise if preferred port just disappeared, clear all "new" port statuses - we only care about new-arrivals since last preferred port selection
631+
if (pp.length) {lister.prefPort.exists = true} else {if (lister.prefPort.exists) {lister.prefPort.exists = false; clearNewPortStatus();}}
632+
633+
// report back to editor; blank (rarely), preferred port first (if any), new wired ports (if any), old wired ports, new wireless ports (if any), and finally old wireless ports
634+
if (qty && !pp.length && !nwp.length) {bp.push("")} // Send leading blank port only if > 0 ports found, none match the preferred port, and there are no new wired ports
635+
var msg_to_send = {type:'port-list',ports:bp.concat(pp.concat(nwp.concat(owp.concat(nwlp.concat(owlp)))))};
636+
637+
log('Sending port list' + (!verboseLogging ? ' (qty '+qty+')' : ': ' + msg_to_send.ports.toString()), mDbug, lister.socket, -1);
638+
lister.socket.send(JSON.stringify(msg_to_send));
639+
if (chrome.runtime.lastError) {log(chrome.runtime.lastError, mDbug)}
640+
}
620641
}
621642

622643

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "BlocklyProp Launcher",
33
"description": "A Chrome application that connects your Propeller-Powered Hardware to the BlocklyProp website.",
4-
"version": "1.0.3",
4+
"version": "1.0.4",
55
"manifest_version": 2,
66
"minimum_chrome_version": "45",
77

0 commit comments

Comments
 (0)