mdb-ui-kit/js/src/file.js

138 lines
4.0 KiB
JavaScript
Raw Normal View History

import BaseInput from './baseInput'
import Checkbox from './checkbox'
import Radio from './radio'
import Switch from './switch'
import Text from './text'
import Textarea from './textare'
import Select from './select'
2015-12-02 03:28:36 +03:00
import Util from './util'
const File = (($) => {
2015-12-02 03:28:36 +03:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const NAME = 'file'
2015-12-02 03:28:36 +03:00
const DATA_KEY = `mdb.${NAME}`
const JQUERY_NAME = `mdb${NAME.charAt(0).toUpperCase() + NAME.slice(1)}`
const JQUERY_NO_CONFLICT = $.fn[JQUERY_NAME]
2015-12-02 03:28:36 +03:00
const Default = {}
2015-12-02 03:28:36 +03:00
const ClassName = {
FILE: NAME,
IS_FILE: 'is-file'
2015-12-02 03:28:36 +03:00
}
const Selector = {
FILENAMES: 'input.form-control[readonly]'
}
2015-12-02 03:28:36 +03:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class File extends BaseInput {
2015-12-02 03:28:36 +03:00
2015-12-07 20:40:42 +03:00
constructor($element, config) {
super($element, $.extend(true, {invalidComponentMatches: [Checkbox, Radio, Text, Textarea, Select, Switch]}, Default, config))
2015-12-02 03:28:36 +03:00
this.$mdbFormGroup.addClass(ClassName.IS_FILE)
2015-12-02 03:28:36 +03:00
}
dispose() {
super.dispose(DATA_KEY)
2015-12-02 03:28:36 +03:00
}
static matches($element) {
if ($element.attr('type') === 'file') {
return true
}
return false
}
static rejectMatch(component, $element) {
Util.assert(this.$element, this.matches($element), `${component} component element ${Util.describe($element)} is invalid for type='file'.`)
}
2015-12-02 03:28:36 +03:00
// ------------------------------------------------------------------------
// protected
// Demarcation element (e.g. first child of a form-group)
outerElement() {
// label.file > input[type=file]
return this.$element.parent().closest(`.${ClassName.FILE}`)
}
rejectWithoutRequiredStructure() {
// label.file > input[type=file]
Util.assert(this.$element, !this.outerElement().prop('tagName') === 'label', `${this.constructor.name}'s ${Util.describe(this.$element)} parent element ${Util.describe(this.outerElement())} should be <label>.`)
Util.assert(this.$element, !this.outerElement().hasClass(ClassName.FILE), `${this.constructor.name}'s ${Util.describe(this.$element)} parent element ${Util.describe(this.outerElement())} should have class .${ClassName.FILE}.`)
}
addFocusListener() {
this.$mdbFormGroup
2015-12-03 20:35:51 +03:00
.on('focus', () => {
this.addFormGroupFocus()
2015-12-02 03:28:36 +03:00
})
.on('blur', () => {
this.removeFormGroupFocus()
2015-12-02 03:28:36 +03:00
})
}
2015-12-02 03:28:36 +03:00
addChangeListener() {
2015-12-02 03:28:36 +03:00
// set the fileinput readonly field with the name of the file
this.$element.on('change', () => {
2015-12-02 03:28:36 +03:00
let value = ''
$.each(this.$element.files, (i, file) => {
2015-12-02 03:28:36 +03:00
value += `${file.name} , `
})
value = value.substring(0, value.length - 2)
if (value) {
this.removeIsEmpty()
2015-12-02 03:28:36 +03:00
} else {
this.addIsEmpty()
2015-12-02 03:28:36 +03:00
}
this.$mdbFormGroup.find(Selector.FILENAMES).val(value)
2015-12-02 03:28:36 +03:00
})
}
// ------------------------------------------------------------------------
// private
2015-12-02 03:28:36 +03:00
// ------------------------------------------------------------------------
// static
static _jQueryInterface(config) {
return this.each(function () {
let $element = $(this)
let data = $element.data(DATA_KEY)
if (!data) {
2015-12-07 20:40:42 +03:00
data = new File($element, config)
2015-12-02 03:28:36 +03:00
$element.data(DATA_KEY, data)
}
})
}
}
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[JQUERY_NAME] = File._jQueryInterface
$.fn[JQUERY_NAME].Constructor = File
$.fn[JQUERY_NAME].noConflict = () => {
$.fn[JQUERY_NAME] = JQUERY_NO_CONFLICT
return File._jQueryInterface
2015-12-02 03:28:36 +03:00
}
return File
2015-12-02 03:28:36 +03:00
})(jQuery)
export default File