[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/legacy/js/ -> ajax-chosen.js (source)

   1  /**
   2   * @copyright   (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
   3   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   4   */
   5  
   6  /**
   7   * ajaxChosen javascript behavior
   8   *
   9   * Used for displaying tags
  10   *
  11   * @package     Joomla.JavaScript
  12   * @since       1.5
  13   * @version     1.0
  14   */
  15  (function($) {
  16    return $.fn.ajaxChosen = function(settings, callback, chosenOptions) {
  17      var chosenXhr, defaultOptions, options, select;
  18      if (settings == null) {
  19        settings = {};
  20      }
  21      if (callback == null) {
  22        callback = {};
  23      }
  24      if (chosenOptions == null) {
  25        chosenOptions = function() {};
  26      }
  27      defaultOptions = {
  28        minTermLength: 3,
  29        afterTypeDelay: 500,
  30        jsonTermKey: "term",
  31        keepTypingMsg: Joomla.Text._('JGLOBAL_KEEP_TYPING'),
  32        lookingForMsg: Joomla.Text._('JGLOBAL_LOOKING_FOR')
  33      };
  34      select = this;
  35      chosenXhr = null;
  36      options = $.extend({}, defaultOptions, $(select).data(), settings);
  37      this.jchosen(chosenOptions ? chosenOptions : {});
  38      return this.each(function() {
  39        return $(this).next('.chosen-container').find(".search-field > input, .chosen-search > input").bind('keyup', function() {
  40          var field, msg, success, untrimmed_val, val;
  41          untrimmed_val = $(this).val();
  42          val = $.trim($(this).val());
  43          msg = val.length < options.minTermLength ? options.keepTypingMsg : options.lookingForMsg + (" '" + val + "'");
  44          select.next('.chosen-container').find('.no-results').text(msg);
  45          if (val === $(this).data('prevVal')) {
  46            return false;
  47          }
  48          $(this).data('prevVal', val);
  49          if (this.timer) {
  50            clearTimeout(this.timer);
  51          }
  52          if (val.length < options.minTermLength) {
  53            return false;
  54          }
  55          field = $(this);
  56          if (!(options.data != null)) {
  57            options.data = {};
  58          }
  59          options.data[options.jsonTermKey] = val;
  60          if (options.dataCallback != null) {
  61            options.data = options.dataCallback(options.data);
  62          }
  63          success = options.success;
  64          options.success = function(data) {
  65            var items, nbItems, selected_values;
  66            if (!(data != null)) {
  67              return;
  68            }
  69            selected_values = [];
  70            select.find('option').each(function() {
  71              if (!$(this).is(":selected")) {
  72                return $(this).remove();
  73              } else {
  74                return selected_values.push($(this).val() + "-" + $(this).text());
  75              }
  76            });
  77            select.find('optgroup:empty').each(function() {
  78              return $(this).remove();
  79            });
  80            items = callback.apply(null, data);
  81            nbItems = 0;
  82            $.each(items, function(i, element) {
  83              var group, text, value;
  84              nbItems++;
  85              if (element.group) {
  86                group = select.find("optgroup[label='" + element.text + "']");
  87                if (!group.size()) {
  88                  group = $("<optgroup />");
  89                }
  90                group.attr('label', element.text).appendTo(select);
  91                return $.each(element.items, function(i, element) {
  92                  var text, value;
  93                  if (typeof element === "string") {
  94                    value = i;
  95                    text = element;
  96                  } else {
  97                    value = element.value;
  98                    text = element.text;
  99                  }
 100                  if ($.inArray(value + "-" + text, selected_values) === -1) {
 101                    return $("<option />").attr('value', value).html(text).appendTo(group);
 102                  }
 103                });
 104              } else {
 105                if (typeof element === "string") {
 106                  value = i;
 107                  text = element;
 108                } else {
 109                  value = element.value;
 110                  text = element.text;
 111                }
 112                if ($.inArray(value + "-" + text, selected_values) === -1) {
 113                  return $("<option />").attr('value', value).html(text).appendTo(select);
 114                }
 115              }
 116            });
 117            if (nbItems) {
 118              select.trigger("chosen:updated");
 119            } else {
 120              select.data().jchosen.no_results_clear();
 121              select.data().jchosen.no_results(field.val());
 122            }
 123            if (success != null) {
 124              success(data);
 125            }
 126            return field.val(untrimmed_val);
 127          };
 128          return this.timer = setTimeout(function() {
 129            if (chosenXhr) {
 130              chosenXhr.abort();
 131            }
 132            return chosenXhr = $.ajax(options);
 133          }, options.afterTypeDelay);
 134        });
 135      });
 136    };
 137  })(jQuery);
 138  
 139  jQuery(document).ready(function ($) {
 140    if (Joomla.getOptions('ajax-chosen')) {
 141  
 142      var options = Joomla.getOptions('ajax-chosen');
 143  
 144      $(options.selector).ajaxChosen({
 145        type: options.type,
 146        url: options.url,
 147        dataType: options.dataType,
 148        jsonTermKey: options.jsonTermKey,
 149        afterTypeDelay: options.afterTypeDelay,
 150        minTermLength: options.minTermLength
 151      }, function (data) {
 152        var results = [];
 153  
 154        $.each(data, function (i, val) {
 155          results.push({ value: val.value, text: val.text });
 156        });
 157  
 158        return results;
 159      });
 160    }
 161  });


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer