Newer
Older
(function () {
var modelNames = ['schemas', 'schemaValues', 'tables', 'tableValues', 'columns', 'columnValues'];
var models = {};
var addablesModelCompiled;
var searchUCDModelCompiled;
function valuesDirectiveClassFactory(entity, value) {
return function (ctx) {
if (ctx.context) {
var attrValues = ['form-control'];
if (ctx.context[value].isChanged) {
attrValues.push('changed');
}
return attrValues.join(' ');
}
};
}
function getValuesDirective(entity, values) {
var directive = {};
for (var i = 0; i < values.length; i++) {
var value = values[i];
directive['[data-edit="' + entity + '.' + value + '"]@value'] = value + ".value";
directive['[data-edit="' + entity + '.' + value + '"]@class'] = valuesDirectiveClassFactory(entity, value);
function compileModels() {
for (var i = 0; i < modelNames.length; i++) {
var name = modelNames[i];
var directive;
switch (name) {
case 'schemas':
case 'tables':
directive = {
'li.removable-tab': {
'tab<-': {
'.entityName': 'tab.title',
'@class': function (ctx) {
var attrValues = ['removable-tab'];
if (ctx.item.toRemove) {
attrValues.push('strikeout');
}
if (ctx.item.active) {
attrValues.push('active');
}
return attrValues.join(' ');
}
}
}
};
break;
case 'columns':
directive = {
'li': {
'column<-': {
'.entityName': 'column.title',
'@class': function (ctx) {
var attrValues = [];
if (ctx.item.toRemove) {
attrValues.push('strikeout');
}
if (ctx.item.active) {
attrValues.push('active');
}
return attrValues.join(' ');
}
}
break;
case 'schemaValues':
directive = getValuesDirective('schema', ['utype', 'description']);
break;
case 'tableValues':
directive = getValuesDirective('table', ['utype', 'description']);
break;
case 'columnValues':
directive = getValuesDirective('column', ['description', 'ucd', 'unit', 'utype']);
break;
}
models[name] = {
name: name,
value: null,
compiled: $p('#' + name).compile(directive)
};
}
addablesModelCompiled = $p('#addablesModal').compile({
'.addables': {
'entity<-': {
'.addablesName': 'entity'
searchUCDModelCompiled = $p('#searchUCDModalContent').compile({
'#searchUCDDescription@value': 'description',
'#UCDnotFoundAlert@class': function (ctx) {
return ctx.context.UCDnotFound ? '' : 'hide';
},
'#selectedUCD@class': function (ctx) {
return ctx.context.selectedUCD === null ? 'hide' : '';
'#suggestedUCD@class': function (ctx) {
return ctx.context.suggestedUCD === null ? 'hide' : '';
},
'#suggestedUCDBtn': 'suggestedUCD',
'#suggestedUCDs@class': function (ctx) {
return ctx.context.suggestedUCDs.length === 0 ? 'hide' : '';
},
'#selectedUCD span': 'selectedUCD',
'#suggestedUCDs .ucdInfo': {
'ucdInfo<-suggestedUCDs': {
'.resultScore': 'ucdInfo.score',
'.resultFlag': 'ucdInfo.flag',
'.resultFlag@class': function (ctx) {
var classes = ['input-group-addon', 'resultFlag'];
classes.push('flag' + ctx.item.flag);
return classes.join(' ');
},
'.resultWord': 'ucdInfo.word',
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
}
});
}
var entityOf = {
tapSchema: 'schema',
schema: 'table',
table: 'column'
};
function renderPage(result) {
var resultModels = result.elementsToUpdate;
for (var key in resultModels) {
var model = models[key];
model.value = resultModels[key];
$p('#' + model.name).render(model.value, model.compiled);
}
var allStatus = result.status;
for (var container in allStatus) {
var status = allStatus[container];
var entity = entityOf[container];
$('#' + entity + 'Wrapper').toggle(status.hasChildren);
$('#' + entity + 'ToRemoveAlert').toggle(status.selectedToRemove);
$('#' + entity + 'Content').toggle(!status.selectedToRemove);
$('[data-open-addables="' + container + '"]').toggle(status.hasAddables);
}
}
// container target of current opened addables dialog
var addablesDialogTarget = null;
function selectUCD(index) {
var ajaxOptions = {
type: "post",
url: 'webresources/tapschema/selectUCD',
dataType: 'text',
contentType: 'application/x-www-form-urlencoded',
success: function (res) {
$('#selectedUCD').show().find('span').text(res);
}
};
if (typeof index !== 'undefined') {
ajaxOptions['data'] = {index: index};
}
$.ajax(ajaxOptions);
}
// Page init
$(document).ready(function () {
// Here only delegates events are used because all elements are dinamically builded in the page.
$('body').on('keyup', '[data-edit]', function (event) {
$field = $(event.target).closest('[data-edit]');
var values = $field.attr('data-edit').split('.');
$.ajax({
type: "post",
url: 'webresources/tapschema/edit',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
container: values[0],
key: values[1],
value: $field.val()
}),
success: function (res) {
$field.toggleClass('changed', res);
// Select entity
$('body').on('click', 'a[data-selector]', function (event) {
$a = $(event.target).closest('a');
$a.closest('ul').find('li.active').removeClass('active');
$a.closest('li').addClass('active');
$.ajax({
type: "post",
url: 'webresources/tapschema/select',
data: {
containerName: $a.attr('data-selector'),
entityName: $.trim($a.text())
},
dataType: 'json',
success: renderPage
});
});
// Remove entities
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
$('body').on('click', '[data-remover]', function (event) {
event.preventDefault();
event.stopPropagation();
$a = $(event.target).closest('[data-selector]');
var containerName = $a.attr('data-selector');
var entityName = $.trim($a.find('.entityName').text());
$.ajax({
type: "post",
url: 'webresources/tapschema/remove',
data: {
containerName: containerName,
entityName: entityName
},
dataType: 'json',
success: function (result) {
if (containerName === 'table') {
renderPage(result.pageModelUpdates);
if (result.toRemove) { // column state is TO_REMOVE
$a.addClass('strikeout');
} else { // column state is NOT_SELECTED
$a.closest('ul').find('li:first-child').addClass('active')
$a.remove();
}
} else {
renderPage(result);
}
});
return false;
});
$('body').on('click', '[data-undo-remove]', function (event) {
$btn = $(event.target).closest('[data-undo-remove]');
$btn.attr('data-undo-remove');
$.ajax({
type: "post",
url: 'webresources/tapschema/undoremove',
data: {containerName: $btn.attr('data-undo-remove')},
dataType: 'json',
success: renderPage
});
});
// Add entities Modal
$('body').on('click', '[data-open-addables]', function (event) {
var containerName = $(event.target).closest('[data-open-addables]').attr('data-open-addables');
$.ajax({
type: "post",
url: 'webresources/tapschema/addables',
data: {containerName: containerName},
dataType: 'json',
success: function (res) {
$p('#addablesModal').render(res, addablesModelCompiled);
$('#addablesModal').modal('show');
addablesDialogTarget = containerName;
}
});
});
$('#addablesModal').on('hidden.bs.modal', function () {
addablesDialogTarget = null;
});
$('body').on('click', '#addEntitiesBtn', function () {
var selected = [];
$('#addablesModal .addablesName').each(function (index, element) {
if ($(element).prev().is(':checked')) {
selected.push($.trim($(element).text()));
}
});
$.ajax({
type: "post",
url: 'webresources/tapschema/add',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
containerName: addablesDialogTarget,
entities: selected
}),
success: function (result) {
renderPage(result);
$('#addablesModal').modal('hide');
}
});
});
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Open Search UCD Dialog
$('body').on('click', '[data-edit="column.ucd"]', function (event) {
console.log(models.columnValues.value.description.value);
$.ajax({
type: "post",
url: 'webresources/tapschema/searchUCD',
data: {
description: models.columnValues.value.description.value
},
dataType: 'json',
success: function (result) {
$p('#searchUCDModalContent').render(result, searchUCDModelCompiled);
$('#searchUCDModal').modal('show');
}
});
});
// Search UCD
$('body').on('click', '#searchUDCBtn', function () {
$.ajax({
type: "post",
url: 'webresources/tapschema/searchUCD',
data: {description: $.trim($('#searchUCDDescription').val())},
dataType: 'json',
success: function (res) {
$p('#searchUCDModalContent').render(res, searchUCDModelCompiled);
}
});
});
$('body').on('keyup', '#searchUCDDescription', function (event) {
if (event.keyCode === 13) {
$('#searchUDCBtn').click();
}
});
// Select UCD
$('body').on('click', '#suggestedUCDBtn', function () {
selectUCD();
});
$('body').on('click', '.selectUCD', function (event) {
var $selectUCDBtn = $(event.target).closest('.selectUCD');
selectUCD($('#suggestedUCDs .selectUCD').index($selectUCDBtn));
});
// Explain UCD
$('body').on('click', '.explainUCD', function (event) {
var $explainUCDBtn = $(event.target).closest('.explainUCD');
var index = $('#suggestedUCDs .explainUCD').index($explainUCDBtn);
$.ajax({
type: "post",
url: 'webresources/tapschema/explainUCD',
data: {index: index},
dataType: 'text',
success: function (res) {
$explainUCDBtn.closest('.ucdInfo').find('.resultDefinition').removeClass('hide')
.find('.help-block').text(res);
}
});
});
$('#updateBtn').on('click', function () {
$.ajax({
type: "post",
url: 'webresources/tapschema/update',
success: function (res) {
console.log(res);
}
});
});
compileModels();
// Load page model
$.ajax({
type: "post",
url: 'webresources/tapschema/model',
dataType: 'json',
});
});