'use strict'; var $ = require("jquery"), utils = require('./utils.js'), yUtils = require('yasgui-utils'), imgs = require('./imgs.js'); require('jquery-ui/sortable'); require('pivottable'); if (!$.fn.pivotUI) throw new Error("Pivot lib not loaded"); var root = module.exports = function(yasr) { var plugin = {}; var options = $.extend(true, {}, root.defaults); if (options.useD3Chart) { try { var d3 = require('d3'); if (d3) require('../node_modules/pivottable/dist/d3_renderers.js'); } catch (e) { //do nothing. just make sure we don't use this renderer } if ($.pivotUtilities.d3_renderers) $.extend(true, $.pivotUtilities.renderers, $.pivotUtilities.d3_renderers); } var $pivotWrapper; var mergeLabelPostfix = null; var getShownVariables = function() { var variables = yasr.results.getVariables(); if (!options.mergeLabelsWithUris) return variables; var shownVariables = []; mergeLabelPostfix = (typeof options.mergeLabelsWithUris == "string" ? options.mergeLabelsWithUris : "Label"); variables.forEach(function(variable) { if (variable.indexOf(mergeLabelPostfix, variable.length - mergeLabelPostfix.length) !== -1) { //this one ends with a postfix if (variables.indexOf(variable.substring(0, variable.length - mergeLabelPostfix.length)) >= 0) { //we have a shorter version of this variable. So, do not include the .. variable in the table return; } } shownVariables.push(variable); }); return shownVariables; }; var formatForPivot = function(callback) { var vars = getShownVariables(); var usedPrefixes = null; if (yasr.options.getUsedPrefixes) { usedPrefixes = (typeof yasr.options.getUsedPrefixes == "function" ? yasr.options.getUsedPrefixes(yasr) : yasr.options.getUsedPrefixes); } yasr.results.getBindings().forEach(function(binding) { var rowObj = {}; vars.forEach(function(variable) { if (variable in binding) { var val = binding[variable].value; if (mergeLabelPostfix && binding[variable + mergeLabelPostfix]) { val = binding[variable + mergeLabelPostfix].value; } else if (binding[variable].type == "uri") { val = utils.uriToPrefixed(usedPrefixes, val); } rowObj[variable] = val; } else { rowObj[variable] = null; } }); callback(rowObj); }); } var validatePivotTableOptions = function(pivotOptions) { //validate settings. we may have different variables, or renderers might be gone if (pivotOptions) { if (yasr.results) { var vars = yasr.results.getVariables(); var keepColsAndRows = true; pivotOptions.cols.forEach(function(variable) { if (vars.indexOf(variable) < 0) keepColsAndRows = false; }); if (keepColsAndRows) { pivotOptionse.rows.forEach(function(variable) { if (vars.indexOf(variable) < 0) keepColsAndRows = false; }); } if (!keepColsAndRows) { pivotOptions.cols = []; pivotOptions.rows = []; } if (!$.pivotUtilities.renderers[settings.rendererName]) delete pivotOptions.rendererName; } } else { pivotOptions = {}; } return pivotOptions; }; var draw = function() { var doDraw = function() { var onRefresh = function(pivotObj) { options.pivotTable.cols = pivotObj.cols; options.pivotTable.rows = pivotObj.rows; options.pivotTable.rendererName = pivotObj.rendererName; options.pivotTable.aggregatorName = pivotObj.aggregatorName; options.pivotTable.vals = pivotObj.vals; yasr.store(); if (pivotObj.rendererName.toLowerCase().indexOf(' chart') >= 0) { openGchartBtn.show(); } else { openGchartBtn.hide(); } yasr.updateHeader(); }; var openGchartBtn = $('