/** * Copyright JS Foundation and other contributors, http://js.foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ RED.palette.editor = (function() { var disabled = false; var editorTabs; var filterInput; var searchInput; var nodeList; var packageList; var loadedList = []; var filteredList = []; var loadedIndex = {}; var typesInUse = {}; var nodeEntries = {}; var eventTimers = {}; var activeFilter = ""; function semVerCompare(A,B) { var aParts = A.split(".").map(function(m) { return parseInt(m);}); var bParts = B.split(".").map(function(m) { return parseInt(m);}); for (var i=0;i<3;i++) { var j = aParts[i]-bParts[i]; if (j<0) { return -1 } if (j>0) { return 1 } } return 0; } function delayCallback(start,callback) { var delta = Date.now() - start; if (delta < 300) { delta = 300; } else { delta = 0; } setTimeout(function() { callback(); },delta); } function changeNodeState(id,state,shade,callback) { shade.show(); var start = Date.now(); $.ajax({ url:"nodes/"+id, type: "PUT", data: JSON.stringify({ enabled: state }), contentType: "application/json; charset=utf-8" }).done(function(data,textStatus,xhr) { delayCallback(start,function() { shade.hide(); callback(); }); }).fail(function(xhr,textStatus,err) { delayCallback(start,function() { shade.hide(); callback(xhr); }); }) } function installNodeModule(id,version,url,callback) { var requestBody = { module: id }; if (version) { requestBody.version = version; } if (url) { requestBody.url = url; } $.ajax({ url:"nodes", type: "POST", data: JSON.stringify(requestBody), contentType: "application/json; charset=utf-8" }).done(function(data,textStatus,xhr) { callback(); }).fail(function(xhr,textStatus,err) { callback(xhr); }); } function removeNodeModule(id,callback) { $.ajax({ url:"nodes/"+id, type: "DELETE" }).done(function(data,textStatus,xhr) { callback(); }).fail(function(xhr,textStatus,err) { callback(xhr); }) } function refreshNodeModuleList() { for (var id in nodeEntries) { if (nodeEntries.hasOwnProperty(id)) { _refreshNodeModule(id); } } } function refreshNodeModule(module) { if (!eventTimers.hasOwnProperty(module)) { eventTimers[module] = setTimeout(function() { delete eventTimers[module]; _refreshNodeModule(module); },100); } } function getContrastingBorder(rgbColor){ var parts = /^rgba?\(\s*(\d+),\s*(\d+),\s*(\d+)[,)]/.exec(rgbColor); if (parts) { var r = parseInt(parts[1]); var g = parseInt(parts[2]); var b = parseInt(parts[3]); var yiq = ((r*299)+(g*587)+(b*114))/1000; if (yiq > 160) { r = Math.floor(r*0.8); g = Math.floor(g*0.8); b = Math.floor(b*0.8); return "rgb("+r+","+g+","+b+")"; } } return rgbColor; } function formatUpdatedAt(dateString) { var now = new Date(); var d = new Date(dateString); var delta = (Date.now() - new Date(dateString).getTime())/1000; if (delta < 60) { return RED._('palette.editor.times.seconds'); } delta = Math.floor(delta/60); if (delta < 10) { return RED._('palette.editor.times.minutes'); } if (delta < 60) { return RED._('palette.editor.times.minutesV',{count:delta}); } delta = Math.floor(delta/60); if (delta < 24) { return RED._('palette.editor.times.hoursV',{count:delta}); } delta = Math.floor(delta/24); if (delta < 7) { return RED._('palette.editor.times.daysV',{count:delta}) } var weeks = Math.floor(delta/7); var days = delta%7; if (weeks < 4) { return RED._('palette.editor.times.weeksV',{count:weeks}) } var months = Math.floor(weeks/4); weeks = weeks%4; if (months < 12) { return RED._('palette.editor.times.monthsV',{count:months}) } var years = Math.floor(months/12); months = months%12; if (months === 0) { return RED._('palette.editor.times.yearsV',{count:years}) } else { return RED._('palette.editor.times.year'+(years>1?'s':'')+'MonthsV',{y:years,count:months}) } } function _refreshNodeModule(module) { if (!nodeEntries.hasOwnProperty(module)) { nodeEntries[module] = {info:RED.nodes.registry.getModule(module)}; var index = [module]; for (var s in nodeEntries[module].info.sets) { if (nodeEntries[module].info.sets.hasOwnProperty(s)) { index.push(s); index = index.concat(nodeEntries[module].info.sets[s].types) } } nodeEntries[module].index = index.join(",").toLowerCase(); nodeList.editableList('addItem', nodeEntries[module]); } else { var moduleInfo = nodeEntries[module].info; var nodeEntry = nodeEntries[module].elements; if (nodeEntry) { var activeTypeCount = 0; var typeCount = 0; var errorCount = 0; nodeEntry.errorList.empty(); nodeEntries[module].totalUseCount = 0; nodeEntries[module].setUseCount = {}; for (var setName in moduleInfo.sets) { if (moduleInfo.sets.hasOwnProperty(setName)) { var inUseCount = 0; var set = moduleInfo.sets[setName]; var setElements = nodeEntry.sets[setName]; if (set.err) { errorCount++; $("