/*//////////////////////////////////////////////////////////////////////////// / sldAssist_fu: Statistical Linked Data Assistant for Users v0.7.2 / Copyright (c) 2013-2014 Hideto Sato / / sldAssist_fu is freely distributable under the terms of an MIT-style license. / For details, see the StatLD web site: http://satolab.tiu.ac.jp/statld/ / / Date: 2013/11/12, Last Modified: 2014/2/1 ////////////////////////////////////////////////////////////////////////////*/ var sldAssist = {}; sldAssist.fu = {}; sldAssist.fu.version = "0.7.2"; sldAssist.fu.alertErrorLevel = 0; // This location is necessary for checking user programs. // 0: Only text message, 1: + Exceptions, 2: + All sldAssist.fu.tempFns = []; /**************************************************************************** * sldAssist.fu.Main class *****************************************************************************/ sldAssist.fu.Main = function() { this.options = { alertAllErrors: sldAssist.fu.alertAllErrors }; this.originalData = []; this.currentData = []; this.log = ""; this.prevTime = null; this.isCompleted = false; }; sldAssist.fu.Main.prototype = { execute: function(exeOptions) { try { this.isCompleted = false; this.log = ""; var dd = new Date(); this.startTime = dd.getTime(); this.writeLog("main", "start"); this.setup(); var root = new sldAssist.plugin.Root(); var exeOptions1 = sldAssist.utility.copyObject(exeOptions); root.setup(this, "root", "root", {}, exeOptions1); root.setData(this.originalData, this.currentData); root.execute(); } catch(e) { this.onError(e); } }, setup: function() { this.setOnError(this.options.alertAllErrors !== false); }, setData: function(originalData, currentData) { this.originalData = originalData; this.currentData = currentData; }, setOnError: function(alertLevel) { if (alertLevel === -1) { window.onerror = undefined; } else if (alertLevel > 1) { var _this = this; window.onerror = function(errMsg, errFile, errNo) { _this.showError(errMsg, errFile, errNo); //return true; // avoid to display an error message in the browser return false; }; } }, showError: function(errMsg, errFile, errNo) { alert("Error: "+errMsg+"\nfile: "+errFile+" line: "+errNo); }, onError: function(e) { if (typeof e === "object") { if (sldAssist.fu.alertErrorLevel > 0) { if (navigator.userAgent.indexOf("Firefox") >= 0) { var fname = sldAssist.utility.shortFileName(e.fileName); this.showError(e.message, fname, e.lineNumber); } else { alert(e.toString()); } } throw e; } else { alert(e); } }, writeLog: function(pluginName, status) { var dd = new Date(); var now = dd.getTime(); if (this.prevTime !== null) { var elapse = now - this.prevTime; this.log += elapse + " ms " + pluginName + " : " + status + "\n"; } this.prevTime = now; }, getData: function() { return this.currentData; }, onComplete: function() {} }; sldAssist.fu.main = new sldAssist.fu.Main(); sldAssist.fu.main.setOnError(sldAssist.fu.alertErrorLevel); // This is necessary here for checking user codes. /**************************************************************************** * sldAssist.plugin.AbstractPlugin class *****************************************************************************/ sldAssist.plugin = {}; sldAssist.plugin.AbstractPlugin = function() {}; sldAssist.plugin.AbstractPlugin.prototype = { setup: function(main, name, nameWithSuffix, options, restOptions) { this.main = main; this.name = name; this.nameWithSuffix = nameWithSuffix; this.setOptions(options); this.checkOptions(); this.nextPlugin = null; var nextPluginSpec = sldAssist.utility.assocShift(restOptions); if (! nextPluginSpec) return; var nextPluginName = this.getGenuineName(nextPluginSpec.key); if (! sldAssist.plugin[nextPluginName]) { throw "Illegal plugin name (" + nextPluginName + ")."; } this.nextPlugin = new sldAssist.plugin[nextPluginName](); this.nextPlugin.setup(main, nextPluginName, nextPluginSpec.key, nextPluginSpec.value, restOptions); }, checkOptions: function() {}, setData: function(originalData, currentData) { this.originalData = originalData; this.currentData = currentData; }, execute: function() { try { this.main.writeLog(this.name, "called"); if (! this.checkExecute()) return; this.main.writeLog(this.name, "start"); this.doExecute(); this.main.writeLog(this.name, "end"); } catch (e) { sldAssist.fu.main.onError(e); } }, checkExecute: function() { return true; }, doExecute: function() { throw "doExecute() must be overridden."; }, doAfterExecute: function() { var data1 = sldAssist.utility.copyObject(this.currentData); this.main.setData(this.originalData, data1); if (this.nextPlugin) { var data2 = sldAssist.utility.copyObject(this.currentData); this.nextPlugin.setData(this.originalData, data2); this.nextPlugin.execute(); } else { this.main.isCompleted = true; if (this.main.onComplete) this.main.onComplete(); } this.originalData = this.currentData = null; }, getGenuineName: function(name) { return name.replace(/_[0-9]{0,2}$/, ""); }, getDefaultOptions: function() { return this.defaultOptions; }, setOptions: function(options) { sldAssist.utility.setOptions(options, this.getDefaultOptions()); this.options = options; }, getElementById: function(id) { var elm = document.getElementById(id); if (! elm) { throw "Illegal element id (" + id + ")"; } return elm; }, checkColumns: function(data, columns) { for (var j=0; j" + "" ; if (options.title) { html += ""; } var i1 = 0; if (useHeader) { html += " "; if (showRowNumber) html += ""; for(var j=0; j" + data[0][j] + ""; } html += ""; i1 = 1; } var fno = -1; if (options.clickableColNames && options.onclick) { var f = function(elm) { var rowNo = elm.parentNode.getAttribute("row"); var colNo = elm.parentNode.getAttribute("col"); options.onclick({row:rowNo, column:colNo}); }; sldAssist.fu.tempFns.push(f); fno = sldAssist.fu.tempFns.length - 1; } for(var i = i1; i< data.length; i++) { html += " "; if (showRowNumber) html += ""; for(j=0; j"; if ((fno >= 0) && sldAssist.utility.indexOfArray(options.clickableColNames, data[0][j]) >= 0) { html += "" + data[i][j] + ""; } else { html += data[i][j]; } html += ""; } html += ""; } html += "
" + options.title + "
No.
" + i + "
" ; return html; }; /**************************************************************************** * sldAssist.plugin.jqDialog extends sldAssist.plugin.data_show *****************************************************************************/ sldAssist.plugin.jqDialog = function() {}; sldAssist.plugin.jqDialog.prototype = new sldAssist.plugin.data_show(); sldAssist.plugin.jqDialog.prototype.doExecute = function() { if (this.options.placeId) { if (! this.options.showRowNumber) this.options.showRowNumber = false; var html = this.getTableHtml(this.currentData, this.options); var $dialog = $("#" + this.options.placeId); $dialog.html(html); $dialog.dialog({ modal: true, width: "auto" }); if (this.options.top) $dialog.parent().css("top", this.options.top); if (this.options.left) $dialog.parent().css("left", this.options.left); } }; /**************************************************************************** * sldAssist.plugin.data_get extends sldAssist.plugin.AbstractView *****************************************************************************/ sldAssist.plugin.data_get = function() {}; sldAssist.plugin.data_get.prototype = new sldAssist.plugin.AbstractView(); sldAssist.plugin.data_get.prototype.checkExecute = function() { return sldAssist.plugin.AbstractHttpRequester.prototype.checkExecute.apply(this); }; sldAssist.plugin.data_get.prototype.doExecute = function() { var text; if (this.options.type === "tabText") { text= this.getTabText(this.currentData); } else { text= this.getArrayText(this.currentData); } if (this.options.textareaId) { this.getElementById(this.options.textareaId).value = text; } else { var bodyElements = document.getElementsByTagName("body"); var divElement = document.createElement("div"); var html = ""; divElement.innerHTML = "
" + html; bodyElements[0].appendChild(divElement); } }; sldAssist.plugin.data_get.prototype.getArrayText = function(data) { var text = "["; var firstI = true; for (var i=0; i"); if (this.options.placeId) { this.getElementById(this.options.placeId).innerHTML = log; } else { var bodyElements = document.getElementsByTagName("body"); var divElement = document.createElement("div"); divElement.innerHTML = "
" + log; bodyElements[0].appendChild(divElement); } } }; /**************************************************************************** * sldAssist.plugin.ccchart extends sldAssist.plugin.AbstractView *****************************************************************************/ sldAssist.plugin.ccchart = function() {}; sldAssist.plugin.ccchart.prototype = new sldAssist.plugin.AbstractView(); sldAssist.plugin.ccchart.prototype.doExecute = function() { var placeId = this.options.placeId; delete this.options.placeId; ccchart.init(placeId, { config: this.options, data:this.currentData }); }; /**************************************************************************** * sldAssist.plugin.gMapChart extends sldAssist.plugin.AbstractView *****************************************************************************/ sldAssist.plugin.gMapChart = function() {}; sldAssist.plugin.gMapChart.prototype = new sldAssist.plugin.AbstractView(); sldAssist.plugin.gMapChart.prototype.doExecute = function() { var place = this.getElementById(this.options.placeId); var circleOptionsArray = this.getArrayFromCases(this.options, "circleOptions"); delete this.options.placeId; var map = new sldAssist.gmap.MapChart(place, this.options); map.draw(this.currentData, circleOptionsArray); }; /************************************************************************************** * sldAssist.plugin.gChart Google Chart Adapter extends sldAssist.plugin.AbstractPlugin ***************************************************************************************/ if ((typeof google !== "undefined") && google.setOnLoadCallback) { sldAssist.plugin.gChartLoaded = false; google.setOnLoadCallback( function() { sldAssist.plugin.gChartLoaded = true; } ); sldAssist.plugin.gChart = function() {}; sldAssist.plugin.gChart.prototype = new sldAssist.plugin.AbstractPlugin; sldAssist.plugin.gChart.prototype.checkExecute = function() { var _this = this; var chartType = this.options.chartType; if ((google.visualization === undefined) || (google.visualization.arrayToDataTable === undefined) || (google.visualization[chartType] === undefined)) { if (sldAssist.plugin.gChartLoaded) { throw "Illegal Google Chart Type: " + chartType; } else { google.setOnLoadCallback(function() { try { _this.execute(); } catch (e) { sldAssist.fu.main.onError(e); } }); return false; } } return sldAssist.plugin.AbstractView.prototype.checkExecute.apply(this); }; sldAssist.plugin.gChart.prototype.doExecute = function() { var chartType = this.options.chartType; var placeId = this.options.placeId; var data1; if (chartType !== "Table") { data1 = google.visualization.arrayToDataTable(this.currentData); } else { var data = this.currentData; var data1 = google.visualization.arrayToDataTable(new Array()); for (var j=0; j= 0) data2[i][j2] = data1[i][j]; } } return data2; }; // data_rows sldAssist.plugin.data_rows = function() {}; sldAssist.plugin.data_rows.prototype = new sldAssist.plugin.AbstractDataHandler(); sldAssist.plugin.data_rows.prototype.modifyData = function(data1) { var type = "select"; var list; if (this.options instanceof Array) { list = this.options; } else { type = this.options.type; list = this.options.list; } var data2 = new Array(); data2[0] = data1[0]; if (type === "remove") { for (var i=1; i 0) && (data1[list[i]] !== undefined)) { data2.push(data1[list[i]]); } } } return data2; }; // data_transpose sldAssist.plugin.data_transpose = function() {}; sldAssist.plugin.data_transpose.prototype = new sldAssist.plugin.AbstractDataHandler(); sldAssist.plugin.data_transpose.prototype.modifyData = function(data1) { var data2 = data1; if (this.options) { var data2 = new Array(); for (var i=0; i= 0) pos1 = resourceId.lastIndexOf("/"); var pos2 = -1; if (this.options.indexOf("#") >= 0) pos2 = resourceId.lastIndexOf("#"); var pos3 = -1; if (this.options.indexOf(":") >= 0) pos3 = resourceId.lastIndexOf(":"); var pos = Math.max(pos1, pos2, pos3); var pos4 = -1; if ((this.options.indexOf("-") >= 0) && (pos >= 0)) pos4 = resourceId.indexOf("-", pos); pos = Math.max(pos, pos4); if (pos < 0) return resourceId; return resourceId.substr(pos + 1, resourceId.length - pos - 1); }; // data_replace sldAssist.plugin.data_replace = function() {}; sldAssist.plugin.data_replace.prototype = new sldAssist.plugin.AbstractDataHandler(); sldAssist.plugin.data_replace.prototype.modifyData = function(data1) { var data2 = new Array(); for (var i=0; i= 0) { data2[i][j] = String(data1[i][j]); } else { data2[i][j] = data1[i][j]; } } } return data2; }; // data_edit sldAssist.plugin.data_edit = function() {}; sldAssist.plugin.data_edit.prototype = new sldAssist.plugin.AbstractDataHandler(); sldAssist.plugin.data_edit.prototype.modifyData = function(data1) { var data2 = this.options(data1); return data2; }; // data_change sldAssist.plugin.data_change = function() {}; sldAssist.plugin.data_change.prototype = new sldAssist.plugin.AbstractDataHandler(); sldAssist.plugin.data_change.prototype.modifyData = function(data1) { var interval = 1; if (typeof this.options.interval !== "undefined") interval = this.options.interval; var rate = true; if (typeof this.options.rate !== "undefined") rate = this.options.rate; var data2 = new Array(); data2[0] = data1[0]; for (var i=1; i<(data1.length - interval); i++) { data2[i] = new Array(); data2[i][0] = data1[i+interval][0]; for (var j=1; j= 0) continue; data3[0].push(data2[0][j]); } // data for (var key in ifile) { if (ifile[key][1] && ifile[key][2]) { for (var k1=0; k1= 0) continue; row.push(data2[ifile[key][2][k2]][j]); } data3.push(row); } } } else { if (! outer) continue; if (ifile[key][1]) { for (var k1=0; k1= 0) continue; row.push(""); } data3.push(row); } } else { for (var k2=0; k2= 0) continue; row.push(""); } for (var j=0; j= 0) continue; row.push(data2[ifile[key][2][k2]][j]); } data3.push(row); } } } } return data3; } var colNos1 = getColNos(data1[0], this.options.colNames1); var colNos2 = getColNos(data2[0], this.options.colNames2); var invertedFile = {}; fillInvertedFile(invertedFile, data1, 1, colNos1); fillInvertedFile(invertedFile, data2, 2, colNos2); return getJoinedData(this.options.outer, invertedFile, data1, data2, colNos1, colNos2); }; /**************************************************************************** * union extends sldAssist.plugin.AbstractConnector *****************************************************************************/ sldAssist.plugin.union = function() {}; sldAssist.plugin.union.prototype = new sldAssist.plugin.AbstractConnector; sldAssist.plugin.union.prototype.modifyData = function(data1) { var data2 = this.options.counterpart.currentData; var data3 = new Array(); data3[0] = sldAssist.utility.unionArrays(data1[0], data2[0]); function addRows(data) { for (var i=1; i ary[i2][prop]) i1 = i2; } } if (i1 === i) continue; var obj = ary[i]; ary[i] = ary[i1]; ary[i1] = obj; } return ary; }, assocShift: function(ob) { for (var key in ob) { if (ob.hasOwnProperty(key)) { var val = ob[key]; delete ob[key]; return {key:key, value:val}; } } }, setOptions: function(options, defaultOptions) { if (typeof defaultOptions === "undefined") return; if ((typeof options !== "object") || (options instanceof Array)) return; for (var k in defaultOptions) { if (typeof options[k] === "undefined") { options[k] = defaultOptions[k]; } } }, indexOfArray: function(ary, value) { for (var i=0; i= 0) continue; if (excludeNull && ((ary1[i]==="") || (ary1[i]===null))) continue; ary2.push(ary1[i]); } return ary2; }, copyObject: function(obj1) { var obj2; if (obj1 instanceof Array) { obj2 = new Array(); for (var i=0; i maxLat) maxLat = data[i][0]; if (data[i][1] < minLng) minLng = data[i][1]; if (data[i][1] > maxLng) maxLng = data[i][1]; } var adjLat = (maxLat - minLat) * adjBounds; maxLat = maxLat + adjLat; minLat = minLat - adjLat; var adjLng = (maxLng - minLng) * adjBounds; maxLng = maxLng + adjLng; minLng = minLng - adjLng; var sw = new google.maps.LatLng(maxLat, minLng); var ne = new google.maps.LatLng(minLat, maxLng); return new google.maps.LatLngBounds(sw, ne); }; /**************************************************************************** * CircleGroup 1列のデータに対応するCircle管理オブジェクト *****************************************************************************/ sldAssist.gmap.CircleGroup = function() {}; sldAssist.gmap.CircleGroup.prototype = { setup: function(map, data, circleOptions, groupNo, groupCount) { this.defaultCircleColors = ["red", "aqua", "lime", "yellow", "purple", "silver"]; this.map = map; this.data = data; this.circleOptions = circleOptions; this.groupNo = groupNo; this.groupCount = groupCount; return this.setDataColNo(); }, setDataColNo: function() { var dataColNo; if (this.circleOptions.dataColName) { dataColNo = sldAssist.utility.indexOfArray(this.data[0], this.circleOptions.dataColName); } else if (this.circleOptions.dataColNo !== undefined) { dataColNo = this.circleOptions.dataColNo - 1; //ユーザのcolNoは1から始まる } if ((dataColNo === undefined) || (dataColNo < 0)) { if (this.data[0].length < (this.groupNo + 3)) { return false; } dataColNo = this.groupNo + 2; } this.dataColNo = dataColNo; return true; }, collectEffectiveRows: function(rowNums) { for (var i=1; i= 0)) { color = this.data[rowNo][colorColNo]; } return color; }, addOptionsFromData: function(rowNo, type, options) { for (var j=0; j (this.groupCount + 2))) { titleColNo = this.data[0].length - 1; } if (this.circleOptions.messageColName) { messageColNo = sldAssist.utility.indexOfArray(this.data[0], this.circleOptions.messageColName); } else if (this.circleOptions.messageColNo !== undefined) { messageColNo = this.circleOptions.messageColNo - 1; } } // contentの決定 var text = "
"; if (this.circleOptions.contentForm) { var form = this.circleOptions.contentForm; for (var j=0; j
"; } if (messageColNo) { text += "" + this.data[rowNo][messageColNo] + "
"; } text += "" + this.data[0][this.dataColNo] + ": " + this.data[rowNo][this.dataColNo] + ""; } text += "
"; return text; }, closeAllInfoWindows: function() { for (var i=0; i