You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by tv...@apache.org on 2013/03/22 23:54:32 UTC

[05/21] git commit: [#4299] ticket:281 Refactored combobox widget a bit

[#4299] ticket:281 Refactored combobox widget a bit


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/1b10b49f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/1b10b49f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/1b10b49f

Branch: refs/heads/master
Commit: 1b10b49f08a39502cc018c5d7737b5b1f33d0bb3
Parents: 8427b1f
Author: Igor Bondarenko <je...@gmail.com>
Authored: Fri Mar 1 11:16:03 2013 +0000
Committer: Tim Van Steenburgh <tv...@gmail.com>
Committed: Fri Mar 22 21:55:15 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/widgets/form_fields.py           |    6 +-
 Allura/allura/lib/widgets/resources/js/combobox.js |  111 +++++++++++++++
 .../lib/widgets/resources/js/project_user_combo.js |  109 --------------
 3 files changed, 116 insertions(+), 110 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/1b10b49f/Allura/allura/lib/widgets/form_fields.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/form_fields.py b/Allura/allura/lib/widgets/form_fields.py
index 32ae7f6..afd12e5 100644
--- a/Allura/allura/lib/widgets/form_fields.py
+++ b/Allura/allura/lib/widgets/form_fields.py
@@ -106,7 +106,11 @@ class ProjectUserCombo(ew.SingleSelectField):
     def resources(self):
         for r in super(ProjectUserCombo, self).resources():
             yield r
-        yield ew.JSLink('js/project_user_combo.js')
+        yield ew.JSLink('js/combobox.js')
+        yield onready('''
+          $('select.project-user-combobox').combobox({
+            source_url: "%susers"
+          });''' % c.project.url())
 
 
 class NeighborhoodProjectSelect(ew.InputField):

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/1b10b49f/Allura/allura/lib/widgets/resources/js/combobox.js
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/resources/js/combobox.js b/Allura/allura/lib/widgets/resources/js/combobox.js
new file mode 100644
index 0000000..7db3dc8
--- /dev/null
+++ b/Allura/allura/lib/widgets/resources/js/combobox.js
@@ -0,0 +1,111 @@
+(function($) {
+  $.widget('ui.combobox', {
+
+    options: {
+      source_url: ''  // caller must provide this
+    },
+
+    _create: function() {
+      var input,
+          that = this,
+          wasOpen = false,
+          loaded = false,  // options list loaded with ajax already?
+          select = this.element.hide(),
+          selected = select.children(':selected'),
+          value = selected.val() ? selected.text() : "",
+          wrapper = this.wrapper = $('<span>')
+            .addClass('ui-combobox')
+            .insertAfter(select);
+
+      function removeIfInvalid(element) {
+        var value = $(element).val(),
+            matcher = new RegExp('^' + $.ui.autocomplete.escapeRegex(value) + '$'),
+            valid = false;
+        select.children('option').each(function() {
+          if ($(this).text().match(matcher)) {
+            this.selected = valid = true;
+            return false;
+          }
+        });
+
+        if (!valid) {
+          $(element).val('');
+          select.val('');
+          input.data('autocomplete').term = '';
+        }
+      }
+
+      input = $('<input>')
+              .appendTo(wrapper)
+              .val(value)
+              .attr('title', '')
+              .addClass('ui-state-default ui-combobox-input')
+              .autocomplete({
+                delay: 0,
+                minLength: 0,
+                source: function (request, response) {
+                  var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), 'i');
+                  response(select.children('option').map(function() {
+                    var text = $(this).text();
+                    if (this.value && (!request.term || matcher.test(text))) {
+                      return {
+                        label: text.replace(
+                                 new RegExp(
+                                   '(?![^&;]+;)(?!<[^<>]*)(' +
+                                   $.ui.autocomplete.escapeRegex(request.term) +
+                                   ')(?![^<>]*>)(?![^&;]+;)', 'gi'
+                                 ), '<strong>$1</strong>'),
+                        value: text,
+                        option: this
+                      };
+                    }
+                  }));
+                },
+                select: function(event, ui) {
+                  ui.item.option.selected = true;
+                  that._trigger('selected', event, {item: ui.item.option});
+                },
+                change: function(event, ui) {
+                  if (!ui.item) {
+                    removeIfInvalid(this);
+                  }
+                }
+              }).addClass('ui-widget ui-widget-content ui-corner-left');
+
+      input.data('autocomplete')._renderItem = function(ul, item) {
+        return $('<li>')
+          .data('item.autocomplete', item)
+          .append('<a>' + item.label + '</a>')
+          .appendTo(ul);
+      };
+
+      $('<a>')
+        .attr('tabIndex', -1)
+        .attr('title', 'Show all options')
+        .appendTo(wrapper)
+        .button({
+          icons: {
+            primary: 'ui-icon-triangle-1-s'
+          },
+          text: false
+        })
+        .removeClass('ui-corner-all')
+        .addClass('ui-corner-right ui-combobox-toggle')
+        .mousedown(function() {
+          wasOpen = input.autocomplete('widget').is(':visible');
+        })
+        .click(function() {
+          input.focus();
+          if (wasOpen) {
+            return;
+          }
+          input.autocomplete('search', '');
+        });
+    },
+
+    _destroy: function() {
+      this.wrapper.remove();
+      this.element.show();
+    }
+  });
+})(jQuery);

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/1b10b49f/Allura/allura/lib/widgets/resources/js/project_user_combo.js
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/resources/js/project_user_combo.js b/Allura/allura/lib/widgets/resources/js/project_user_combo.js
deleted file mode 100644
index 46b84c6..0000000
--- a/Allura/allura/lib/widgets/resources/js/project_user_combo.js
+++ /dev/null
@@ -1,109 +0,0 @@
-(function($) {
-  $.widget('ui.combobox', {
-    _create: function() {
-      var input,
-          that = this,
-          wasOpen = false,
-          select = this.element.hide(),
-          selected = select.children(':selected'),
-          value = selected.val() ? selected.text() : "",
-          wrapper = this.wrapper = $('<span>')
-            .addClass('ui-combobox')
-            .insertAfter(select);
-
-      function removeIfInvalid(element) {
-        var value = $(element).val(),
-            matcher = new RegExp('^' + $.ui.autocomplete.escapeRegex(value) + '$'),
-            valid = false;
-        select.children('option').each(function() {
-          if ($(this).text().match(matcher)) {
-            this.selected = valid = true;
-            return false;
-          }
-        });
-
-        if (!valid) {
-          $(element).val('');
-          select.val('');
-          input.data('autocomplete').term = '';
-        }
-      }
-
-      input = $('<input>')
-              .appendTo(wrapper)
-              .val(value)
-              .attr('title', '')
-              .addClass('ui-state-default ui-combobox-input')
-              .autocomplete({
-                delay: 0,
-                minLength: 0,
-                source: function (request, response) {
-                  var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), 'i');
-                  response(select.children('option').map(function() {
-                    var text = $(this).text();
-                    if (this.value && (!request.term || matcher.test(text))) {
-                      return {
-                        label: text.replace(
-                                 new RegExp(
-                                   '(?![^&;]+;)(?!<[^<>]*)(' +
-                                   $.ui.autocomplete.escapeRegex(request.term) +
-                                   ')(?![^<>]*>)(?![^&;]+;)', 'gi'
-                                 ), '<strong>$1</strong>'),
-                        value: text,
-                        option: this
-                      };
-                    }
-                  }));
-                },
-                select: function(event, ui) {
-                  ui.item.option.selected = true;
-                  that._trigger('selected', event, {item: ui.item.option});
-                },
-                change: function(event, ui) {
-                  if (!ui.item) {
-                    removeIfInvalid(this);
-                  }
-                }
-              }).addClass('ui-widget ui-widget-content ui-corner-left');
-
-      input.data('autocomplete')._renderItem = function(ul, item) {
-        return $('<li>')
-          .data('item.autocomplete', item)
-          .append('<a>' + item.label + '</a>')
-          .appendTo(ul);
-      };
-
-      $('<a>')
-        .attr('tabIndex', -1)
-        .attr('title', 'Show all users')
-        .appendTo(wrapper)
-        .button({
-          icons: {
-            primary: 'ui-icon-triangle-1-s'
-          },
-          text: false
-        })
-        .removeClass('ui-corner-all')
-        .addClass('ui-corner-right ui-combobox-toggle')
-        .mousedown(function() {
-          wasOpen = input.autocomplete('widget').is(':visible');
-        })
-        .click(function() {
-          input.focus();
-          if (wasOpen) {
-            return;
-          }
-          input.autocomplete('search', '');
-        });
-    },
-
-    _destroy: function() {
-      this.wrapper.remove();
-      this.element.show();
-    }
-  });
-})(jQuery);
-
-$(function() {
-  $('select.project-user-combobox').combobox();
-});