diff -r 000000000000 -r 47700a43ef3f src/pyams_thesaurus/zmi/resources/js/pyams_thesaurus.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pyams_thesaurus/zmi/resources/js/pyams_thesaurus.js Tue Apr 14 17:52:05 2015 +0200 @@ -0,0 +1,195 @@ +(function($) { + + PyAMS_thesaurus = { + + tree: { + + /** + * Display term sub-nodes + */ + displaySubNodes: function(term, nodes, source) { + if (source === undefined) + source = $('span.term:withtext("' + term + '")').siblings('i[data-ams-click-handler]'); + var group = source.parents('span.label').siblings('ul.group'); + var parent = group.closest('ul.group').closest('li'); + group.empty(); + for (var index in nodes) { + var node = nodes[index]; + var li = $('
  • '); + node.extracts.reverse(); + for (var ext_index in node.extracts) { + var extract = node.extracts[ext_index]; + var div = $('
    ').addClass('pull-right margin-right-2') + .appendTo(li); + var square = $('').attr('data-ams-extract-name', extract.name) + .addClass('fa fa-fw fa-square extract-checker') + .css('color', '#' + extract.color) + .appendTo(div); + var parent_checker = $('>div i.extract-checker[data-ams-extract-name="' + extract.name + '"]', parent); + if (parent_checker.hasClass('used')) { + if (extract.used) { + square.addClass('used'); + } else { + square.removeClass('fa-square') + .addClass('fa-square-o'); + } + var switcher = $('i.switcher', 'table.extracts tr[data-ams-element-name="' + extract.name + '"]'); + if (!switcher.hasClass('fa-eye')) { + square.css('visibility', 'hidden'); + } + } else { + square.addClass('disabled') + .css('color', 'silver'); + } + } + var span = $('').addClass('label label-success') + .addClass(node.css_class) + .attr('data-ams-url', node.view) + .attr('data-toggle', 'modal'); + if (node.expand) + $('').addClass('fa fa-lg fa-plus-circle') + .attr('data-ams-click-handler', 'PyAMS_thesaurus.tree.expand') + .attr('data-ams-stop-propagation', true) + .appendTo(span); + $('').addClass('term') + .html(node.label) + .appendTo(span); + span.appendTo(li); + $('').addClass('hidden group') + .appendTo(li); + li.appendTo(group); + if (node.subnodes) + PyAMS_thesaurus.tree.displaySubNodes(node.label, node.subnodes); + } + group.removeClass('hidden'); + source.removeClass('fa-gear') + .addClass('fa-minus-circle'); + }, + + /** + * Expand thesaurus node + */ + expand: function() { + var source = $(this); + if (source.hasClass('fa-plus-circle')) { + source.removeClass('fa-plus-circle') + .addClass('fa-gear'); + var label = source.siblings('span.term').text(); + var context = source.closest('div.tree').data('ams-tree-context'); + MyAMS.ajax.post(context + '/get-nodes.json', {term: label}, function(data) { + PyAMS_thesaurus.tree.displaySubNodes(label, data.nodes, source); + }); + } else { + PyAMS_thesaurus.tree.collapse.call(this); + } + }, + + /** + * Collapse thesaurus node + */ + collapse: function() { + var source = $(this); + source.parents('span.label').siblings('ul.group').addClass('hidden'); + source.removeClass('fa-minus-circle') + .addClass('fa-plus-circle'); + }, + + /** + * Update term label or status + */ + updateTerm: function(options) { + var element = $('span.term:withtext("' + options.term + '")').siblings('i[data-ams-click-handler]'); + if (element.hasClass('fa-minus-circle')) + PyAMS_thesaurus.tree.collapse.call(element); + PyAMS_thesaurus.tree.expand.call(element); + }, + + /** + * Find ans display term in thesaurus tree + */ + findTerm: function(options) { + MyAMS.ajax.post('get-parent-nodes.json', {term: options.term}, function(data) { + PyAMS_thesaurus.tree.displaySubNodes(data.parent, data.nodes); + var element = $('span.term:withtext("' + data.term + '")').parents('span.label'); + $('html,body').animate({scrollTop: element.offset().top - 100}, 1000); + element.css('background-color', 'darkgreen') + .on('mouseover', function() { + $(this).css('background-color', ''); + }) + }); + }, + + /** + * Extract display switcher + * + * This function is called via HREF reference so only returns a function + * which will actually do the job... + */ + switchExtract: function() { + return function() { + var switcher = $('i.fa', $(this)); + var extract = switcher.parents('tr:first').data('ams-element-name'); + if (switcher.hasClass('fa-eye-slash')) + PyAMS_thesaurus.tree.showExtract.call(this, extract, switcher); + else + PyAMS_thesaurus.tree.hideExtract.call(this, extract, switcher); + } + }, + + /** + * Show extract + */ + showExtract: function(extract, switcher) { + $('i.extract-checker[data-ams-extract-name="' + extract + '"]').css('visibility', ''); + switcher.removeClass('fa-eye-slash') + .removeClass('text-danger') + .addClass('fa-eye'); + }, + + /** + * Hide extract + */ + hideExtract: function(extract, switcher) { + $('i.extract-checker[data-ams-extract-name="' + extract + '"]').css('visibility', 'hidden'); + switcher.removeClass('fa-eye') + .addClass('fa-eye-slash') + .addClass('text-danger'); + } + }, + + + /** + * Search terms in tree + */ + search: function() { + var label = $(this).val(); + PyAMS_thesaurus.tree.findTerm({term: label}); + } + }; + + $(document).on('click', 'i.extract-checker', function(e) { + var checker = $(this); + if (checker.hasClass('disabled')) + return; + var term = $('span.term', checker.closest('div').siblings('span')); + MyAMS.ajax.post('switch-extract.json', {term: term.text(), + extract: checker.data('ams-extract-name')}, function(data) { + if (data.used) { + checker.removeClass('fa-square-o') + .addClass('fa-square used'); + $('>li >div i.extract-checker[data-ams-extract-name="' + data.extract + '"]', + checker.closest('div').siblings('ul.group')).removeClass('fa-square disabled') + .addClass('fa-square-o') + .css('color', '#' + data.color); + } else { + checker.removeClass('fa-square used') + .addClass('fa-square-o'); + $('i.extract-checker[data-ams-extract-name="' + data.extract + '"]', + checker.closest('div').siblings('ul.group')).removeClass('fa-square-o') + .addClass('fa-square disabled') + .css('color', 'silver'); + } + }); + }); + +})(jQuery);