230 lines
8.9 KiB
JavaScript
230 lines
8.9 KiB
JavaScript
![]() |
/*
|
||
|
|
||
|
Copyright 2011, Google Inc.
|
||
|
All rights reserved.
|
||
|
|
||
|
Redistribution and use in source and binary forms, with or without
|
||
|
modification, are permitted provided that the following conditions are
|
||
|
met:
|
||
|
|
||
|
* Redistributions of source code must retain the above copyright
|
||
|
notice, this list of conditions and the following disclaimer.
|
||
|
* Redistributions in binary form must reproduce the above
|
||
|
copyright notice, this list of conditions and the following disclaimer
|
||
|
in the documentation and/or other materials provided with the
|
||
|
distribution.
|
||
|
* Neither the name of Google Inc. nor the names of its
|
||
|
contributors may be used to endorse or promote products derived from
|
||
|
this software without specific prior written permission.
|
||
|
|
||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
|
||
|
*/
|
||
|
|
||
|
Refine.SeparatorBasedParserUI = function(controller, jobID, job, format, config,
|
||
|
dataContainerElmt, progressContainerElmt, optionContainerElmt) {
|
||
|
this._controller = controller;
|
||
|
this._jobID = jobID;
|
||
|
this._job = job;
|
||
|
this._format = format;
|
||
|
this._config = config;
|
||
|
|
||
|
this._dataContainer = dataContainerElmt;
|
||
|
this._progressContainer = progressContainerElmt;
|
||
|
this._optionContainer = optionContainerElmt;
|
||
|
|
||
|
this._timerID = null;
|
||
|
this._initialize();
|
||
|
this._updatePreview();
|
||
|
};
|
||
|
Refine.DefaultImportingController.parserUIs["SeparatorBasedParserUI"] = Refine.SeparatorBasedParserUI;
|
||
|
|
||
|
Refine.SeparatorBasedParserUI.encodeSeparator = function(s) {
|
||
|
return s.replace("\\", "\\\\")
|
||
|
.replace("\n", "\\n")
|
||
|
.replace("\t", "\\t");
|
||
|
};
|
||
|
|
||
|
Refine.SeparatorBasedParserUI.decodeSeparator = function(s) {
|
||
|
return s.replace("\\n", "\n")
|
||
|
.replace("\\t", "\t")
|
||
|
.replace("\\\\", "\\");
|
||
|
};
|
||
|
|
||
|
Refine.SeparatorBasedParserUI.prototype.dispose = function() {
|
||
|
if (this._timerID != null) {
|
||
|
window.clearTimeout(this._timerID);
|
||
|
this._timerID = null;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
Refine.SeparatorBasedParserUI.prototype.confirmReadyToCreateProject = function() {
|
||
|
return true; // always ready
|
||
|
};
|
||
|
|
||
|
Refine.SeparatorBasedParserUI.prototype.getOptions = function() {
|
||
|
var options = {};
|
||
|
|
||
|
switch (this._optionContainer.find("input[name='row-separator']:checked")[0].value) {
|
||
|
case 'new-line':
|
||
|
options.lineSeparator = "\n";
|
||
|
break;
|
||
|
default:
|
||
|
options.lineSeparator = Refine.SeparatorBasedParserUI.decodeSeparator(
|
||
|
this._optionContainerElmts.rowSeparatorInput[0].value);
|
||
|
}
|
||
|
switch (this._optionContainer.find("input[name='column-separator']:checked")[0].value) {
|
||
|
case 'comma':
|
||
|
options.separator = ",";
|
||
|
break;
|
||
|
case 'tab':
|
||
|
options.separator = "\t";
|
||
|
break;
|
||
|
default:
|
||
|
options.separator = Refine.SeparatorBasedParserUI.decodeSeparator(
|
||
|
this._optionContainerElmts.columnSeparatorInput[0].value);
|
||
|
}
|
||
|
|
||
|
var parseIntDefault = function(s, def) {
|
||
|
try {
|
||
|
var n = parseInt(s);
|
||
|
if (!isNaN(n)) {
|
||
|
return n;
|
||
|
}
|
||
|
} catch (e) {
|
||
|
// Ignore
|
||
|
}
|
||
|
return def;
|
||
|
};
|
||
|
if (this._optionContainerElmts.ignoreCheckbox[0].checked) {
|
||
|
options.ignoreLines = parseIntDefault(this._optionContainerElmts.ignoreInput[0].value, -1);
|
||
|
} else {
|
||
|
options.ignoreLines = -1;
|
||
|
}
|
||
|
if (this._optionContainerElmts.headerLinesCheckbox[0].checked) {
|
||
|
options.headerLines = parseIntDefault(this._optionContainerElmts.headerLinesInput[0].value, 0);
|
||
|
} else {
|
||
|
options.headerLines = 0;
|
||
|
}
|
||
|
if (this._optionContainerElmts.skipCheckbox[0].checked) {
|
||
|
options.skipDataLines = parseIntDefault(this._optionContainerElmts.skipInput[0].value, 0);
|
||
|
} else {
|
||
|
options.skipDataLines = 0;
|
||
|
}
|
||
|
if (this._optionContainerElmts.limitCheckbox[0].checked) {
|
||
|
options.limit = parseIntDefault(this._optionContainerElmts.limitInput[0].value, -1);
|
||
|
} else {
|
||
|
options.limit = -1;
|
||
|
}
|
||
|
options.storeBlankRows = this._optionContainerElmts.storeBlankRowsCheckbox[0].checked;
|
||
|
|
||
|
options.guessCellValueTypes = this._optionContainerElmts.guessCellValueTypesCheckbox[0].checked;
|
||
|
options.processQuotes = this._optionContainerElmts.processQuoteMarksCheckbox[0].checked;
|
||
|
|
||
|
options.storeBlankCellsAsNulls = this._optionContainerElmts.storeBlankCellsAsNullsCheckbox[0].checked;
|
||
|
options.includeFileSources = this._optionContainerElmts.includeFileSourcesCheckbox[0].checked;
|
||
|
|
||
|
return options;
|
||
|
};
|
||
|
|
||
|
Refine.SeparatorBasedParserUI.prototype._initialize = function() {
|
||
|
var self = this;
|
||
|
|
||
|
this._optionContainer.unbind().empty().html(
|
||
|
DOM.loadHTML("core", "scripts/index/parser-interfaces/separator-based-parser-ui.html"));
|
||
|
this._optionContainerElmts = DOM.bind(this._optionContainer);
|
||
|
this._optionContainerElmts.previewButton.click(function() { self._updatePreview(); });
|
||
|
|
||
|
var rowSeparatorValue = (this._config.lineSeparator == "\n") ? 'new-line' : 'custom';
|
||
|
this._optionContainer.find(
|
||
|
"input[name='row-separator'][value='" + rowSeparatorValue + "']").attr("checked", "checked");
|
||
|
this._optionContainerElmts.rowSeparatorInput[0].value =
|
||
|
Refine.SeparatorBasedParserUI.encodeSeparator(this._config.lineSeparator);
|
||
|
|
||
|
var columnSeparatorValue = (this._config.separator == ",") ? 'comma' :
|
||
|
((this._config.separator == "\t") ? 'tab' : 'custom');
|
||
|
this._optionContainer.find(
|
||
|
"input[name='column-separator'][value='" + columnSeparatorValue + "']").attr("checked", "checked");
|
||
|
this._optionContainerElmts.columnSeparatorInput[0].value =
|
||
|
Refine.SeparatorBasedParserUI.encodeSeparator(this._config.separator);
|
||
|
|
||
|
if (this._config.ignoreLines > 0) {
|
||
|
this._optionContainerElmts.ignoreCheckbox.attr("checked", "checked");
|
||
|
this._optionContainerElmts.ignoreInput[0].value = this._config.ignoreLines.toString();
|
||
|
}
|
||
|
if (this._config.headerLines > 0) {
|
||
|
this._optionContainerElmts.headerLinesCheckbox.attr("checked", "checked");
|
||
|
this._optionContainerElmts.headerLinesInput[0].value = this._config.headerLines.toString();
|
||
|
}
|
||
|
if (this._config.limit > 0) {
|
||
|
this._optionContainerElmts.limitCheckbox.attr("checked", "checked");
|
||
|
this._optionContainerElmts.limitInput[0].value = this._config.limit.toString();
|
||
|
}
|
||
|
if (this._config.skipDataLines > 0) {
|
||
|
this._optionContainerElmts.skipCheckbox.attr("checked", "checked");
|
||
|
this._optionContainerElmts.skipInput.value[0].value = this._config.skipDataLines.toString();
|
||
|
}
|
||
|
if (this._config.storeBlankRows) {
|
||
|
this._optionContainerElmts.storeBlankRowsCheckbox.attr("checked", "checked");
|
||
|
}
|
||
|
|
||
|
if (this._config.guessCellValueTypes) {
|
||
|
this._optionContainerElmts.guessCellValueTypesCheckbox.attr("checked", "checked");
|
||
|
}
|
||
|
if (this._config.processQuotes) {
|
||
|
this._optionContainerElmts.processQuoteMarksCheckbox.attr("checked", "checked");
|
||
|
}
|
||
|
|
||
|
if (this._config.storeBlankCellsAsNulls) {
|
||
|
this._optionContainerElmts.storeBlankCellsAsNullsCheckbox.attr("checked", "checked");
|
||
|
}
|
||
|
if (this._config.includeFileSources) {
|
||
|
this._optionContainerElmts.includeFileSourcesCheckbox.attr("checked", "checked");
|
||
|
}
|
||
|
|
||
|
var onChange = function() {
|
||
|
self._scheduleUpdatePreview();
|
||
|
};
|
||
|
this._optionContainer.find("input").bind("change", onChange);
|
||
|
this._optionContainer.find("select").bind("change", onChange);
|
||
|
};
|
||
|
|
||
|
Refine.SeparatorBasedParserUI.prototype._scheduleUpdatePreview = function() {
|
||
|
if (this._timerID != null) {
|
||
|
window.clearTimeout(this._timerID);
|
||
|
this._timerID = null;
|
||
|
}
|
||
|
|
||
|
var self = this;
|
||
|
this._timerID = window.setTimeout(function() {
|
||
|
self._timerID = null;
|
||
|
self._updatePreview();
|
||
|
}, 500); // 0.5 second
|
||
|
};
|
||
|
|
||
|
Refine.SeparatorBasedParserUI.prototype._updatePreview = function() {
|
||
|
var self = this;
|
||
|
|
||
|
this._progressContainer.show();
|
||
|
|
||
|
this._controller.updateFormatAndOptions(this.getOptions(), function(result) {
|
||
|
if (result.status == "ok") {
|
||
|
self._controller.getPreviewData(function(projectData) {
|
||
|
self._progressContainer.hide();
|
||
|
|
||
|
new Refine.PreviewTable(projectData, self._dataContainer.unbind().empty());
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
};
|