GPQAPP
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) {7typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :8typeof define === 'function' && define.amd ? define(factory) :9(global = global || self, global.bsCustomFileInput = factory());10}(this, (function () { 'use strict';11
12var Selector = {13CUSTOMFILE: '.custom-file input[type="file"]',14CUSTOMFILELABEL: '.custom-file-label',15FORM: 'form',16INPUT: 'input'17};18
19var textNodeType = 3;20
21var getDefaultText = function getDefaultText(input) {22var defaultText = '';23var label = input.parentNode.querySelector(Selector.CUSTOMFILELABEL);24
25if (label) {26defaultText = label.textContent;27}28
29return defaultText;30};31
32var findFirstChildNode = function findFirstChildNode(element) {33if (element.childNodes.length > 0) {34var childNodes = [].slice.call(element.childNodes);35
36for (var i = 0; i < childNodes.length; i++) {37var node = childNodes[i];38
39if (node.nodeType !== textNodeType) {40return node;41}42}43}44
45return element;46};47
48var restoreDefaultText = function restoreDefaultText(input) {49var defaultText = input.bsCustomFileInput.defaultText;50var label = input.parentNode.querySelector(Selector.CUSTOMFILELABEL);51
52if (label) {53var element = findFirstChildNode(label);54element.textContent = defaultText;55}56};57
58var fileApi = !!window.File;59var FAKE_PATH = 'fakepath';60var FAKE_PATH_SEPARATOR = '\\';61
62var getSelectedFiles = function getSelectedFiles(input) {63if (input.hasAttribute('multiple') && fileApi) {64return [].slice.call(input.files).map(function (file) {65return file.name;66}).join(', ');67}68
69if (input.value.indexOf(FAKE_PATH) !== -1) {70var splittedValue = input.value.split(FAKE_PATH_SEPARATOR);71return splittedValue[splittedValue.length - 1];72}73
74return input.value;75};76
77function handleInputChange() {78var label = this.parentNode.querySelector(Selector.CUSTOMFILELABEL);79
80if (label) {81var element = findFirstChildNode(label);82var inputValue = getSelectedFiles(this);83
84if (inputValue.length) {85element.textContent = inputValue;86} else {87restoreDefaultText(this);88}89}90}91
92function handleFormReset() {93var customFileList = [].slice.call(this.querySelectorAll(Selector.INPUT)).filter(function (input) {94return !!input.bsCustomFileInput;95});96
97for (var i = 0, len = customFileList.length; i < len; i++) {98restoreDefaultText(customFileList[i]);99}100}101
102var customProperty = 'bsCustomFileInput';103var Event = {104FORMRESET: 'reset',105INPUTCHANGE: 'change'106};107var bsCustomFileInput = {108init: function init(inputSelector, formSelector) {109if (inputSelector === void 0) {110inputSelector = Selector.CUSTOMFILE;111}112
113if (formSelector === void 0) {114formSelector = Selector.FORM;115}116
117var customFileInputList = [].slice.call(document.querySelectorAll(inputSelector));118var formList = [].slice.call(document.querySelectorAll(formSelector));119
120for (var i = 0, len = customFileInputList.length; i < len; i++) {121var input = customFileInputList[i];122Object.defineProperty(input, customProperty, {123value: {124defaultText: getDefaultText(input)125},126writable: true127});128handleInputChange.call(input);129input.addEventListener(Event.INPUTCHANGE, handleInputChange);130}131
132for (var _i = 0, _len = formList.length; _i < _len; _i++) {133formList[_i].addEventListener(Event.FORMRESET, handleFormReset);134
135Object.defineProperty(formList[_i], customProperty, {136value: true,137writable: true138});139}140},141destroy: function destroy() {142var formList = [].slice.call(document.querySelectorAll(Selector.FORM)).filter(function (form) {143return !!form.bsCustomFileInput;144});145var customFileInputList = [].slice.call(document.querySelectorAll(Selector.INPUT)).filter(function (input) {146return !!input.bsCustomFileInput;147});148
149for (var i = 0, len = customFileInputList.length; i < len; i++) {150var input = customFileInputList[i];151restoreDefaultText(input);152input[customProperty] = undefined;153input.removeEventListener(Event.INPUTCHANGE, handleInputChange);154}155
156for (var _i2 = 0, _len2 = formList.length; _i2 < _len2; _i2++) {157formList[_i2].removeEventListener(Event.FORMRESET, handleFormReset);158
159formList[_i2][customProperty] = undefined;160}161}162};163
164return bsCustomFileInput;165
166})));167//# sourceMappingURL=bs-custom-file-input.js.map
168