GPQAPP

Форк
0
/
bs-custom-file-input.js 
167 строк · 4.9 Кб
1
/*!
2
 * bsCustomFileInput v1.3.4 (https://github.com/Johann-S/bs-custom-file-input)
3
 * Copyright 2018 - 2020 Johann-S <johann.servoire@gmail.com>
4
 * Licensed under MIT (https://github.com/Johann-S/bs-custom-file-input/blob/master/LICENSE)
5
 */
6
(function (global, factory) {
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
8
  typeof define === 'function' && define.amd ? define(factory) :
9
  (global = global || self, global.bsCustomFileInput = factory());
10
}(this, (function () { 'use strict';
11

12
  var Selector = {
13
    CUSTOMFILE: '.custom-file input[type="file"]',
14
    CUSTOMFILELABEL: '.custom-file-label',
15
    FORM: 'form',
16
    INPUT: 'input'
17
  };
18

19
  var textNodeType = 3;
20

21
  var getDefaultText = function getDefaultText(input) {
22
    var defaultText = '';
23
    var label = input.parentNode.querySelector(Selector.CUSTOMFILELABEL);
24

25
    if (label) {
26
      defaultText = label.textContent;
27
    }
28

29
    return defaultText;
30
  };
31

32
  var findFirstChildNode = function findFirstChildNode(element) {
33
    if (element.childNodes.length > 0) {
34
      var childNodes = [].slice.call(element.childNodes);
35

36
      for (var i = 0; i < childNodes.length; i++) {
37
        var node = childNodes[i];
38

39
        if (node.nodeType !== textNodeType) {
40
          return node;
41
        }
42
      }
43
    }
44

45
    return element;
46
  };
47

48
  var restoreDefaultText = function restoreDefaultText(input) {
49
    var defaultText = input.bsCustomFileInput.defaultText;
50
    var label = input.parentNode.querySelector(Selector.CUSTOMFILELABEL);
51

52
    if (label) {
53
      var element = findFirstChildNode(label);
54
      element.textContent = defaultText;
55
    }
56
  };
57

58
  var fileApi = !!window.File;
59
  var FAKE_PATH = 'fakepath';
60
  var FAKE_PATH_SEPARATOR = '\\';
61

62
  var getSelectedFiles = function getSelectedFiles(input) {
63
    if (input.hasAttribute('multiple') && fileApi) {
64
      return [].slice.call(input.files).map(function (file) {
65
        return file.name;
66
      }).join(', ');
67
    }
68

69
    if (input.value.indexOf(FAKE_PATH) !== -1) {
70
      var splittedValue = input.value.split(FAKE_PATH_SEPARATOR);
71
      return splittedValue[splittedValue.length - 1];
72
    }
73

74
    return input.value;
75
  };
76

77
  function handleInputChange() {
78
    var label = this.parentNode.querySelector(Selector.CUSTOMFILELABEL);
79

80
    if (label) {
81
      var element = findFirstChildNode(label);
82
      var inputValue = getSelectedFiles(this);
83

84
      if (inputValue.length) {
85
        element.textContent = inputValue;
86
      } else {
87
        restoreDefaultText(this);
88
      }
89
    }
90
  }
91

92
  function handleFormReset() {
93
    var customFileList = [].slice.call(this.querySelectorAll(Selector.INPUT)).filter(function (input) {
94
      return !!input.bsCustomFileInput;
95
    });
96

97
    for (var i = 0, len = customFileList.length; i < len; i++) {
98
      restoreDefaultText(customFileList[i]);
99
    }
100
  }
101

102
  var customProperty = 'bsCustomFileInput';
103
  var Event = {
104
    FORMRESET: 'reset',
105
    INPUTCHANGE: 'change'
106
  };
107
  var bsCustomFileInput = {
108
    init: function init(inputSelector, formSelector) {
109
      if (inputSelector === void 0) {
110
        inputSelector = Selector.CUSTOMFILE;
111
      }
112

113
      if (formSelector === void 0) {
114
        formSelector = Selector.FORM;
115
      }
116

117
      var customFileInputList = [].slice.call(document.querySelectorAll(inputSelector));
118
      var formList = [].slice.call(document.querySelectorAll(formSelector));
119

120
      for (var i = 0, len = customFileInputList.length; i < len; i++) {
121
        var input = customFileInputList[i];
122
        Object.defineProperty(input, customProperty, {
123
          value: {
124
            defaultText: getDefaultText(input)
125
          },
126
          writable: true
127
        });
128
        handleInputChange.call(input);
129
        input.addEventListener(Event.INPUTCHANGE, handleInputChange);
130
      }
131

132
      for (var _i = 0, _len = formList.length; _i < _len; _i++) {
133
        formList[_i].addEventListener(Event.FORMRESET, handleFormReset);
134

135
        Object.defineProperty(formList[_i], customProperty, {
136
          value: true,
137
          writable: true
138
        });
139
      }
140
    },
141
    destroy: function destroy() {
142
      var formList = [].slice.call(document.querySelectorAll(Selector.FORM)).filter(function (form) {
143
        return !!form.bsCustomFileInput;
144
      });
145
      var customFileInputList = [].slice.call(document.querySelectorAll(Selector.INPUT)).filter(function (input) {
146
        return !!input.bsCustomFileInput;
147
      });
148

149
      for (var i = 0, len = customFileInputList.length; i < len; i++) {
150
        var input = customFileInputList[i];
151
        restoreDefaultText(input);
152
        input[customProperty] = undefined;
153
        input.removeEventListener(Event.INPUTCHANGE, handleInputChange);
154
      }
155

156
      for (var _i2 = 0, _len2 = formList.length; _i2 < _len2; _i2++) {
157
        formList[_i2].removeEventListener(Event.FORMRESET, handleFormReset);
158

159
        formList[_i2][customProperty] = undefined;
160
      }
161
    }
162
  };
163

164
  return bsCustomFileInput;
165

166
})));
167
//# sourceMappingURL=bs-custom-file-input.js.map
168

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.