'use strict'; var $ = require("jquery"), EventEmitter = require('events').EventEmitter, utils = require("yasgui-utils"); console = console || { "log": function() {} }; //make sure any console statements don't break in IE require('./jquery/extendJquery.js'); /** * Main YASR constructor * * @constructor * @param {DOM-Element} parent element to append editor to. * @param {object} settings * @class YASR * @return {doc} YASR document */ var YASR = function(parent, options, queryResults) { EventEmitter.call(this); var yasr = this; // console.log(EventEmitter.call(this)); // var yasr = {}; // EventEmitter.call(yasr); yasr.options = $.extend(true, {}, module.exports.defaults, options); //the recursive copy does merge (overwrite) array values how we want it to. Do this manually if (options.outputPlugins) yasr.options.outputPlugins = options.outputPlugins; yasr.container = $("
").appendTo(parent); yasr.header = $("
").appendTo(yasr.container); yasr.resultsContainer = $("
").appendTo(yasr.container); yasr.storage = utils.storage; var prefix = null; yasr.getPersistencyId = function(postfix) { if (prefix === null) { //instantiate prefix if (yasr.options.persistency && yasr.options.persistency.prefix) { prefix = (typeof yasr.options.persistency.prefix == 'string' ? yasr.options.persistency.prefix : yasr.options.persistency.prefix(yasr)); } else { prefix = false; } } if (prefix && postfix != null) { return prefix + (typeof postfix == 'string' ? postfix : postfix(yasr)); } else { return null; } }; if (yasr.options.useGoogleCharts) { //pre-load google-loader require('./gChartLoader.js') .once('initError', function() { yasr.options.useGoogleCharts = false }) .init(); } //first initialize plugins yasr.plugins = {}; for (var pluginName in module.exports.plugins) { if (!yasr.options.useGoogleCharts && pluginName == "gchart") continue; yasr.plugins[pluginName] = new module.exports.plugins[pluginName](yasr); } yasr.updateHeader = function() { var downloadIcon = yasr.header.find(".yasr_downloadIcon") .removeAttr("title"); //and remove previous titles var embedButton = yasr.header.find(".yasr_embedBtn"); var outputPlugin = yasr.plugins[yasr.options.output]; if (outputPlugin) { //Manage download link var info = (outputPlugin.getDownloadInfo ? outputPlugin.getDownloadInfo() : null); if (info) { if (info.buttonTitle) downloadIcon.attr('title', info.buttonTitle); downloadIcon.prop("disabled", false); downloadIcon.find("path").each(function() { this.style.fill = "black"; }); } else { downloadIcon.prop("disabled", true).prop("title", "Download not supported for this result representation"); downloadIcon.find("path").each(function() { this.style.fill = "gray"; }); } //Manage embed button var link = null; if (outputPlugin.getEmbedHtml) link = outputPlugin.getEmbedHtml(); if (link && link.length > 0) { embedButton.show(); } else { embedButton.hide(); } } }; yasr.draw = function(output) { if (!yasr.results) return false; if (!output) output = yasr.options.output; //ah, our default output does not take our current results. Try to autodetect var selectedOutput = null; var selectedOutputPriority = -1; var unsupportedOutputs = []; for (var tryOutput in yasr.plugins) { if (yasr.plugins[tryOutput].canHandleResults(yasr)) { var priority = yasr.plugins[tryOutput].getPriority; if (typeof priority == "function") priority = priority(yasr); if (priority != null && priority != undefined && priority > selectedOutputPriority) { selectedOutputPriority = priority; selectedOutput = tryOutput; } } else { unsupportedOutputs.push(tryOutput); } } disableOutputs(unsupportedOutputs); var outputToDraw = null; if (output in yasr.plugins && yasr.plugins[output].canHandleResults(yasr)) { outputToDraw = output; } else if (selectedOutput) { outputToDraw = selectedOutput; } if (outputToDraw) { $(yasr.resultsContainer).empty(); yasr.emit('draw', yasr, yasr.plugins[outputToDraw]); yasr.plugins[outputToDraw].draw(); yasr.emit('drawn', yasr, yasr.plugins[outputToDraw]); yasr.updateHeader(); return true; } else { yasr.updateHeader(); return false; } }; var disableOutputs = function(outputs) { //first enable everything. yasr.header.find('.yasr_btnGroup .yasr_btn').removeClass('disabled'); //now disable the outputs passed as param outputs.forEach(function(outputName) { yasr.header.find('.yasr_btnGroup .select_' + outputName).addClass('disabled'); }); }; yasr.somethingDrawn = function() { return !yasr.resultsContainer.is(":empty"); }; yasr.setResponse = function(dataOrJqXhr, textStatus, jqXhrOrErrorString) { try { yasr.results = require("./parsers/wrapper.js")(dataOrJqXhr, textStatus, jqXhrOrErrorString); } catch (exception) { yasr.results = { getException: function() { return exception } }; } yasr.draw(); //store if needed var resultsId = yasr.getPersistencyId(yasr.options.persistency.results.key); if (resultsId) { if (yasr.results.getOriginalResponseAsString && yasr.results.getOriginalResponseAsString().length < yasr.options.persistency.results.maxSize) { utils.storage.set(resultsId, yasr.results.getAsStoreObject(), "month"); } else { //remove old string utils.storage.remove(resultsId); } } }; var $toggableWarning = null; var $toggableWarningClose = null; var $toggableWarningMsg = null; yasr.warn = function(warning) { if (!$toggableWarning) { //first time instantiation $toggableWarning = $('
', { class: 'toggableWarning' }).prependTo(yasr.container).hide(); $toggableWarningClose = $('', { class: 'toggleWarning' }) .html('×') .click(function() { $toggableWarning.hide(400); }) .appendTo($toggableWarning); $toggableWarningMsg = $('', { class: 'toggableMsg' }).appendTo($toggableWarning); } $toggableWarningMsg.empty(); if (warning instanceof $) { $toggableWarningMsg.append(warning); } else { $toggableWarningMsg.html(warning); } $toggableWarning.show(400); }; var blobDownloadSupported = null; var checkBlobDownloadSupported = function() { if (blobDownloadSupported === null) { var windowUrl = window.URL || window.webkitURL || window.mozURL || window.msURL; blobDownloadSupported = windowUrl && Blob; } return blobDownloadSupported; }; var embedBtn = null; var drawHeader = function(yasr) { var drawOutputSelector = function() { var btnGroup = $('
'); $.each(yasr.options.outputPlugins, function(i, pluginName) { console.log(pluginName); var plugin = yasr.plugins[pluginName]; if (!plugin) return; //plugin not loaded if (plugin.hideFromSelection) return; var name = plugin.name || pluginName; var button = $("") .text(name) .addClass("select_" + pluginName) .click(function() { //update buttons btnGroup.find("button.selected").removeClass("selected"); $(this).addClass("selected"); //set and draw output yasr.options.output = pluginName; //store if needed yasr.store(); //close warning if there is any if ($toggableWarning) $toggableWarning.hide(400); yasr.draw(); }) .appendTo(btnGroup); if (yasr.options.output == pluginName) button.addClass("selected"); }); if (btnGroup.children().length > 1) yasr.header.append(btnGroup); }; var drawDownloadIcon = function() { var stringToUrl = function(string, contentType) { var url = null; var windowUrl = window.URL || window.webkitURL || window.mozURL || window.msURL; if (windowUrl && Blob) { var blob = new Blob([string], { type: contentType }); url = windowUrl.createObjectURL(blob); } return url; }; var button = $("") .append(require("yasgui-utils").svg.getElement(require('./imgs.js').download)) .click(function() { var currentPlugin = yasr.plugins[yasr.options.output]; if (currentPlugin && currentPlugin.getDownloadInfo) { var downloadInfo = currentPlugin.getDownloadInfo(); var downloadUrl = stringToUrl(downloadInfo.getContent(), (downloadInfo.contentType ? downloadInfo.contentType : "text/plain")); var downloadMockLink = $("", { href: downloadUrl, download: downloadInfo.filename }); require('./utils.js').fireClick(downloadMockLink); // downloadMockLink[0].click(); } }); yasr.header.append(button); }; var drawFullscreenButton = function() { var button = $("") .append(require("yasgui-utils").svg.getElement(require('./imgs.js').fullscreen)) .click(function() { yasr.container.addClass('yasr_fullscreen'); }); yasr.header.append(button); }; var drawSmallscreenButton = function() { var button = $("") .append(require("yasgui-utils").svg.getElement(require('./imgs.js').smallscreen)) .click(function() { yasr.container.removeClass('yasr_fullscreen'); }); yasr.header.append(button); }; var drawEmbedButton = function() { embedBtn = $("