From 114f11fe5a1229cf50d95b68e973e3f2dc2c27fb Mon Sep 17 00:00:00 2001 From: Petr Vobornik Date: Fri, 5 Jun 2015 15:55:11 +0200 Subject: webui: ListViewWidget A widget for rendering a list of groups of items. Intended to be used in sidebar. Plan is to serve also as a base for FacetGroupsWidget. https://fedorahosted.org/freeipa/ticket/3129 Reviewed-By: Martin Kosek Reviewed-By: Tomas Babej --- install/ui/src/freeipa/widgets/ListViewWidget.js | 233 +++++++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 install/ui/src/freeipa/widgets/ListViewWidget.js diff --git a/install/ui/src/freeipa/widgets/ListViewWidget.js b/install/ui/src/freeipa/widgets/ListViewWidget.js new file mode 100644 index 000000000..d378e2b1e --- /dev/null +++ b/install/ui/src/freeipa/widgets/ListViewWidget.js @@ -0,0 +1,233 @@ +// +// Copyright (C) 2015 FreeIPA Contributors see COPYING for license +// + +define([ + 'dojo/_base/declare', + 'dojo/_base/lang', + 'dojo/on', + 'dojo/Evented', + 'dojo/Stateful', + '../jquery', + '../ipa', + '../reg', + '../text', + '../util' +], function(declare, lang, on, Evented, Stateful, $, IPA, reg, text, util) { + +var widgets = {}; + +/** + * Widget for rendering a list of groups of items + * @class + */ +widgets.ListViewWidget = declare([Stateful, Evented], { + + /** + * List of groups + * @type {Object[]} + */ + groups: null, + + /** + * Should be visible + * @type {Boolean} + */ + visible: true, + + /** + * Selected item + * @property {Object} + */ + selected_item: null, + + // behavior + select_on_click: true, + + /** + * Raised when menu item is clicked + * @event item-click + */ + + // nodes + el: null, + group_els: null, + item_els: null, + + // html and styling + css_class: '', + group_el_type: '
', + group_class: '', + group_label_el_type: '
', + group_label_class: 'nav-category', + group_label_title_el_type: '

', + group_label_title_class: '', + item_cont_el_type: '
', + item_cont_class: '', + item_list_el_type: '
    ', + item_list_class: 'nav nav-pills nav-stacked', + item_el_type: '
  • ', + item_class: 't', + item_link_class: 'item-link', + selected_class: 'active', + + _groupsSetter: function(value) { + this.groups = value; + this.render_groups(); + }, + + _get_group_items: function(group) { + return group.items; + }, + + render: function() { + + this.el = $('
    ', { 'class': this.css_class }); + this.render_groups(); + return this.el; + }, + + render_groups: function() { + if (!this.el) return; + + this.group_els = {}; + this.item_els = {}; + + this.el.empty(); + if (!this.groups) return; + + for (var i=0; i', { + text: item.label || item.name || '', + 'class': 'item-link', + href: this.create_item_link(item), + name: item.name, + title: item.title + }).appendTo(el); + + return el; + }, + + create_item_link: function(item) { + return '#'; + }, + + on_click: function(item) { + this.emit('item-click', { source: this, context: item }); + if (this.select_on_click) { + this.select(item); + } + }, + + update_group: function(group) { + if (!this.group_els[group.name]) return; + var label_el = this.group_els[group.name].label_title_el; + label_el.text(group.label); + if (group.title) label_el.attr('title', group.title); + }, + + update_item: function(item) { + var item_el = this.item_els[item.name]; + var label_el = $('a', item_el); + label_el.text(item.label); + if (item.title) label_el.attr('title', item.title); + }, + + select: function(item) { + if (!this.el) return; + var cls = this.selected_class; + var item_el = this.item_els[item.name]; + + this.el.find(this.item_class).removeClass(cls); + this.el.find(this.item_link_class).removeClass(cls); + + if (!item_el) return; // return if can't select + + item_el.addClass(cls); + item_el.find(this.item_link_class).addClass(cls); + this.set('selected_item', item); + this.emit('select', { source: this, context: item }); + }, + + select_first: function() { + if (!this.el) return; + this.el.find(this.item_link_class).removeClass(this.selected_class); + this.el.find(this.item_class).removeClass(this.selected_class); + var first = this.el.find(this.item_link_class + ':first'); + first.addClass(this.selected_class); + first.parent().addClass(this.selected_class); + }, + + set_visible: function(visible) { + this.set('visible', visible); + this._apply_visible(); + }, + + _apply_visible: function() { + if (!this.el) return; + this.el.css('display', this.visible ? '' : 'none'); + }, + + constructor: function(spec) { + lang.mixin(this, spec); + } +}); + + return widgets.ListViewWidget; +}); \ No newline at end of file -- cgit