bs-custom-file-input.js.map 10 KB

1
  1. {"version":3,"file":"bs-custom-file-input.js","sources":["../src/selector.js","../src/util.js","../src/eventHandlers.js","../src/index.js"],"sourcesContent":["const Selector = {\n CUSTOMFILE: '.custom-file input[type=\"file\"]',\n CUSTOMFILELABEL: '.custom-file-label',\n FORM: 'form',\n INPUT: 'input',\n}\n\nexport default Selector\n","import Selector from './selector'\n\nconst textNodeType = 3\nconst getDefaultText = (input) => {\n let defaultText = ''\n\n const label = input.parentNode.querySelector(Selector.CUSTOMFILELABEL)\n\n if (label) {\n defaultText = label.textContent\n }\n\n return defaultText\n}\n\nconst findFirstChildNode = (element) => {\n if (element.childNodes.length > 0) {\n const childNodes = [].slice.call(element.childNodes)\n\n for (let i = 0; i < childNodes.length; i++) {\n const node = childNodes[i]\n if (node.nodeType !== textNodeType) {\n return node\n }\n }\n }\n\n return element\n}\n\nconst restoreDefaultText = (input) => {\n const defaultText = input.bsCustomFileInput.defaultText\n const label = input.parentNode.querySelector(Selector.CUSTOMFILELABEL)\n\n if (label) {\n const element = findFirstChildNode(label)\n\n element.textContent = defaultText\n }\n}\n\nexport {\n getDefaultText,\n findFirstChildNode,\n restoreDefaultText,\n}\n","import { findFirstChildNode, restoreDefaultText } from './util'\nimport Selector from './selector'\n\nconst fileApi = !!window.File\nconst FAKE_PATH = 'fakepath'\nconst FAKE_PATH_SEPARATOR = '\\\\'\n\nconst getSelectedFiles = (input) => {\n if (input.hasAttribute('multiple') && fileApi) {\n return [].slice.call(input.files)\n .map((file) => file.name)\n .join(', ')\n }\n\n if (input.value.indexOf(FAKE_PATH) !== -1) {\n const splittedValue = input.value.split(FAKE_PATH_SEPARATOR)\n\n return splittedValue[splittedValue.length - 1]\n }\n\n return input.value\n}\n\nfunction handleInputChange() {\n const label = this.parentNode.querySelector(Selector.CUSTOMFILELABEL)\n\n if (label) {\n const element = findFirstChildNode(label)\n const inputValue = getSelectedFiles(this)\n\n if (inputValue.length) {\n element.textContent = inputValue\n } else {\n restoreDefaultText(this)\n }\n }\n}\n\nfunction handleFormReset() {\n const customFileList = [].slice.call(this.querySelectorAll(Selector.INPUT))\n .filter((input) => !!input.bsCustomFileInput)\n\n for (let i = 0, len = customFileList.length; i < len; i++) {\n restoreDefaultText(customFileList[i])\n }\n}\n\nexport {\n handleInputChange,\n handleFormReset,\n}\n","import { getDefaultText, restoreDefaultText } from './util'\nimport { handleFormReset, handleInputChange } from './eventHandlers'\nimport Selector from './selector'\n\nconst customProperty = 'bsCustomFileInput'\nconst Event = {\n FORMRESET : 'reset',\n INPUTCHANGE : 'change',\n}\n\nconst bsCustomFileInput = {\n init(inputSelector = Selector.CUSTOMFILE, formSelector = Selector.FORM) {\n const customFileInputList = [].slice.call(document.querySelectorAll(inputSelector))\n const formList = [].slice.call(document.querySelectorAll(formSelector))\n\n for (let i = 0, len = customFileInputList.length; i < len; i++) {\n const input = customFileInputList[i]\n\n Object.defineProperty(input, customProperty, {\n value: {\n defaultText: getDefaultText(input),\n },\n writable: true,\n })\n\n handleInputChange.call(input)\n input.addEventListener(Event.INPUTCHANGE, handleInputChange)\n }\n\n for (let i = 0, len = formList.length; i < len; i++) {\n formList[i].addEventListener(Event.FORMRESET, handleFormReset)\n Object.defineProperty(formList[i], customProperty, {\n value: true,\n writable: true,\n })\n }\n },\n\n destroy() {\n const formList = [].slice.call(document.querySelectorAll(Selector.FORM))\n .filter((form) => !!form.bsCustomFileInput)\n const customFileInputList = [].slice.call(document.querySelectorAll(Selector.INPUT))\n .filter((input) => !!input.bsCustomFileInput)\n\n for (let i = 0, len = customFileInputList.length; i < len; i++) {\n const input = customFileInputList[i]\n\n restoreDefaultText(input)\n input[customProperty] = undefined\n\n input.removeEventListener(Event.INPUTCHANGE, handleInputChange)\n }\n\n for (let i = 0, len = formList.length; i < len; i++) {\n formList[i].removeEventListener(Event.FORMRESET, handleFormReset)\n formList[i][customProperty] = undefined\n }\n },\n}\n\nexport default bsCustomFileInput\n"],"names":["Selector","CUSTOMFILE","CUSTOMFILELABEL","FORM","INPUT","textNodeType","getDefaultText","input","defaultText","label","parentNode","querySelector","textContent","findFirstChildNode","element","childNodes","length","slice","call","i","node","nodeType","restoreDefaultText","bsCustomFileInput","fileApi","window","File","FAKE_PATH","FAKE_PATH_SEPARATOR","getSelectedFiles","hasAttribute","files","map","file","name","join","value","indexOf","splittedValue","split","handleInputChange","inputValue","handleFormReset","customFileList","querySelectorAll","filter","len","customProperty","Event","FORMRESET","INPUTCHANGE","init","inputSelector","formSelector","customFileInputList","document","formList","Object","defineProperty","writable","addEventListener","destroy","form","undefined","removeEventListener"],"mappings":";;;;;;;;;;;EAAA,IAAMA,QAAQ,GAAG;EACfC,EAAAA,UAAU,EAAE,iCADG;EAEfC,EAAAA,eAAe,EAAE,oBAFF;EAGfC,EAAAA,IAAI,EAAE,MAHS;EAIfC,EAAAA,KAAK,EAAE;EAJQ,CAAjB;;ECEA,IAAMC,YAAY,GAAG,CAArB;;EACA,IAAMC,cAAc,GAAG,SAAjBA,cAAiB,CAACC,KAAD,EAAW;EAChC,MAAIC,WAAW,GAAG,EAAlB;EAEA,MAAMC,KAAK,GAAGF,KAAK,CAACG,UAAN,CAAiBC,aAAjB,CAA+BX,QAAQ,CAACE,eAAxC,CAAd;;EAEA,MAAIO,KAAJ,EAAW;EACTD,IAAAA,WAAW,GAAGC,KAAK,CAACG,WAApB;EACD;;EAED,SAAOJ,WAAP;EACD,CAVD;;EAYA,IAAMK,kBAAkB,GAAG,SAArBA,kBAAqB,CAACC,OAAD,EAAa;EACtC,MAAIA,OAAO,CAACC,UAAR,CAAmBC,MAAnB,GAA4B,CAAhC,EAAmC;EACjC,QAAMD,UAAU,GAAG,GAAGE,KAAH,CAASC,IAAT,CAAcJ,OAAO,CAACC,UAAtB,CAAnB;;EAEA,SAAK,IAAII,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGJ,UAAU,CAACC,MAA/B,EAAuCG,CAAC,EAAxC,EAA4C;EAC1C,UAAMC,IAAI,GAAGL,UAAU,CAAC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