[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/resources/src/jquery/ -> jquery.placeholder.js (source)

   1  /**
   2   * HTML5 placeholder emulation for jQuery plugin
   3   *
   4   * This will automatically use the HTML5 placeholder attribute if supported, or emulate this behavior if not.
   5   *
   6   * This is a fork from Mathias Bynens' jquery.placeholder as of this commit
   7   * https://github.com/mathiasbynens/jquery-placeholder/blob/47f05d400e2dd16b59d144141a2cf54a9a77c502/jquery.placeholder.js
   8   *
   9   * @author Mathias Bynens <http://mathiasbynens.be/>
  10   * @author Trevor Parscal <[email protected]>, 2012
  11   * @author Krinkle <[email protected]>, 2012
  12   * @author Alex Ivanov <[email protected]>, 2013
  13   * @version 2.1.0
  14   * @license MIT
  15   */
  16  (function ($) {
  17  
  18      var isInputSupported = 'placeholder' in document.createElement('input'),
  19          isTextareaSupported = 'placeholder' in document.createElement('textarea'),
  20          prototype = $.fn,
  21          valHooks = $.valHooks,
  22          propHooks = $.propHooks,
  23          hooks,
  24          placeholder;
  25  
  26      if (isInputSupported && isTextareaSupported) {
  27  
  28          placeholder = prototype.placeholder = function (text) {
  29              var hasArgs = arguments.length;
  30  
  31              if (hasArgs) {
  32                  changePlaceholder.call(this, text);
  33              }
  34  
  35              return this;
  36          };
  37  
  38          placeholder.input = placeholder.textarea = true;
  39  
  40      } else {
  41  
  42          placeholder = prototype.placeholder = function (text) {
  43              var $this = this,
  44                  hasArgs = arguments.length;
  45  
  46              if (hasArgs) {
  47                  changePlaceholder.call(this, text);
  48              }
  49  
  50              $this
  51                  .filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
  52                  .filter(function () {
  53                      return !$(this).data('placeholder-enabled');
  54                  })
  55                  .bind({
  56                      'focus.placeholder drop.placeholder': clearPlaceholder,
  57                      'blur.placeholder': setPlaceholder
  58                  })
  59                  .data('placeholder-enabled', true)
  60                  .trigger('blur.placeholder');
  61              return $this;
  62          };
  63  
  64          placeholder.input = isInputSupported;
  65          placeholder.textarea = isTextareaSupported;
  66  
  67          hooks = {
  68              'get': function (element) {
  69                  var $element = $(element),
  70                      $passwordInput = $element.data('placeholder-password');
  71                  if ($passwordInput) {
  72                      return $passwordInput[0].value;
  73                  }
  74  
  75                  return $element.data('placeholder-enabled') && $element.hasClass('placeholder') ? '' : element.value;
  76              },
  77              'set': function (element, value) {
  78                  var $element = $(element),
  79                      $passwordInput = $element.data('placeholder-password');
  80                  if ($passwordInput) {
  81                      $passwordInput[0].value = value;
  82                      return value;
  83                  }
  84  
  85                  if (!$element.data('placeholder-enabled')) {
  86                      element.value = value;
  87                      return value;
  88                  }
  89                  if (!value) {
  90                      element.value = value;
  91                      // Issue #56: Setting the placeholder causes problems if the element continues to have focus.
  92                      if (element !== safeActiveElement()) {
  93                          // We can't use `triggerHandler` here because of dummy text/password inputs :(
  94                          setPlaceholder.call(element);
  95                      }
  96                  } else if ($element.hasClass('placeholder')) {
  97                      if (!clearPlaceholder.call(element, true, value)) {
  98                          element.value = value;
  99                      }
 100                  } else {
 101                      element.value = value;
 102                  }
 103                  // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363
 104                  return $element;
 105              }
 106          };
 107  
 108          if (!isInputSupported) {
 109              valHooks.input = hooks;
 110              propHooks.value = hooks;
 111          }
 112          if (!isTextareaSupported) {
 113              valHooks.textarea = hooks;
 114              propHooks.value = hooks;
 115          }
 116  
 117          $(function () {
 118              // Look for forms
 119              $(document).delegate('form', 'submit.placeholder', function () {
 120                  // Clear the placeholder values so they don't get submitted
 121                  var $inputs = $('.placeholder', this).each(clearPlaceholder);
 122                  setTimeout(function () {
 123                      $inputs.each(setPlaceholder);
 124                  }, 10);
 125              });
 126          });
 127  
 128          // Clear placeholder values upon page reload
 129          $(window).bind('beforeunload.placeholder', function () {
 130              $('.placeholder').each(function () {
 131                  this.value = '';
 132              });
 133          });
 134  
 135      }
 136  
 137  	function args(elem) {
 138          // Return an object of element attributes
 139          var newAttrs = {},
 140              rinlinejQuery = /^jQuery\d+$/;
 141          $.each(elem.attributes, function (i, attr) {
 142              if (attr.specified && !rinlinejQuery.test(attr.name)) {
 143                  newAttrs[attr.name] = attr.value;
 144              }
 145          });
 146          return newAttrs;
 147      }
 148  
 149  	function clearPlaceholder(event, value) {
 150          var input = this,
 151              $input = $(input);
 152          if (input.value === $input.attr('placeholder') && $input.hasClass('placeholder')) {
 153              if ($input.data('placeholder-password')) {
 154                  $input = $input.hide().next().show().attr('id', $input.removeAttr('id').data('placeholder-id'));
 155                  // If `clearPlaceholder` was called from `$.valHooks.input.set`
 156                  if (event === true) {
 157                      $input[0].value = value;
 158                      return value;
 159                  }
 160                  $input.focus();
 161              } else {
 162                  input.value = '';
 163                  $input.removeClass('placeholder');
 164                  if (input === safeActiveElement()) {
 165                      input.select();
 166                  }
 167              }
 168          }
 169      }
 170  
 171  	function setPlaceholder() {
 172          var $replacement,
 173              input = this,
 174              $input = $(input),
 175              id = this.id;
 176          if (!input.value) {
 177              if (input.type === 'password') {
 178                  if (!$input.data('placeholder-textinput')) {
 179                      try {
 180                          $replacement = $input.clone().attr({ 'type': 'text' });
 181                      } catch (e) {
 182                          $replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' }));
 183                      }
 184                      $replacement
 185                          .removeAttr('name')
 186                          .data({
 187                              'placeholder-password': $input,
 188                              'placeholder-id': id
 189                          })
 190                          .bind('focus.placeholder drop.placeholder', clearPlaceholder);
 191                      $input
 192                          .data({
 193                              'placeholder-textinput': $replacement,
 194                              'placeholder-id': id
 195                          })
 196                          .before($replacement);
 197                  }
 198                  $input = $input.removeAttr('id').hide().prev().attr('id', id).show();
 199                  // Note: `$input[0] != input` now!
 200              }
 201              $input.addClass('placeholder');
 202              $input[0].value = $input.attr('placeholder');
 203          } else {
 204              $input.removeClass('placeholder');
 205          }
 206      }
 207  
 208  	function safeActiveElement() {
 209          // Avoid IE9 `document.activeElement` of death
 210          // https://github.com/mathiasbynens/jquery-placeholder/pull/99
 211          try {
 212              return document.activeElement;
 213          } catch (err) {}
 214      }
 215  
 216  	function changePlaceholder(text) {
 217          var hasArgs = arguments.length,
 218              $input = this;
 219          if (hasArgs) {
 220              if ($input.attr('placeholder') !== text) {
 221                  $input.prop('placeholder', text);
 222                  if ($input.hasClass('placeholder')) {
 223                      $input[0].value = text;
 224                  }
 225              }
 226          }
 227      }
 228  
 229  }(jQuery));


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1