From b8a45cfba2ba0dfd524e9e3ba96eb2533054bb51 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 11 Dec 2015 19:26:46 +0100 Subject: [PATCH] feature #4184: Add label filtering and dropdown --- src/sunstone/public/app/sunstone.js | 10 +- src/sunstone/public/app/sunstone/buttons.hbs | 4 + .../app/tabs/templates-tab/datatable.js | 87 ++++++++++++- .../public/app/utils/labels/labels-table.js | 4 +- src/sunstone/public/app/utils/labels/tree.js | 106 +++++++++++++++ src/sunstone/public/app/utils/labels/utils.js | 18 ++- src/sunstone/public/scss/app.scss | 122 +++++++++++++++++- 7 files changed, 336 insertions(+), 15 deletions(-) create mode 100644 src/sunstone/public/app/utils/labels/tree.js diff --git a/src/sunstone/public/app/sunstone.js b/src/sunstone/public/app/sunstone.js index e5b8fd7f59..bb42f44d83 100644 --- a/src/sunstone/public/app/sunstone.js +++ b/src/sunstone/public/app/sunstone.js @@ -454,13 +454,13 @@ define(function(require) { var _setupTabs = function() { var topTabs = $(".left-content ul li.topTab"); - var subTabs = $(".left-content ul li.subTab"); + var subTabs = $(".left-content ul li.subTab > a"); subTabs.on("click", function() { - if ($(this).hasClass('topTab')) { + if ($(this).closest('li').hasClass('topTab')) { return false; } else { - var tabName = $(this).attr('id').substring(3); + var tabName = $(this).closest('li').attr('id').substring(3); _showTab(tabName); return false; } @@ -527,6 +527,7 @@ define(function(require) { //clean selected menu $("#navigation li").removeClass("navigation-active-li"); $("#navigation li#li_" + tabName).addClass("navigation-active-li"); + $('.tree', '#navigation').remove(); var tab = $('#' + tabName); //show tab @@ -534,6 +535,9 @@ define(function(require) { var dataTable = SunstoneCfg['tabs'][tabName]['dataTable']; if (dataTable) { + if (dataTable.clearLabelsFilter) { + dataTable.clearLabelsFilter(); + } dataTable.recountCheckboxes(); } diff --git a/src/sunstone/public/app/sunstone/buttons.hbs b/src/sunstone/public/app/sunstone/buttons.hbs index 825189e4c0..4dd0633f36 100644 --- a/src/sunstone/public/app/sunstone/buttons.hbs +++ b/src/sunstone/public/app/sunstone/buttons.hbs @@ -29,6 +29,10 @@ + +
    +
      diff --git a/src/sunstone/public/app/tabs/templates-tab/datatable.js b/src/sunstone/public/app/tabs/templates-tab/datatable.js index 29cb6a0228..533f0a7971 100644 --- a/src/sunstone/public/app/tabs/templates-tab/datatable.js +++ b/src/sunstone/public/app/tabs/templates-tab/datatable.js @@ -23,6 +23,8 @@ define(function(require) { var SunstoneConfig = require('sunstone-config'); var Locale = require('utils/locale'); var Humanize = require('utils/humanize'); + var LabelsUtils = require('utils/labels/utils'); + var Tree = require('utils/labels/tree'); /* CONSTANTS @@ -60,7 +62,8 @@ define(function(require) { Locale.tr("Owner") , Locale.tr("Group"), Locale.tr("Name"), - Locale.tr("Registration time") + Locale.tr("Registration time"), + Locale.tr("Labels") ]; this.selectOptions = { @@ -72,12 +75,17 @@ define(function(require) { "you_selected_multiple": Locale.tr("You selected the following Templates:") }; + this.labels = []; + TabDataTable.call(this); }; Table.prototype = Object.create(TabDataTable.prototype); Table.prototype.constructor = Table; Table.prototype.elementArray = _elementArray; + Table.prototype.preUpdateView = _preUpdateView; + Table.prototype.postUpdateView = _postUpdateView; + Table.prototype.clearLabelsFilter = _clearLabelsFilter; return Table; @@ -88,6 +96,11 @@ define(function(require) { function _elementArray(element_json) { var element = element_json[XML_ROOT]; + var labelsStr = LabelsUtils.labelsStr(element); + if (labelsStr) { + this.labels.push(labelsStr); + } + return [ '' + + '' + + '' + + ''); + + $('#li_' + TAB_NAME).off('click', '.one-label'); + $('#li_' + TAB_NAME).on('click', '.one-label', function() { + var regExp = []; + var label = $(this).attr('one-label-full-name'); + regExp.push('^' + label + '$'); + regExp.push(',' + label + '$'); + regExp.push('^' + label + ','); + regExp.push(',' + label + ','); + that.dataTable.fnFilter(regExp.join('|'), 6, true, false); + return false; + }); + + + /*$('#li_' + TAB_NAME).off('change', '.labelCheckbox'); + $('#li_' + TAB_NAME).on('change', '.labelCheckbox', function() { + var regExp = []; + var label; + $('.labelCheckbox:checked', '#li_' + TAB_NAME).each(function() { + label = $(this).closest('span').attr('one-label-full-name'); + regExp.push('^' + label + '$'); + regExp.push(',' + label + '$'); + regExp.push('^' + label + ','); + regExp.push(',' + label + ','); + }); + + that.dataTable.fnFilter(regExp.join('|'), 6, true, false); + return false; + });*/ + } + + function _clearLabelsFilter() { + this.dataTable.fnFilter('', 6, true, false); + } }); diff --git a/src/sunstone/public/app/utils/labels/labels-table.js b/src/sunstone/public/app/utils/labels/labels-table.js index 7843c2d50f..d3f1b1904b 100644 --- a/src/sunstone/public/app/utils/labels/labels-table.js +++ b/src/sunstone/public/app/utils/labels/labels-table.js @@ -17,7 +17,7 @@ define(function(require) { /* DEPENDENCIES */ - var Tree = require('utils/tree'); + var Tree = require('./tree'); var TemplateUtils = require('utils/template-utils'); var LabelsUtils = require('./utils'); var TableTemplate = require('hbs!./labels-table/table'); @@ -38,7 +38,7 @@ define(function(require) { this.element = opts.element; this.resource = opts.resource; this.xmlRoot = opts.xmlRoot; - this.labels = LabelsUtils.deserializeLabels(this.element); + this.labels = LabelsUtils.deserializeLabels(LabelsUtils.labelsStr(this.element)); return this; }; diff --git a/src/sunstone/public/app/utils/labels/tree.js b/src/sunstone/public/app/utils/labels/tree.js new file mode 100644 index 0000000000..7af31b5c12 --- /dev/null +++ b/src/sunstone/public/app/utils/labels/tree.js @@ -0,0 +1,106 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2015, OpenNebula Project, OpenNebula Systems */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +define(function(require) { + var Locale = require('utils/locale'); + + /* + CONSTRUCTOR + */ + + return { + 'html': _html, + 'setup': _setup + }; + + /* + FUNCTION DEFINITIONS + */ + + /** + * Returns a
        html tree + * @param {object} tree Recursive structure of elements with attributes + * htmlStr: html to be inserted in this line, + * subTree: array of tree elements. Must exist, can be empty + * @return {string} html string + */ + function _html(tree, collapsed){ + var html; + + if (tree.htmlStr && tree.htmlStr != ""){ + html = '
          '+_innerHtml(tree, collapsed)+'
        '; + } else { + html = '
          '; + $.each(tree.subTree, function(){ + html += _innerHtml(this, collapsed); + }); + html += '
        '; + } + + return html; + } + + function _innerHtml(tree, collapsed){ + var html = '
      • '; + + if (collapsed) { + if (tree.subTree.length > 0) { + html += ' '; + } else { + html += ' '; + } + + html += tree.htmlStr; + html += '