Permalink
Browse files

Added new "Data Structure Format" for JSON export type; closes #8

  • Loading branch information...
benkeen committed Mar 31, 2013
1 parent 123cdfb commit b54d9124081f51ca27191b02ec1c762c306f1ccc
View
@@ -1 +1 @@
-<html><head></head><body></body></html>
+<html><head></head><body></body></html>
@@ -12,7 +12,7 @@ class HTML extends ExportTypePlugin {
protected $contentTypeHeader = "text/html";
protected $codeMirrorModes = array("xml");
- function __construct($runtimeContext) {
+ public function __construct($runtimeContext) {
parent::__construct($runtimeContext);
if ($runtimeContext == "generation") {
$this->smarty = new Smarty();
@@ -24,7 +24,7 @@ function __construct($runtimeContext) {
/**
* @see ExportTypePlugin::generate()
*/
- function generate($generator) {
+ public function generate($generator) {
$exportTarget = $generator->getExportTarget();
$postData = $generator->getPostData();
$data = $generator->generateExportData();
@@ -73,13 +73,12 @@ function generate($generator) {
* @param Generator $generator
* @return string
*/
- function getDownloadFilename($generator) {
+ public function getDownloadFilename($generator) {
$time = date("U");
return "randomdata-{$time}.html";
}
-
- function getAdditionalSettingsHTML() {
+ public function getAdditionalSettingsHTML() {
$html =<<< END
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
@@ -129,11 +129,11 @@
};
var _resetSettings = function() {
- $("input[name=etHTMLExportFormat][value=table").attr("checked", "checked");
+ $("input[name=etHTMLExportFormat][value=table]").attr("checked", "checked");
$("#etHTMLUseCustomExportFormat").removeAttr("disabled").trigger("click");
_codeMirror.setValue($("#etHTMLCustomSmarty_Template").html());
};
-
+
var _resetCustomHTML = function() {
_codeMirror.setValue($("#etHTMLCustomSmarty_Template").html());
};
@@ -12,13 +12,66 @@ class JSON extends ExportTypePlugin {
protected $contentTypeHeader = "text/json";
public $L = array();
- function generate($generator) {
+
+ public function generate($generator) {
$exportTarget = $generator->getExportTarget();
$postData = $generator->getPostData();
$data = $generator->generateExportData();
- $stripWhitespace = isset($postData["etJSON_stripWhitespace"]);
+ $stripWhitespace = isset($postData["etJSON_stripWhitespace"]);
+ $dataStructureFormat = isset($postData["etJSON_dataStructureFormat"]) ? $postData["etJSON_dataStructureFormat"] : "complex";
+
+ $content = "";
+ if ($dataStructureFormat == "complex") {
+ $content = $this->generateComplex($generator, $data, $stripWhitespace);
+ } else {
+ $content = $this->generateSimple($generator, $data, $stripWhitespace);
+ }
+
+ return array(
+ "success" => true,
+ "content" => $content
+ );
+ }
+
+ private function generateSimple($generator, $data, $stripWhitespace) {
+ $newline = ($stripWhitespace) ? "" : "\n";
+ $tab = ($stripWhitespace) ? "" : "\t";
+ $space = ($stripWhitespace) ? "" : " ";
+
+ $content = "";
+ if ($generator->isFirstBatch()) {
+ $content .= "[$newline";
+ }
+ $numCols = count($data["colData"]);
+ $numRows = count($data["rowData"]);
+
+ for ($i=0; $i<$numRows; $i++) {
+ $content .= "{$tab}{{$newline}";
+
+ $pairs = array();
+ for ($j=0; $j<$numCols; $j++) {
+ $varName = preg_replace('/"/', '\"', $data["colData"][$j]);
+ $pairs[] = "{$tab}{$tab}\"$varName\":{$space}\"{$data["rowData"][$i][$j]}\"";
+ }
+ $content .= implode(",$newline", $pairs);
+
+ if ($data["isLastBatch"] && $i == $numRows - 1) {
+ $content .= "{$newline}{$tab}}$newline";
+ } else {
+ $content .= "{$newline}{$tab}},$newline";
+ }
+ }
+ if ($generator->isLastBatch()) {
+ $content .= "]";
+ }
+
+ return $content;
+ }
+
+
+ private function generateComplex($generator, $data, $stripWhitespace) {
$content = "";
if ($generator->isFirstBatch()) {
$quotedCols = Utils::enquoteArray($data["colData"]);
@@ -57,11 +110,7 @@ function generate($generator) {
$content .= "\n\t]\n}";
}
}
-
- return array(
- "success" => true,
- "content" => $content
- );
+ return $content;
}
/**
@@ -70,16 +119,20 @@ function generate($generator) {
* @param Generator $generator
* @return string
*/
- function getDownloadFilename($generator) {
+ public function getDownloadFilename($generator) {
$time = date("M-j-Y");
return "data{$time}.json";
}
- function getAdditionalSettingsHTML() {
-
+ public function getAdditionalSettingsHTML() {
$html =<<< END
<input type="checkbox" name="etJSON_stripWhitespace" id="etJSON_stripWhitespace" value="1" />
- <label for="etJSON_stripWhitespace">{$this->L["strip_whitespace"]}</label>
+ <label for="etJSON_stripWhitespace">{$this->L["strip_whitespace"]}</label><br />
+ {$this->L["data_structure_format"]}
+ <input type="radio" name="etJSON_dataStructureFormat" value="complex" id="stJSON_dataStructureFormat1" checked="checked" />
+ <label for="stJSON_dataStructureFormat1">{$this->L["complex"]}</label>
+ <input type="radio" name="etJSON_dataStructureFormat" value="simple" id="stJSON_dataStructureFormat2" />
+ <label for="stJSON_dataStructureFormat2">{$this->L["simple"]}</label>
END;
return $html;
}
@@ -44,24 +44,28 @@ define([
} else {
$("#etJSON_stripWhitespace").removeAttr("checked");
}
+
+ // for backward compatibility with early alpha versions
+ if (settings.hasOwnProperty("dataStructureFormat")) {
+ $("input[name=etJSON_dataStructure][value=" + settings.dataStructureFormat + "]").attr("checked", "checked");
+ }
};
var _saveSettings = function() {
return {
- stripWhitespace: ($("#etJSON_stripWhitespace")[0].checked) ? 1 : 0
+ stripWhitespace: ($("#etJSON_stripWhitespace")[0].checked) ? 1 : 0,
+ dataStructureFormat: $("#etJSON_dataStructureFormat").val()
};
};
var _resetSettings = function() {
$("#etJSON_stripWhitespace").removeAttr("checked");
};
-
manager.registerExportType(MODULE_ID, {
init: _init,
loadSettings: _loadSettings,
saveSettings: _saveSettings,
resetSettings: _resetSettings
});
-
});
@@ -2,3 +2,6 @@
$L = array();
$L["strip_whitespace"] = "Strip whitespace from generated results";
+$L["complex"] = "Complex";
+$L["simple"] = "Simple";
+$L["data_structure_format"] = "Data structure format";
@@ -81,7 +81,8 @@ private function generatePerl($data) {
$pairs = array();
for ($j=0; $j<$numCols; $j++) {
- $pairs[] = "{$data["colData"][$j]} => \"{$data["rowData"][$i][$j]}\"";
+ $varName = preg_replace('/"/', '\"', $data["colData"][$j]);
+ $pairs[] = "\"$varName\" => \"{$data["rowData"][$i][$j]}\"";
}
$content .= implode(",", $pairs);
@@ -47,6 +47,7 @@ define([
var _init = function() {
var subscriptions = {};
subscriptions[C.EVENT.GENERATE] = _onGenerate;
+ subscriptions[C.EVENT.RESULT_TYPE.CHANGE] = _resultTypeChanged;
manager.subscribe(MODULE_ID, subscriptions);
};
@@ -64,6 +65,15 @@ define([
$("#etProgrammingLanguage_language").val("JavaScript");
};
+ /**
+ * Called when the user changes the result type
+ */
+ var _resultTypeChanged = function(msg) {
+ if (msg.newExportType == "ProgrammingLanguage") {
+ $("#gdColTitleTop,#gdColTitleBottom").html(LANG.row_label);
+ }
+ };
+
manager.registerExportType(MODULE_ID, {
init: _init,
loadSettings: _loadSettings,
@@ -1,3 +1,4 @@
<?php
$L = array();
+$L["row_label"] = "Var/Prop Name";
@@ -25,7 +25,6 @@ define([
manager.subscribe(MODULE_ID, subscriptions);
};
-
/**
* Called when the user changes the result type
*/
View
@@ -0,0 +1 @@
+<html><body></body></html>

0 comments on commit b54d912

Please sign in to comment.