var RdfSchemaAlignment = {};
function RdfSchemaAlignmentDialog(schema){
this._originalSchema = schema || { rootNodes: [] };
this._schema = cloneDeep(this._originalSchema); // this is what can be munched on
if (!this._schema.rootNodes.length) {
this._schema.rootNodes.push(RdfSchemaAlignment.createNewRootNode());
}
//this._schema = { rootNodes: [] };
//this._schema.rootNodes.push(RdfSchemaAlignment.createNewRootNode());
this._nodeUIs = [];
this._createDialog();
RdfSchemaAlignment._defaultNamespace = this._schema.baseUri;
this._replaceBaseUri(RdfSchemaAlignment._defaultNamespace || URL.getHostname()+'/');
};
RdfSchemaAlignmentDialog.prototype._createDialog = function() {
var self = this;
var frame = DialogSystem.createDialog();
frame.width("1000px");
var header = $('
').addClass("dialog-header").text("RDF Schema Alignment").appendTo(frame);
var body = $('').addClass("dialog-body").appendTo(frame);
var footer = $('').addClass("dialog-footer").appendTo(frame);
this._constructFooter(footer);
this._constructBody(body);
this._level = DialogSystem.showDialog(frame);
this._renderBody(body);
};
RdfSchemaAlignmentDialog.prototype._constructFooter = function(footer) {
var self = this;
$('').html(" OK ").click(function() {
var schema = self.getJSON();
Gridworks.postProcess(
"rdf-exporter-extension",
"save-rdf-schema",
{},
{ schema: JSON.stringify(schema) },
{},
{
onDone: function() {
DialogSystem.dismissUntil(self._level - 1);
theProject.schema = schema;
}
}
);
}).appendTo(footer);
$('').text("Cancel").click(function() {
DialogSystem.dismissUntil(self._level - 1);
}).appendTo(footer);
};
RdfSchemaAlignmentDialog.prototype._constructBody = function(body) {
var self = this;
$('' +
'RDF Schema Alignment... node types are: cell-as-resource, cell-as-literal, cell-as-blank, literal, resource, and blank' +
'
').appendTo(body);
var html = $(
'Base URI: edit
'+
'' +
'
' +
'
' +
'
' +
'
' +
'
'
).appendTo(body);
var elmts = DOM.bind(html);
this._baseUriSpan = elmts.baseUriSpan;
elmts.baseUriSpan.text(RdfSchemaAlignment._defaultNamespace);
elmts.editBaseUriLink.click(function(evt){
evt.preventDefault();
self._editBaseUri($(evt.target));
});
};
RdfSchemaAlignmentDialog.prototype._previewRdf = function(){
var self = this;
var schema = this.getJSON();
self._previewPane.empty().html('');
$.post(
"/command/rdf-exporter-extension/preview-rdf?" + $.param({ project: theProject.id }),
{ schema: JSON.stringify(schema), engine: JSON.stringify(ui.browsingEngine.getJSON()) },
function(data) {
self._previewPane.empty();
self._previewPane.text(data.v);
},
"json"
);
};
RdfSchemaAlignmentDialog.prototype._renderBody = function(body) {
var self = this;
$("#rdf-schema-alignment-tabs").tabs({
select:function(evt,tabs){
if(tabs.index===1){
self._previewRdf();
}
}
});
$("#rdf-schema-alignment-tabs-preview").css("display", "");
$("#rdf-schema-alignment-tabs-vocabulary-manager").css("display", "");
this._canvas = $(".schema-alignment-dialog-canvas");
this._nodeTable = $('').addClass("schema-alignment-table-layout").appendTo(this._canvas)[0];
for (var i = 0; i < this._schema.rootNodes.length; i++) {
this._nodeUIs.push(new RdfSchemaAlignmentDialog.UINode(
this,
this._schema.rootNodes[i],
this._nodeTable,
{
expanded: true,
mustBeCellTopic: true
}
));
}
this._previewPane = $("#rdf-schema-alignment-dialog-preview");
this._vocabularyManager = $("#rdf-vocabulary-manager");
var vocab_html = $(
'' +
'
' +
''
);
var elmts = DOM.bind(vocab_html);
this._vocabularyManager.html(vocab_html);
this._vocabularyListContainer = elmts.vocabularyList;
this._newVocabularyContainer = elmts.newVocabulary;
this._listVocabularies();
};
//TODO refactor vocabulary manager into its stand-alone Javascript object (~Class)
RdfSchemaAlignmentDialog.prototype._listVocabularies = function(){
var self = this;
self._vocabularyListContainer.empty();
self._renderVocabularyFooter();
$.get("/command/rdf-exporter-extension/list-vocabularies",{},function(o){
var vocab_table = $('').width('100%').addClass('data-table')[0];
var tr = vocab_table.insertRow(vocab_table.rows.length);
$(tr).addClass('');
$(tr).html(' | Prefix | Namespace | ');
$('td',tr).addClass('column-header');
for(var i=0;i').html('').click(getDelFunc).appendTo($(td));
td = tr.insertCell(1);
$(td).text(vocabulary.name );
td = tr.insertCell(2);
$(td).text(vocabulary.uri);
}
$(vocab_table).appendTo(self._vocabularyListContainer);
},"json");
};
RdfSchemaAlignmentDialog.prototype._renderVocabularyFooter = function(){
var self = this;
self._newVocabularyContainer.empty();
var html = $(
'Add New:
' +
''
);
var elmts = DOM.bind(html);
elmts.addvocab.click(function(e){
e.preventDefault();
var prefix = elmts.prefix.val();
var namespace = elmts.namespace.val();
var url = elmts.url.val();
if(!(prefix && namespace && url)){
alert('Please enter all required parameters');
return ;
}
var dismissBusy = DialogSystem.showBusy('Importing vocabulary from: ' + url);
$.get("/command/rdf-exporter-extension/import-vocabulary",{prefix:prefix,namespace:namespace,url:url},function(data){
dismissBusy();
if (data.code === "error"){
alert('Error:' + data.message)
}else{
self._listVocabularies();
}
},"json");
});
html.appendTo(self._newVocabularyContainer);
};
RdfSchemaAlignmentDialog.prototype._deleteVocabulary = function(uri){
var self = this;
var dismissBusy = DialogSystem.showBusy('Deleteing vocabulary: ' + uri);
$.post("/command/rdf-exporter-extension/delete-vocabulary",{uri:uri},function(data){
dismissBusy();
if (data.code === "error"){
alert('Error:' + data.message)
}else{
self._listVocabularies();
}
},"json");
};
RdfSchemaAlignment.createNewRootNode = function() {
var links = [];
var columns = theProject.columnModel.columns;
for (var i = 0; i < columns.length; i++) {
var column = columns[i];
var target = {
nodeType: "cell-as-literal",
columnName: column.name,
columnIndex:i
};
links.push({
uri: null,
curie:null,
target: target
});
}
rootNode = { nodeType: "cell-as-resource", uriExpression:"gel:row.index.urlify(baseURI)", columnIndex:-1, isRowNumberCell:true};
rootNode.links = links;
return rootNode;
};
RdfSchemaAlignmentDialog.prototype._editBaseUri = function(src){
var self = this;
var menu = MenuSystem.createMenu().width('400px');
menu.html(''
);
MenuSystem.showMenu(menu,function(){});
MenuSystem.positionMenuLeftRight(menu, src);
var elmts = DOM.bind(menu);
elmts.newBaseUri.val(RdfSchemaAlignment._defaultNamespace).focus().select();
elmts.applyButton.click(function() {
var newBaseUri = elmts.newBaseUri.val();
if(!newBaseUri || !newBaseUri.substring(7)=='http://'){
alert('Base URI should start with http://');
return;
}
MenuSystem.dismissAll();
self._replaceBaseUri(newBaseUri);
});
elmts.cancelButton.click(function() {
MenuSystem.dismissAll();
});
};
RdfSchemaAlignmentDialog.prototype._replaceBaseUri = function(newBaseUri){
var self = this;
RdfSchemaAlignment._defaultNamespace = newBaseUri;
$.post("/command/rdf-exporter-extension/save-baseURI?" + $.param({project: theProject.id }),{baseURI:newBaseUri},function(data){
if (data.code === "error"){
alert('Error:' + data.message)
}else{
self._baseUriSpan.empty().text(newBaseUri);
}
},"json");
};
RdfSchemaAlignmentDialog.prototype.getJSON = function() {
var rootNodes = [];
for (var i = 0; i < this._nodeUIs.length; i++) {
var node = this._nodeUIs[i].getJSON();
if (node !== null) {
rootNodes.push(node);
}
}
return {
baseUri:RdfSchemaAlignment._defaultNamespace,
rootNodes: rootNodes
};
};