From 46137fdf898690836ae61e54b1cb2711492287f3 Mon Sep 17 00:00:00 2001 From: "Endi S. Dewata" Date: Mon, 16 May 2011 22:23:20 -0500 Subject: jQuery ordered map. The ordered map is a jQuery extension for creating a collection which can be accessed both as an ordered list and as a map. This collection can be used to store various objects including entities, fields, columns, and dialogs. A test suite for this class has been added as well. Ticket #1232 --- install/ui/Makefile.am | 1 + install/ui/add.js | 10 +-- install/ui/associate.js | 69 +++++++++--------- install/ui/details.js | 49 +++++++------ install/ui/dialog.js | 59 +++++++-------- install/ui/dns.js | 5 +- install/ui/entity.js | 100 ++++++++++++-------------- install/ui/hbac.js | 5 +- install/ui/index.html | 1 + install/ui/ipa.js | 22 ++---- install/ui/jquery.ordered-map.js | 68 ++++++++++++++++++ install/ui/jsl.conf | 2 + install/ui/search.js | 5 +- install/ui/sudo.js | 10 +-- install/ui/test/aci_tests.html | 1 + install/ui/test/all_tests.html | 2 + install/ui/test/association_tests.html | 1 + install/ui/test/certificate_tests.html | 1 + install/ui/test/details_tests.html | 1 + install/ui/test/details_tests.js | 4 +- install/ui/test/entity_tests.html | 1 + install/ui/test/index.html | 1 + install/ui/test/ipa_tests.html | 1 + install/ui/test/navigation_tests.html | 1 + install/ui/test/ordered_map_tests.html | 21 ++++++ install/ui/test/ordered_map_tests.js | 128 +++++++++++++++++++++++++++++++++ install/ui/test/widget_tests.html | 1 + install/ui/widget.js | 47 ++++++------ 28 files changed, 427 insertions(+), 190 deletions(-) create mode 100755 install/ui/jquery.ordered-map.js create mode 100755 install/ui/test/ordered_map_tests.html create mode 100755 install/ui/test/ordered_map_tests.js (limited to 'install/ui') diff --git a/install/ui/Makefile.am b/install/ui/Makefile.am index b7567749d..5f77b0698 100644 --- a/install/ui/Makefile.am +++ b/install/ui/Makefile.am @@ -25,6 +25,7 @@ app_DATA = \ jquery-ui.js \ jquery.ba-bbq.js \ jquery-ui.css \ + jquery.ordered-map.js \ certificate.js \ entitle.js \ group.js \ diff --git a/install/ui/add.js b/install/ui/add.js index 7a7bd086b..fcd320edf 100644 --- a/install/ui/add.js +++ b/install/ui/add.js @@ -102,8 +102,9 @@ IPA.add_dialog = function (spec) { var field, value; - for (var i=0; i', { 'name': field.name }).appendTo(container); @@ -122,8 +122,8 @@ IPA.details_section = function(spec) { if (that.template) return; - var fields = that.fields; - for (var i = 0; i < fields.length; ++i) { + var fields = that.fields.values; + for (var i=0; i').appendTo(that.container); - for (var i=0; i').appendTo(table); @@ -172,8 +174,9 @@ IPA.dialog = function(spec) { * Setup behavior */ that.setup = function() { - for (var i=0; i + diff --git a/install/ui/ipa.js b/install/ui/ipa.js index c21c16ec6..f2ffcf1d5 100644 --- a/install/ui/ipa.js +++ b/install/ui/ipa.js @@ -1,3 +1,4 @@ +/*jsl:import jquery.ordered-map.js */ /* Authors: * Pavel Zuna * Adam Young @@ -21,6 +22,7 @@ */ +/* REQUIRES: jquery.ordered-map.js */ /*global $:true, location:true */ var IPA = ( function () { @@ -47,11 +49,9 @@ var IPA = ( function () { that.messages = {}; that.whoami = {}; - that.entities = []; + that.entities = $.ordered_map(); that.entity_factories = {}; - that.entities_by_name = {}; - that.layout = $.bbq.getState('layout'); that.layouts_dir = 'layouts'; @@ -127,27 +127,19 @@ var IPA = ( function () { }; that.get_entities = function() { - return that.entities; + return that.entities.values; }; that.get_entity = function(name) { - return that.entities_by_name[name]; + return that.entities.get(name); }; that.add_entity = function(entity) { - that.entities.push(entity); - that.entities_by_name[entity.name] = entity; + that.entities.put(entity.name, entity); }; that.remove_entity = function(name) { - for (var i=0; i + * + * Copyright (C) 2010 Red Hat + * see file 'COPYING' for use and warranty information + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +jQuery.ordered_map = jQuery.fn.ordered_map = function() { + + var that = {}; + + /** + * These variables can be read directly but should not be + * modified directly. Use the provided methods instead. + */ + that.keys = []; + that.values = []; + that.map = {}; + + that.__defineGetter__('length', function() { + return that.keys.length; + }); + + that.get = function(key) { + return that.map[key]; + }; + + that.put = function(key, value) { + that.keys.push(key); + that.values.push(value); + that.map[key] = value; + }; + + that.remove = function(key) { + + var i = that.keys.indexOf(key); + if (i<0) return null; + + that.keys.splice(i, 1); + that.values.splice(i, 1); + + var value = that.map[key]; + delete that.map[key]; + + return value; + }; + + that.empty = function() { + that.keys = []; + that.values = []; + that.map = {}; + }; + + return that; +}; diff --git a/install/ui/jsl.conf b/install/ui/jsl.conf index ed4c892f3..e7d09ec0c 100644 --- a/install/ui/jsl.conf +++ b/install/ui/jsl.conf @@ -117,6 +117,7 @@ +define alert +define $ +define JSON ++define jQuery ### Files # Specify which files to lint @@ -124,6 +125,7 @@ # To add a set of files, use "+process FileName", "+process Folder\Path\*.js", # or "+process Folder\Path\*.htm". # ++process jquery.ordered-map.js +process ipa.js +process widget.js +process dialog.js diff --git a/install/ui/search.js b/install/ui/search.js index dad11f1a9..1e2186620 100644 --- a/install/ui/search.js +++ b/install/ui/search.js @@ -67,8 +67,9 @@ IPA.search_facet = function(spec) { search_all: that.search_all }); - for (var i=0; i + diff --git a/install/ui/test/all_tests.html b/install/ui/test/all_tests.html index 1d2c7b300..be0bf3ea1 100644 --- a/install/ui/test/all_tests.html +++ b/install/ui/test/all_tests.html @@ -7,6 +7,7 @@ + @@ -18,6 +19,7 @@ + diff --git a/install/ui/test/association_tests.html b/install/ui/test/association_tests.html index 383c9b0b6..e9e3650ba 100644 --- a/install/ui/test/association_tests.html +++ b/install/ui/test/association_tests.html @@ -7,6 +7,7 @@ + diff --git a/install/ui/test/certificate_tests.html b/install/ui/test/certificate_tests.html index 5b411ff13..dd8ccb6e3 100755 --- a/install/ui/test/certificate_tests.html +++ b/install/ui/test/certificate_tests.html @@ -7,6 +7,7 @@ + diff --git a/install/ui/test/details_tests.html b/install/ui/test/details_tests.html index ee3679b9c..e83e70e24 100644 --- a/install/ui/test/details_tests.html +++ b/install/ui/test/details_tests.html @@ -7,6 +7,7 @@ + diff --git a/install/ui/test/details_tests.js b/install/ui/test/details_tests.js index 2715b4eac..8f98f1fef 100644 --- a/install/ui/test/details_tests.js +++ b/install/ui/test/details_tests.js @@ -62,7 +62,7 @@ test("Testing IPA.details_section.create().", function() { section.entity_name = 'user'; section.init(); - var fields = section.fields; + var fields = section.fields.values; var container = $("
"); section.create(container); @@ -264,7 +264,7 @@ test("Testing IPA.details_section_setup again()",function(){ text({name:'cn', label:'Entity Name'}). text({name:'description', label:'Description'}). text({name:'number', label:'Entity ID'}); - var fields = section.fields; + var fields = section.fields.values; var container = $("
"); var details = $("
"); container.append(details); diff --git a/install/ui/test/entity_tests.html b/install/ui/test/entity_tests.html index e512d4380..8b41f8fe9 100644 --- a/install/ui/test/entity_tests.html +++ b/install/ui/test/entity_tests.html @@ -6,6 +6,7 @@ + diff --git a/install/ui/test/index.html b/install/ui/test/index.html index 948cce000..3552cc77e 100644 --- a/install/ui/test/index.html +++ b/install/ui/test/index.html @@ -24,6 +24,7 @@
Complete Test Suite
    +
  • Ordered Map Test Suite
  • Core Test Suite
  • Entity Test Suite
  • Details Test Suite diff --git a/install/ui/test/ipa_tests.html b/install/ui/test/ipa_tests.html index 903e71545..8f8ab93ef 100644 --- a/install/ui/test/ipa_tests.html +++ b/install/ui/test/ipa_tests.html @@ -9,6 +9,7 @@ + diff --git a/install/ui/test/navigation_tests.html b/install/ui/test/navigation_tests.html index 16d095aca..a1a68f7f6 100644 --- a/install/ui/test/navigation_tests.html +++ b/install/ui/test/navigation_tests.html @@ -7,6 +7,7 @@ + diff --git a/install/ui/test/ordered_map_tests.html b/install/ui/test/ordered_map_tests.html new file mode 100755 index 000000000..dda669348 --- /dev/null +++ b/install/ui/test/ordered_map_tests.html @@ -0,0 +1,21 @@ + + + + Ordered Map Test Suite + + + + + + + + + +

    Ordered Map Test Suite

    +

    +
    +

    +
      +
      + + diff --git a/install/ui/test/ordered_map_tests.js b/install/ui/test/ordered_map_tests.js new file mode 100755 index 000000000..b708a1283 --- /dev/null +++ b/install/ui/test/ordered_map_tests.js @@ -0,0 +1,128 @@ +/* Authors: + * Endi Sukma Dewata + * + * Copyright (C) 2010 Red Hat + * see file 'COPYING' for use and warranty information + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +module('ordered_map'); + +test("Testing $.ordered_map constructor.", function() { + + var test = $.ordered_map(); + + strictEqual(test.length, 0, "Checking length."); + deepEqual(test.keys, [], "Checking keys."); + deepEqual(test.values, [], "Checking values."); + deepEqual(test.map, {}, "Checking map."); +}); + +test("Testing $.ordered_map.put().", function() { + + var test = $.ordered_map(); + + var key1 = 'key1'; + var value1 = 'value1'; + + var key2 = 'key2'; + var value2 = 'value2'; + + var map = {}; + map[key1] = value1; + map[key2] = value2; + + test.put(key1, value1); + test.put(key2, value2); + + strictEqual(test.length, 2, 'Checking length.'); + deepEqual(test.keys, [key1, key2], 'Checking keys.'); + deepEqual(test.values, [value1, value2], 'Checking values.'); + deepEqual(test.map, map, 'Checking map.'); +}); + +test("Testing $.ordered_map.get().", function() { + + var test = $.ordered_map(); + + var key1 = 'key1'; + var value1 = 'value1'; + + var key2 = 'key2'; + var value2 = 'value2'; + + var map = {}; + map[key1] = value1; + map[key2] = value2; + + test.put(key1, value1); + test.put(key2, value2); + + var result1 = test.get(key1); + var result2 = test.get(key2); + + strictEqual(test.length, 2, 'Checking length.'); + deepEqual(test.keys, [key1, key2], 'Checking keys.'); + deepEqual(test.values, [value1, value2], 'Checking values.'); + deepEqual(test.map, map, 'Checking map.'); + strictEqual(result1, value1, 'Checking result 1.'); + strictEqual(result2, value2, 'Checking result 2.'); +}); + +test("Testing $.ordered_map.remove().", function() { + + var test = $.ordered_map(); + + var key1 = 'key1'; + var value1 = 'value1'; + + var key2 = 'key2'; + var value2 = 'value2'; + + var map = {}; + map[key2] = value2; + + test.put(key1, value1); + test.put(key2, value2); + + var result1 = test.remove(key1); + + strictEqual(test.length, 1, 'Checking length.'); + deepEqual(test.keys, [key2], 'Checking keys.'); + deepEqual(test.values, [value2], 'Checking values.'); + deepEqual(test.map, map, 'Checking map.'); + strictEqual(result1, value1, 'Checking result.'); +}); + +test("Testing $.ordered_map.empty().", function() { + + var test = $.ordered_map(); + + var key1 = 'key1'; + var value1 = 'value1'; + + var key2 = 'key2'; + var value2 = 'value2'; + + test.put(key1, value1); + test.put(key2, value2); + + test.empty(); + + strictEqual(test.length, 0, 'Checking length.'); + deepEqual(test.keys, [], 'Checking keys.'); + deepEqual(test.values, [], 'Checking values.'); + deepEqual(test.map, {}, 'Checking map.'); +}); diff --git a/install/ui/test/widget_tests.html b/install/ui/test/widget_tests.html index e38b44216..e504f6133 100755 --- a/install/ui/test/widget_tests.html +++ b/install/ui/test/widget_tests.html @@ -10,6 +10,7 @@ + diff --git a/install/ui/widget.js b/install/ui/widget.js index dd2288886..85980acee 100644 --- a/install/ui/widget.js +++ b/install/ui/widget.js @@ -1078,20 +1078,18 @@ IPA.table_widget = function (spec) { that.scrollable = spec.scrollable; that.save_values = typeof spec.save_values == 'undefined' ? true : spec.save_values; - that.columns = []; - that.columns_by_name = {}; + that.columns = $.ordered_map(); that.get_columns = function() { - return that.columns; + return that.columns.values; }; that.get_column = function(name) { - return that.columns_by_name[name]; + return that.columns.get(name); }; that.add_column = function(column) { - that.columns.push(column); - that.columns_by_name[column.name] = column; + that.columns.put(column.name, column); }; that.set_columns = function(columns) { @@ -1102,8 +1100,7 @@ IPA.table_widget = function (spec) { }; that.clear_columns = function() { - that.columns = []; - that.columns_by_name = {}; + that.columns.empty(); }; that.create_column = function(spec) { @@ -1115,8 +1112,9 @@ IPA.table_widget = function (spec) { that.init = function() { that.widget_init(); - for (var i=0; i').appendTo(tr); - if (that.scrollable && (i == that.columns.length-1)) { + if (that.scrollable && (i == columns.length-1)) { if (column.width) { var width = parseInt(column.width.substring(0, column.width.length-2),10); width += 16; @@ -1168,7 +1167,7 @@ IPA.table_widget = function (spec) { 'html': label }).appendTo(th); - if (i == that.columns.length-1) { + if (i == columns.length-1) { $('', { 'name': 'buttons', 'style': 'float: right;' @@ -1194,8 +1193,8 @@ IPA.table_widget = function (spec) { 'value': 'user' }).appendTo(td); - for (/* var */ i=0; i').appendTo(tr); if (column.width) { @@ -1211,7 +1210,7 @@ IPA.table_widget = function (spec) { tr = $('').appendTo(tfoot); - td = $('', { colspan: that.columns.length+1 }).appendTo(tr); + td = $('', { colspan: columns.length+1 }).appendTo(tr); $('', { 'name': 'summary' @@ -1295,17 +1294,22 @@ IPA.table_widget = function (spec) { }; that.get_record = function(result, index) { + var record = {}; - for (var i=0; i