Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VTextField.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. // Styles
  3. import '../../../src/stylus/components/_text-fields.styl';
  4. // Extensions
  5. import VInput from '../VInput';
  6. // Components
  7. import VCounter from '../VCounter';
  8. import VLabel from '../VLabel';
  9. // Mixins
  10. import Maskable from '../../mixins/maskable';
  11. import Loadable from '../../mixins/loadable';
  12. // Directives
  13. import Ripple from '../../directives/ripple';
  14. // Utilities
  15. import { keyCodes } from '../../util/helpers';
  16. import { deprecate } from '../../util/console';
  17. var dirtyTypes = ['color', 'file', 'time', 'date', 'datetime-local', 'week', 'month'];
  18. /* @vue/component */
  19. export default VInput.extend({
  20. name: 'v-text-field',
  21. directives: { Ripple: Ripple },
  22. mixins: [Maskable, Loadable],
  23. inheritAttrs: false,
  24. props: {
  25. appendOuterIcon: String,
  26. /** @deprecated */
  27. appendOuterIconCb: Function,
  28. autofocus: Boolean,
  29. box: Boolean,
  30. browserAutocomplete: String,
  31. clearable: Boolean,
  32. clearIcon: {
  33. type: String,
  34. default: '$vuetify.icons.clear'
  35. },
  36. clearIconCb: Function,
  37. color: {
  38. type: String,
  39. default: 'primary'
  40. },
  41. counter: [Boolean, Number, String],
  42. flat: Boolean,
  43. fullWidth: Boolean,
  44. label: String,
  45. outline: Boolean,
  46. placeholder: String,
  47. prefix: String,
  48. prependInnerIcon: String,
  49. /** @deprecated */
  50. prependInnerIconCb: Function,
  51. reverse: Boolean,
  52. singleLine: Boolean,
  53. solo: Boolean,
  54. soloInverted: Boolean,
  55. suffix: String,
  56. type: {
  57. type: String,
  58. default: 'text'
  59. }
  60. },
  61. data: function data() {
  62. return {
  63. badInput: false,
  64. initialValue: null,
  65. internalChange: false,
  66. isClearing: false
  67. };
  68. },
  69. computed: {
  70. classes: function classes() {
  71. return {
  72. 'v-text-field': true,
  73. 'v-text-field--full-width': this.fullWidth,
  74. 'v-text-field--prefix': this.prefix,
  75. 'v-text-field--single-line': this.isSingle,
  76. 'v-text-field--solo': this.isSolo,
  77. 'v-text-field--solo-inverted': this.soloInverted,
  78. 'v-text-field--solo-flat': this.flat,
  79. 'v-text-field--box': this.box,
  80. 'v-text-field--enclosed': this.isEnclosed,
  81. 'v-text-field--reverse': this.reverse,
  82. 'v-text-field--outline': this.hasOutline,
  83. 'v-text-field--placeholder': this.placeholder
  84. };
  85. },
  86. counterValue: function counterValue() {
  87. return (this.internalValue || '').toString().length;
  88. },
  89. directivesInput: function directivesInput() {
  90. return [];
  91. },
  92. // TODO: Deprecate
  93. hasOutline: function hasOutline() {
  94. return this.outline || this.textarea;
  95. },
  96. internalValue: {
  97. get: function get() {
  98. return this.lazyValue;
  99. },
  100. set: function set(val) {
  101. if (this.mask && val !== this.lazyValue) {
  102. this.lazyValue = this.unmaskText(this.maskText(this.unmaskText(val)));
  103. this.setSelectionRange();
  104. } else {
  105. this.lazyValue = val;
  106. this.$emit('input', this.lazyValue);
  107. }
  108. }
  109. },
  110. isDirty: function isDirty() {
  111. return this.lazyValue != null && this.lazyValue.toString().length > 0 || this.badInput;
  112. },
  113. isEnclosed: function isEnclosed() {
  114. return this.box || this.isSolo || this.hasOutline || this.fullWidth;
  115. },
  116. isLabelActive: function isLabelActive() {
  117. return this.isDirty || dirtyTypes.includes(this.type);
  118. },
  119. isSingle: function isSingle() {
  120. return this.isSolo || this.singleLine;
  121. },
  122. isSolo: function isSolo() {
  123. return this.solo || this.soloInverted;
  124. },
  125. labelPosition: function labelPosition() {
  126. var offset = this.prefix && !this.labelValue ? this.prefixWidth : 0;
  127. return !this.$vuetify.rtl !== !this.reverse ? {
  128. left: 'auto',
  129. right: offset
  130. } : {
  131. left: offset,
  132. right: 'auto'
  133. };
  134. },
  135. showLabel: function showLabel() {
  136. return this.hasLabel && (!this.isSingle || !this.isLabelActive && !this.placeholder && !this.prefixLabel);
  137. },
  138. labelValue: function labelValue() {
  139. return !this.isSingle && Boolean(this.isFocused || this.isLabelActive || this.placeholder || this.prefixLabel);
  140. },
  141. prefixWidth: function prefixWidth() {
  142. if (!this.prefix && !this.$refs.prefix) return;
  143. return this.$refs.prefix.offsetWidth;
  144. },
  145. prefixLabel: function prefixLabel() {
  146. return this.prefix && !this.value;
  147. }
  148. },
  149. watch: {
  150. isFocused: function isFocused(val) {
  151. // Sets validationState from validatable
  152. this.hasColor = val;
  153. if (val) {
  154. this.initialValue = this.lazyValue;
  155. } else if (this.initialValue !== this.lazyValue) {
  156. this.$emit('change', this.lazyValue);
  157. }
  158. },
  159. value: function value(val) {
  160. var _this = this;
  161. if (this.mask && !this.internalChange) {
  162. var masked = this.maskText(this.unmaskText(val));
  163. this.lazyValue = this.unmaskText(masked);
  164. // Emit when the externally set value was modified internally
  165. String(val) !== this.lazyValue && this.$nextTick(function () {
  166. _this.$refs.input.value = masked;
  167. _this.$emit('input', _this.lazyValue);
  168. });
  169. } else this.lazyValue = val;
  170. }
  171. },
  172. mounted: function mounted() {
  173. this.autofocus && this.onFocus();
  174. },
  175. methods: {
  176. /** @public */
  177. focus: function focus() {
  178. this.onFocus();
  179. },
  180. /** @public */
  181. blur: function blur() {
  182. this.$refs.input ? this.$refs.input.blur() : this.onBlur();
  183. },
  184. clearableCallback: function clearableCallback() {
  185. var _this2 = this;
  186. this.internalValue = null;
  187. this.$nextTick(function () {
  188. return _this2.$refs.input.focus();
  189. });
  190. },
  191. genAppendSlot: function genAppendSlot() {
  192. var slot = [];
  193. if (this.$slots['append-outer']) {
  194. slot.push(this.$slots['append-outer']);
  195. } else if (this.appendOuterIcon) {
  196. slot.push(this.genIcon('appendOuter'));
  197. }
  198. return this.genSlot('append', 'outer', slot);
  199. },
  200. genPrependInnerSlot: function genPrependInnerSlot() {
  201. var slot = [];
  202. if (this.$slots['prepend-inner']) {
  203. slot.push(this.$slots['prepend-inner']);
  204. } else if (this.prependInnerIcon) {
  205. slot.push(this.genIcon('prependInner'));
  206. }
  207. return this.genSlot('prepend', 'inner', slot);
  208. },
  209. genIconSlot: function genIconSlot() {
  210. var slot = [];
  211. if (this.$slots['append']) {
  212. slot.push(this.$slots['append']);
  213. } else if (this.appendIcon) {
  214. slot.push(this.genIcon('append'));
  215. }
  216. return this.genSlot('append', 'inner', slot);
  217. },
  218. genInputSlot: function genInputSlot() {
  219. var input = VInput.options.methods.genInputSlot.call(this);
  220. var prepend = this.genPrependInnerSlot();
  221. prepend && input.children.unshift(prepend);
  222. return input;
  223. },
  224. genClearIcon: function genClearIcon() {
  225. if (!this.clearable) return null;
  226. var icon = !this.isDirty ? false : 'clear';
  227. if (this.clearIconCb) deprecate(':clear-icon-cb', '@click:clear', this);
  228. return this.genSlot('append', 'inner', [this.genIcon(icon, !this.$listeners['click:clear'] && this.clearIconCb || this.clearableCallback, false)]);
  229. },
  230. genCounter: function genCounter() {
  231. if (this.counter === false || this.counter == null) return null;
  232. var max = this.counter === true ? this.$attrs.maxlength : this.counter;
  233. return this.$createElement(VCounter, {
  234. props: {
  235. dark: this.dark,
  236. light: this.light,
  237. max: max,
  238. value: this.counterValue
  239. }
  240. });
  241. },
  242. genDefaultSlot: function genDefaultSlot() {
  243. return [this.genTextFieldSlot(), this.genClearIcon(), this.genIconSlot(), this.genProgress()];
  244. },
  245. genLabel: function genLabel() {
  246. if (!this.showLabel) return null;
  247. var data = {
  248. props: {
  249. absolute: true,
  250. color: this.validationState,
  251. dark: this.dark,
  252. disabled: this.disabled,
  253. focused: !this.isSingle && (this.isFocused || !!this.validationState),
  254. left: this.labelPosition.left,
  255. light: this.light,
  256. right: this.labelPosition.right,
  257. value: this.labelValue
  258. }
  259. };
  260. if (this.$attrs.id) data.props.for = this.$attrs.id;
  261. return this.$createElement(VLabel, data, this.$slots.label || this.label);
  262. },
  263. genInput: function genInput() {
  264. var listeners = Object.assign({}, this.$listeners);
  265. delete listeners['change']; // Change should not be bound externally
  266. var data = {
  267. style: {},
  268. domProps: {
  269. value: this.maskText(this.lazyValue)
  270. },
  271. attrs: _extends({
  272. 'aria-label': (!this.$attrs || !this.$attrs.id) && this.label
  273. }, this.$attrs, {
  274. autofocus: this.autofocus,
  275. disabled: this.disabled,
  276. readonly: this.readonly,
  277. type: this.type
  278. }),
  279. on: Object.assign(listeners, {
  280. blur: this.onBlur,
  281. input: this.onInput,
  282. focus: this.onFocus,
  283. keydown: this.onKeyDown
  284. }),
  285. ref: 'input'
  286. };
  287. if (this.placeholder) data.attrs.placeholder = this.placeholder;
  288. if (this.mask) data.attrs.maxlength = this.masked.length;
  289. if (this.browserAutocomplete) data.attrs.autocomplete = this.browserAutocomplete;
  290. return this.$createElement('input', data);
  291. },
  292. genMessages: function genMessages() {
  293. if (this.hideDetails) return null;
  294. return this.$createElement('div', {
  295. staticClass: 'v-text-field__details'
  296. }, [VInput.options.methods.genMessages.call(this), this.genCounter()]);
  297. },
  298. genTextFieldSlot: function genTextFieldSlot() {
  299. return this.$createElement('div', {
  300. staticClass: 'v-text-field__slot'
  301. }, [this.genLabel(), this.prefix ? this.genAffix('prefix') : null, this.genInput(), this.suffix ? this.genAffix('suffix') : null]);
  302. },
  303. genAffix: function genAffix(type) {
  304. return this.$createElement('div', {
  305. 'class': 'v-text-field__' + type,
  306. ref: type
  307. }, this[type]);
  308. },
  309. onBlur: function onBlur(e) {
  310. this.isFocused = false;
  311. // Reset internalChange state
  312. // to allow external change
  313. // to persist
  314. this.internalChange = false;
  315. this.$emit('blur', e);
  316. },
  317. onClick: function onClick() {
  318. if (this.isFocused || this.disabled) return;
  319. this.$refs.input.focus();
  320. },
  321. onFocus: function onFocus(e) {
  322. if (!this.$refs.input) return;
  323. if (document.activeElement !== this.$refs.input) {
  324. return this.$refs.input.focus();
  325. }
  326. if (!this.isFocused) {
  327. this.isFocused = true;
  328. this.$emit('focus', e);
  329. }
  330. },
  331. onInput: function onInput(e) {
  332. this.internalChange = true;
  333. this.mask && this.resetSelections(e.target);
  334. this.internalValue = e.target.value;
  335. this.badInput = e.target.validity && e.target.validity.badInput;
  336. },
  337. onKeyDown: function onKeyDown(e) {
  338. this.internalChange = true;
  339. if (e.keyCode === keyCodes.enter) this.$emit('change', this.internalValue);
  340. this.$emit('keydown', e);
  341. },
  342. onMouseDown: function onMouseDown(e) {
  343. // Prevent input from being blurred
  344. if (e.target !== this.$refs.input) {
  345. e.preventDefault();
  346. e.stopPropagation();
  347. }
  348. VInput.options.methods.onMouseDown.call(this, e);
  349. },
  350. onMouseUp: function onMouseUp(e) {
  351. if (this.hasMouseDown) this.focus();
  352. VInput.options.methods.onMouseUp.call(this, e);
  353. }
  354. }
  355. });
  356. //# sourceMappingURL=VTextField.js.map