/** * 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 = ""; var semverre = /^(\d+)(\.(\d+))?(\.(\d+))?(-([0-9A-Za-z-]+))?(\.([0-9A-Za-z-.]+))?$/; var NUMBERS_ONLY = /^\d+$/; function SemVerPart(part) { this.number = 0; this.text = part; if ( NUMBERS_ONLY.test(part)){ this.number = parseInt(part); this.type = "N"; } else { this.type = part == undefined || part.length < 1 ? "E" : "T"; } } SemVerPart.prototype.compare = function(other) { var types = this.type + other.type; switch ( types ) { case "EE": return 0; case "NT": case "TE": case "EN": return -1; case "NN": return this.number - other.number; case "TT": return this.text.localeCompare( other.text ); case "ET": case "TN": case "NE": return 1; } }; function SemVer(ver) { var groups = ver.match( semverre ); this.parts = [ new SemVerPart( groups[1] ), new SemVerPart( groups[3] ), new SemVerPart( groups[5] ), new SemVerPart( groups[7] ), new SemVerPart( groups[9] ) ]; } SemVer.prototype.compare = function(other) { var result = 0; for ( var i = 0, n = this.parts.length; result == 0 && i < n; i++ ) { result = this.parts[ i ].compare( other.parts[ i ] ); } return result; }; function semVerCompare(ver1, ver2) { var semver1 = new SemVer(ver1); var semver2 = new SemVer(ver2); var result = semver1.compare(semver2); return result; } 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++; var errMessage = set.err; if (set.err.message) { errMessage = set.err.message; } else if (set.err.code) { errMessage = set.err.code; } $("