2011-08-02 05:34:47 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
2011-08-04 22:37:14 +02:00
|
|
|
* Redistributions of source code must retain the above copyright
|
2011-08-02 05:34:47 +02:00
|
|
|
notice, this list of conditions and the following disclaimer.
|
2011-08-04 22:37:14 +02:00
|
|
|
* Redistributions in binary form must reproduce the above
|
2011-08-02 05:34:47 +02:00
|
|
|
copyright notice, this list of conditions and the following disclaimer
|
|
|
|
in the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
2011-08-04 22:37:14 +02:00
|
|
|
* Neither the name of Google Inc. nor the names of its
|
2011-08-02 05:34:47 +02:00
|
|
|
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.
|
|
|
|
|
2011-08-04 22:37:14 +02:00
|
|
|
*/
|
2011-08-02 05:34:47 +02:00
|
|
|
|
|
|
|
Refine.JsonParserUI = function(controller, jobID, job, format, config,
|
|
|
|
dataContainerElmt, progressContainerElmt, optionContainerElmt) {
|
2011-08-04 22:37:14 +02:00
|
|
|
|
|
|
|
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._showPickRecordNodesUI();
|
2011-08-02 05:34:47 +02:00
|
|
|
};
|
2011-10-08 01:44:11 +02:00
|
|
|
Refine.DefaultImportingController.parserUIs.JsonParserUI = Refine.JsonParserUI;
|
2011-08-02 05:34:47 +02:00
|
|
|
|
|
|
|
Refine.JsonParserUI.prototype.dispose = function() {
|
2011-10-08 01:44:11 +02:00
|
|
|
if (this._timerID !== null) {
|
2011-08-04 22:37:14 +02:00
|
|
|
window.clearTimeout(this._timerID);
|
|
|
|
this._timerID = null;
|
|
|
|
}
|
2011-08-02 05:34:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Refine.JsonParserUI.prototype.confirmReadyToCreateProject = function() {
|
|
|
|
if ((this._config.recordPath) && this._config.recordPath.length > 0) {
|
2011-08-04 22:37:14 +02:00
|
|
|
return true;
|
2011-08-02 05:34:47 +02:00
|
|
|
} else {
|
2013-07-02 13:40:50 +02:00
|
|
|
window.alert($.i18n._('core-index-import')["warning-record-path"]);
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Refine.JsonParserUI.prototype.getOptions = function() {
|
2014-02-12 10:00:17 +01:00
|
|
|
if(!this._config.recordPath){
|
|
|
|
this._setRecordPath(this._config.defaultRecordPath);
|
|
|
|
}
|
2011-08-04 22:37:14 +02:00
|
|
|
var options = {
|
|
|
|
recordPath: this._config.recordPath
|
|
|
|
};
|
|
|
|
|
2011-09-29 07:58:02 +02:00
|
|
|
var parseIntDefault = function(s, def) {
|
|
|
|
try {
|
2011-10-08 01:44:11 +02:00
|
|
|
var n = parseInt(s,10);
|
2011-09-29 07:58:02 +02:00
|
|
|
if (!isNaN(n)) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
};
|
2011-08-04 22:37:14 +02:00
|
|
|
if (this._optionContainerElmts.limitCheckbox[0].checked) {
|
|
|
|
options.limit = parseIntDefault(this._optionContainerElmts.limitInput[0].value, -1);
|
|
|
|
} else {
|
|
|
|
options.limit = -1;
|
|
|
|
}
|
2012-09-08 03:20:25 +02:00
|
|
|
|
|
|
|
options.trimStrings = this._optionContainerElmts.trimStringsCheckbox[0].checked;
|
|
|
|
options.guessCellValueTypes = this._optionContainerElmts.guessCellValueTypesCheckbox[0].checked;
|
|
|
|
options.storeEmptyStrings = this._optionContainerElmts.storeEmptyStringsCheckbox[0].checked;
|
|
|
|
|
2011-08-04 22:37:14 +02:00
|
|
|
options.includeFileSources = this._optionContainerElmts.includeFileSourcesCheckbox[0].checked;
|
|
|
|
|
|
|
|
return options;
|
2011-08-02 05:34:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Refine.JsonParserUI.prototype._initialize = function() {
|
2011-08-04 22:37:14 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this._optionContainer.unbind().empty().html(
|
|
|
|
DOM.loadHTML("core", "scripts/index/parser-interfaces/json-parser-ui.html"));
|
|
|
|
this._optionContainerElmts = DOM.bind(this._optionContainer);
|
|
|
|
this._optionContainerElmts.previewButton.click(function() { self._updatePreview(); });
|
|
|
|
|
2013-07-02 13:40:50 +02:00
|
|
|
this._optionContainerElmts.pickRecordElementsButton.text($.i18n._('core-index-import')["warning-record-path"]);
|
2013-07-12 11:12:33 +02:00
|
|
|
this._optionContainerElmts.previewButton.html($.i18n._('core-buttons')["update-preview"]);
|
2013-07-02 13:40:50 +02:00
|
|
|
$('#or-import-load').text($.i18n._('core-index-parser')["load-at-most"]);
|
2013-07-11 11:05:13 +02:00
|
|
|
$('#or-import-rows').text($.i18n._('core-index-parser')["rows-data"]);
|
2013-07-02 13:40:50 +02:00
|
|
|
$('#or-import-preserve').text($.i18n._('core-index-parser')["preserve-empty"]);
|
|
|
|
$('#or-import-trim').html($.i18n._('core-index-parser')["trim"]);
|
|
|
|
$('#or-import-parseCell').html($.i18n._('core-index-parser')["parse-cell"]);
|
|
|
|
$('#or-import-source').html($.i18n._('core-index-parser')["store-source"]);
|
|
|
|
$('#or-import-jsonParser').text($.i18n._('core-index-parser')["json-parser"]);
|
|
|
|
|
2011-08-04 22:37:14 +02:00
|
|
|
if (this._config.limit > 0) {
|
2013-09-18 21:38:12 +02:00
|
|
|
this._optionContainerElmts.limitCheckbox.prop("checked", true);
|
2011-08-04 22:37:14 +02:00
|
|
|
this._optionContainerElmts.limitInput[0].value = this._config.limit.toString();
|
|
|
|
}
|
2012-09-08 03:20:25 +02:00
|
|
|
if (this._config.trimStrings) {
|
2013-07-27 19:32:26 +02:00
|
|
|
this._optionContainerElmts.trimStringsCheckbox.attr("checked", "unchecked");
|
2012-09-08 03:20:25 +02:00
|
|
|
}
|
|
|
|
if (this._config.guessCellValueTypes) {
|
2013-07-27 19:32:26 +02:00
|
|
|
this._optionContainerElmts.guessCellValueTypesCheckbox.attr("checked", "unchecked");
|
2012-09-08 03:20:25 +02:00
|
|
|
}
|
|
|
|
if (this._config.storeEmptyStrings) {
|
2013-09-18 21:38:12 +02:00
|
|
|
this._optionContainerElmts.storeEmptyStringsCheckbox.prop("checked", true);
|
2012-09-08 03:20:25 +02:00
|
|
|
}
|
2011-08-04 22:37:14 +02:00
|
|
|
if (this._config.includeFileSources) {
|
2013-09-18 21:38:12 +02:00
|
|
|
this._optionContainerElmts.includeFileSourcesCheckbox.prop("checked", true);
|
2011-08-04 22:37:14 +02:00
|
|
|
}
|
|
|
|
this._optionContainerElmts.pickRecordElementsButton.click(function() {
|
|
|
|
self._showPickRecordNodesUI();
|
|
|
|
});
|
|
|
|
|
|
|
|
var onChange = function() {
|
|
|
|
self._scheduleUpdatePreview();
|
|
|
|
};
|
|
|
|
this._optionContainer.find("input").bind("change", onChange);
|
|
|
|
this._optionContainer.find("select").bind("change", onChange);
|
2011-08-02 05:34:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Refine.JsonParserUI.prototype._showPickRecordNodesUI = function() {
|
2012-01-27 17:46:11 +01:00
|
|
|
var ANONYMOUS_NODE_NAME = '_';
|
2011-08-04 22:37:14 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this._dataContainer.unbind().empty().html(
|
|
|
|
DOM.loadHTML("core", "scripts/index/parser-interfaces/json-parser-select-ui.html"));
|
|
|
|
|
|
|
|
var elmts = DOM.bind(this._dataContainer);
|
|
|
|
|
|
|
|
var escapeElmt = $('<span>');
|
|
|
|
var escapeHtml = function(s) {
|
|
|
|
escapeElmt.empty().text(s);
|
|
|
|
return escapeElmt.html();
|
|
|
|
};
|
|
|
|
var textAsHtml = function(s) {
|
|
|
|
s = s.length <= 200 ? s : (s.substring(0, 200) + ' ...');
|
|
|
|
return '<span class="text">' + escapeHtml(s) + '</span>';
|
|
|
|
};
|
|
|
|
var hittest = function(evt, elmt) {
|
|
|
|
var a = $(evt.target).closest('.node');
|
|
|
|
return a.length > 0 && a[0] == elmt[0];
|
|
|
|
};
|
|
|
|
var registerEvents = function(elmt, path) {
|
|
|
|
elmt.bind('mouseover', function(evt) {
|
|
|
|
if (hittest(evt, elmt)) {
|
|
|
|
elmts.domContainer.find('.highlight').removeClass('highlight');
|
|
|
|
elmt.addClass('highlight');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.bind('mouseout', function(evt) {
|
|
|
|
elmt.removeClass('highlight');
|
|
|
|
})
|
|
|
|
.click(function(evt) {
|
|
|
|
if (hittest(evt, elmt)) {
|
|
|
|
self._setRecordPath(path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
var renderArray = function(a, container, parentPath) {
|
|
|
|
$('<span>').addClass('punctuation').text('[').appendTo(container);
|
|
|
|
|
|
|
|
var parentPath2 = [].concat(parentPath);
|
2012-01-27 17:46:11 +01:00
|
|
|
parentPath2.push(ANONYMOUS_NODE_NAME);
|
2011-08-04 22:37:14 +02:00
|
|
|
|
|
|
|
var elementNode = null;
|
|
|
|
for (var i = 0; i < a.length; i++) {
|
2011-10-08 01:44:11 +02:00
|
|
|
if (elementNode !== null) {
|
2011-08-04 22:37:14 +02:00
|
|
|
$('<span>').addClass('punctuation').text(',').appendTo(elementNode);
|
|
|
|
}
|
|
|
|
elementNode = $('<div>').addClass('node').addClass('indented').appendTo(container);
|
|
|
|
|
|
|
|
renderNode(a[i], elementNode, parentPath2);
|
|
|
|
}
|
|
|
|
|
|
|
|
$('<span>').addClass('punctuation').text(']').appendTo(container);
|
|
|
|
};
|
|
|
|
var renderObject = function(o, container, parentPath) {
|
|
|
|
$('<span>').addClass('punctuation').text('{').appendTo(container);
|
|
|
|
|
|
|
|
var elementNode = null;
|
|
|
|
for (var key in o) {
|
|
|
|
if (o.hasOwnProperty(key)) {
|
2011-10-08 01:44:11 +02:00
|
|
|
if (elementNode !== null) {
|
2011-08-04 22:37:14 +02:00
|
|
|
$('<span>').addClass('punctuation').text(',').appendTo(elementNode);
|
2011-08-02 05:34:47 +02:00
|
|
|
}
|
2011-08-04 22:37:14 +02:00
|
|
|
elementNode = $('<div>').addClass('node').addClass('indented').appendTo(container);
|
|
|
|
|
|
|
|
$('<span>').text(key).addClass('field-name').appendTo(elementNode);
|
|
|
|
$('<span>').text(': ').addClass('punctuation').appendTo(elementNode);
|
|
|
|
|
|
|
|
var parentPath2 = [].concat(parentPath);
|
|
|
|
parentPath2.push(key);
|
|
|
|
|
|
|
|
renderNode(o[key], elementNode, parentPath2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$('<span>').addClass('punctuation').text('}').appendTo(container);
|
|
|
|
|
|
|
|
registerEvents(container, parentPath);
|
|
|
|
};
|
|
|
|
var renderNode = function(node, container, parentPath) {
|
2011-10-08 01:44:11 +02:00
|
|
|
if (node === null) {
|
2011-08-04 22:37:14 +02:00
|
|
|
$('<span>').addClass('literal').text('null').appendTo(container);
|
|
|
|
} else {
|
|
|
|
if ($.isPlainObject(node)) {
|
|
|
|
renderObject(node, container, parentPath);
|
|
|
|
} else if ($.isArray(node)) {
|
|
|
|
renderArray(node, container, parentPath);
|
|
|
|
} else {
|
|
|
|
$('<span>').addClass('literal').text(node.toString()).appendTo(container);
|
2011-08-02 05:34:47 +02:00
|
|
|
registerEvents(container, parentPath);
|
2011-08-04 22:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-08-09 16:17:35 +02:00
|
|
|
rootNode = $('<div>').addClass('node').addClass('indented').appendTo(elmts.domContainer);
|
2014-02-12 10:00:17 +01:00
|
|
|
this._config.defaultRecordPath=[ANONYMOUS_NODE_NAME];
|
2012-08-09 16:17:35 +02:00
|
|
|
renderNode(this._config.dom, rootNode, [ANONYMOUS_NODE_NAME]);
|
2011-08-02 05:34:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Refine.JsonParserUI.prototype._scheduleUpdatePreview = function() {
|
2011-10-08 01:44:11 +02:00
|
|
|
if (this._timerID !== null) {
|
2011-08-04 22:37:14 +02:00
|
|
|
window.clearTimeout(this._timerID);
|
|
|
|
this._timerID = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
this._timerID = window.setTimeout(function() {
|
|
|
|
self._timerID = null;
|
|
|
|
self._updatePreview();
|
|
|
|
}, 500); // 0.5 second
|
2011-08-02 05:34:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Refine.JsonParserUI.prototype._setRecordPath = function(path) {
|
2011-08-04 22:37:14 +02:00
|
|
|
this._config.recordPath = path;
|
|
|
|
this._updatePreview();
|
2011-08-02 05:34:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Refine.JsonParserUI.prototype._updatePreview = function() {
|
2011-08-04 22:37:14 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this._progressContainer.show();
|
|
|
|
|
|
|
|
var options = this.getOptions();
|
|
|
|
// for preview, we need exact text, so it's easier to show where the columns are split
|
|
|
|
options.guessCellValueTypes = false;
|
|
|
|
|
|
|
|
this._controller.updateFormatAndOptions(options, function(result) {
|
|
|
|
if (result.status == "ok") {
|
|
|
|
self._controller.getPreviewData(function(projectData) {
|
|
|
|
self._progressContainer.hide();
|
|
|
|
|
|
|
|
new Refine.PreviewTable(projectData, self._dataContainer.unbind().empty());
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
});
|
2011-08-02 05:34:47 +02:00
|
|
|
};
|