MediaWiki:Gadget-ImprovedUploadForm-FormRestorer.js

MediaWiki系统消息页面
/* global FormRestorer */
'use strict';


/* <nowiki> */
/**
 * SPDX-License-Identifier: CC-BY-SA-4.0
 * _addText: '{{Gadget Header|license=CC-BY-SA-4.0}}'
 *
 * @source <commons.wikimedia.org/wiki/MediaWiki:FormRestorer.js>
 */
/**
 * Save form contents in a cookie, and read them from there again.
 * Author: Lupo, January 2008
 * License: Quadruple licensed GFDL, GPL, LGPL and Creative Commons Attribution 3.0 (CC-BY-3.0; http://creativecommons.org/licenses/by/3.0)
 * Choose whichever license of these you like best :-)
 */
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
(function () {
  window.FormRestorer = {
    getCookie: function getCookie(cookieName) {
      if (!document.cookie || !document.cookie.length || !cookieName || !cookieName.length) {
        return;
      }
      var start = document.cookie.indexOf("".concat(cookieName, "="));
      if (start < 0) {
        return;
      }
      start += cookieName.length + 1;
      var end = document.cookie.indexOf(';', start);
      if (end < 0) {
        return document.cookie.substring(start);
      }
      return document.cookie.substring(start, end);
    },
    saveForm: function saveForm(cookieName, formId, additionalData, cookieAttributes) {
      var form = document.querySelector("#".concat(formId));
      if (!form || !form.elements || !form.elements.length) {
        return;
      }
      var cookieVal = null;
      var formVal = formId;
      if (additionalData) {
        formVal += "/=".concat(additionalData);
        cookieVal = '';
      }
      var _iterator = _createForOfIteratorHelper(form.elements),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var element = _step.value;
          if (element.nodeName.toLowerCase() === 'form') {
            continue;
          } // Don't do nested forms.
          var elementValue = null;
          var singleSelect = false;
          switch (element.type) {
            case 'checkbox':
            case 'radio':
              {
                elementValue = element.checked ? 'T' : 'F';
                break;
              }
            case 'select-one':
              {
                singleSelect = true;
              }
            /* fall through*/
            // eslint-disable-next-line no-fallthrough
            case 'select-multiple':
              {
                for (var j = 0; j < element.length; j++) {
                  if (element[j].selected) {
                    if (elementValue === null) {
                      elementValue = String(j);
                      if (singleSelect) {
                        break;
                      }
                    } else {
                      elementValue = "".concat(elementValue, "/").concat(j);
                    }
                  }
                }
                break;
              }
            // case 'file': // Is read-only anyway, we cannot set it (security policies)
            case 'text':
            case 'password':
            case 'textarea':
              {
                elementValue = element.value;
                if (elementValue === null) {
                  elementValue = '';
                }
                break;
              }
            default:
              {
                // We don't do 'hidden' inputs. We also don't do buttons yet. Would we need to?
                break;
              }
          } // end switch
          if (elementValue !== null) {
            var thisItem = element.id;
            if (!thisItem || !thisItem.length) {
              thisItem = element.name;
            }
            thisItem += "/".concat(element.type, "=").concat(elementValue);
            if (!cookieVal || !cookieVal.length) {
              cookieVal = thisItem;
            } else {
              cookieVal += "\f".concat(thisItem);
            }
          }
        } // end for
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
      if (cookieVal) {
        document.cookie = "".concat(cookieName, "=").concat(encodeURIComponent("".concat(formVal, "\f").concat(cookieVal))).concat(cookieAttributes || '');
      }
    },
    readForm: function readForm(cookieName) {
      try {
        var cookie = FormRestorer.getCookie(cookieName);
        if (cookie.length < 3) {
          return null;
        }
        var pairs = decodeURIComponent(cookie).split('\f');
        if (pairs.length < 2) {
          return null;
        }
        var values = [pairs.length];
        // First one is the form id
        for (var i = 0; i < pairs.length; i++) {
          var j = pairs[i].indexOf('=');
          var k = pairs[i].lastIndexOf('/', j);
          if (j < 0 || k < 0) {
            values[i] = null;
            continue;
          }
          var elementName = pairs[i].substring(0, k);
          var elementType = pairs[i].substring(k + 1, j);
          var val = pairs[i].substring(j + 1);
          var value = null;
          switch (elementType) {
            case 'checkbox':
            case 'radio':
              {
                value = val === 'T';
                break;
              }
            case 'select-one':
              {
                value = Number(val);
                break;
              }
            case 'select-multiple':
              {
                {
                  var numbers = val.split('/');
                  value = [numbers.length];
                  for (j = 0; j < numbers.length; j++) {
                    value[j] = Number(numbers[j]);
                  }
                }
                break;
              }
            default:
              {
                value = val;
                break;
              }
          } // end switch
          values[i] = {
            id: elementName,
            type: elementType,
            val: value
          };
        } // end for
        return values;
      } catch (_unused) {
        return null;
      }
    }
  };
  // end FormRestorer
})();

/* </nowiki> */