From 5b54666f39fd7e929b88b362c83e12a224a5af61 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 20 Mar 2016 20:04:09 +0100 Subject: [PATCH] added support for pseudo global circuit, started dynamic switching of pipes, nicer format evaluation --- contrib/html/index.html | 141 +++++++++++++++++++++++------- contrib/html/items/broadcast.json | 2 +- contrib/html/items/global.json | 8 ++ 3 files changed, 116 insertions(+), 35 deletions(-) create mode 100644 contrib/html/items/global.json diff --git a/contrib/html/index.html b/contrib/html/index.html index 06b80a92..e9d38278 100644 --- a/contrib/html/index.html +++ b/contrib/html/index.html @@ -143,7 +143,14 @@ jsPlumb.ready(function() { if (anchor.hasOwnProperty('update') && needUpdates) { var update = anchor.update; update.endpoint = endp; - needUpdates.push(update); + if (anchor.hasOwnProperty('connectWith')) { + if (needConnections) { + update.connectWith = anchor.connectWith; + } + needUpdates.push(update); + } else { + endp.needUpdate = update; + } } } }; @@ -250,12 +257,25 @@ jsPlumb.ready(function() { } }*/ }; - var buildText = function(format, fields) { + var formatGlobalField = function(name, value) { + if (name=='signal') { + return value==1?'OK':'none'; + } + if (name=='lastup') { + return new Date(value*1000).toLocaleString().replace(' ', ' '); + } + return ''; + }; + var buildText = function(format, message, circuit, name) { + var fields = circuit=='global'?message:(message?message.fields:null); var ret = ''; if (format) { format = format.split(' '); for (var i=0; i0) ret += ', '; + if (ret.length>0) { + if (ret[ret.length-1]==':') ret += ' '; + else ret += ', '; + } if (format[i][0]!='$') { if (format[i]=='\\n') ret += "
"; @@ -267,13 +287,20 @@ jsPlumb.ready(function() { if (field.unit) { ret += ' '+field.unit; } + } else if (circuit=='global' && format[i]=='$' && !Array.isArray(fields)) { + ret += formatGlobalField(name, fields); } else { ret += '?'; } } + } else if (circuit=='global' && !Array.isArray(fields)) { + ret += formatGlobalField(name, fields) } else if (fields) { for (var i in fields) { - if (ret.length>0) ret += ', '; + if (ret.length>0) { + if (ret[ret.length-1]==':') ret += ' '; + else ret += ', '; + } var field = fields[i]; ret += field.hasOwnProperty('value')&&field.value!=null?field.value:'?'; if (field && field.unit) { @@ -302,18 +329,24 @@ jsPlumb.ready(function() { var found = false; var fields = allMessages[condition[k]][condition[k+1]].fields; var cond = condition[k+2]; - if (!fields || !fields[0] || !fields[0].hasOwnProperty('value') || !isNaN(Array.isArray(cond) ? cond[0] : cond) && isNaN(fields[0].value)) { - fields = getFields(condition[k], condition[k+1], true, false, true); - if (!fields || !fields[0] || !fields[0].hasOwnProperty('value')) return false; + var key = 0; + if (fields && !fields.hasOwnProperty(key)) { + for (key in fields) { + break; + } + } + if (!fields || !fields[key] || !fields[key].hasOwnProperty('value') || !isNaN(Array.isArray(cond) ? cond[0] : cond) && isNaN(fields[key].value)) { + fields = getFields(condition[k], condition[k+1], key==0, false, true); + if (!fields || !fields[key] || !fields[key].hasOwnProperty('value')) return false; } if (Array.isArray(cond)) { for (var i=0; i=needConnections.length) { - first = false; - i = -1; - } - continue; - } var avail = new Array(); var best = 99; endpoints.each(function(endpoint) { @@ -441,6 +468,9 @@ jsPlumb.ready(function() { } }); dst = avail.length>0 ? avail[0] : null; + if (dst && !instance.getEndpoint(src).isTarget) { + resolveDsts["-"+data[2]] = dst; + } } else { dst = data[1]+data[2]; } @@ -452,19 +482,37 @@ jsPlumb.ready(function() { } var type = 'off';//TODO dst.indexOf("Flow")>=0?'hot':'cold'; var conn = instance.connect({uuids: [src, dst], editable: true, type: type}); - if (conn && data.length>4 && data[3]) { // add label - connectionItems.push({conn: conn, data: data}); + if (!conn) continue; + connectionItems[src+"-"+dst] = {conn: conn, data: data}; + var srcendpoint = instance.getEndpoint(src); + var dstendpoint = instance.getEndpoint(dst); + if (srcendpoint.needUpdate) { + var update = srcendpoint.needUpdate; + delete(srcendpoint.needUpdate); + update.endpoint = srcendpoint; + update.connectWith = dstendpoint.getUuid(); + needUpdates.push(update); + console.log("src need update "+conn); + } + if (dstendpoint.needUpdate) { + var update = dstendpoint.needUpdate; + delete(dstendpoint.needUpdate); + update.endpoint = srcendpoint; + update.connectWith = dstendpoint.getUuid(); + needUpdates.push(update); + console.log("dst need update "+conn); + } + if (data.length>4 && data[3]) { // add label var cmessages = allMessages[data[3]]; var message = cmessages[data[4]]; var over = conn.getOverlay("label"); - over.setLabel(buildText(data.length>5?data[5]:null, message.fields)); + over.setLabel(buildText(data.length>5?data[5]:null, message, data[3], data[4])); var key = data[3]+'/'+data[4]; if (!needUris[key]) { needUris.push(key); } } } - // TODO needUpdates // request all shown values and enable automatic polling for (var i=0; i0) { needed = true; } else console.log(key+':'+fields); - if (!fields) { + if (cname!='global' && !message.fields) { continue; } each(elems, function(elem) { - elem.innerHTML = buildText(elem.getAttribute('data-format'), fields); + elem.innerHTML = buildText(elem.getAttribute('data-format'), message, cname, mname); }); } } + // run needUpdates + for (var i=0; i