2016-01-26 21:12:48 +03:00
import BaseInput from './baseInput'
//import Checkbox from './checkbox'
//import Radio from './radio'
//import Switch from './switch'
//import Text from './text'
//import Textarea from './textarea'
//import Select from './select'
import Util from './util'
const File = ( ( $ ) => {
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* Constants
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
const NAME = 'file'
2016-03-28 23:18:19 +03:00
const DATA _KEY = ` bmd. ${ NAME } `
const JQUERY _NAME = ` bmd ${ NAME . charAt ( 0 ) . toUpperCase ( ) + NAME . slice ( 1 ) } `
2016-01-26 21:12:48 +03:00
const JQUERY _NO _CONFLICT = $ . fn [ JQUERY _NAME ]
const Default = { }
const ClassName = {
FILE : NAME ,
IS _FILE : 'is-file'
}
const Selector = {
FILENAMES : 'input.form-control[readonly]'
}
/ * *
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* Class Definition
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
class File extends BaseInput {
constructor ( $element , config ) {
super ( $element , $ . extend ( true ,
//{invalidComponentMatches: [Checkbox, Radio, Text, Textarea, Select, Switch]},
Default , config ) )
2016-03-28 23:18:19 +03:00
this . $bmdFormGroup . addClass ( ClassName . IS _FILE )
2016-01-26 21:12:48 +03:00
}
dispose ( ) {
super . dispose ( DATA _KEY )
}
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'. ` )
}
// ------------------------------------------------------------------------
// 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 ( ) {
2016-03-28 23:18:19 +03:00
this . $bmdFormGroup
2016-01-26 21:12:48 +03:00
. on ( 'focus' , ( ) => {
this . addFormGroupFocus ( )
} )
. on ( 'blur' , ( ) => {
this . removeFormGroupFocus ( )
} )
}
addChangeListener ( ) {
// set the fileinput readonly field with the name of the file
this . $element . on ( 'change' , ( ) => {
let value = ''
$ . each ( this . $element . files , ( i , file ) => {
value += ` ${ file . name } , `
} )
value = value . substring ( 0 , value . length - 2 )
if ( value ) {
this . addIsFilled ( )
} else {
this . removeIsFilled ( )
}
2016-03-28 23:18:19 +03:00
this . $bmdFormGroup . find ( Selector . FILENAMES ) . val ( value )
2016-01-26 21:12:48 +03:00
} )
}
// ------------------------------------------------------------------------
// private
// ------------------------------------------------------------------------
// static
static _jQueryInterface ( config ) {
return this . each ( function ( ) {
let $element = $ ( this )
let data = $element . data ( DATA _KEY )
if ( ! data ) {
data = new File ( $element , config )
$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
}
return File
} ) ( jQuery )
export default File