material.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define("/Plugin/material", ["exports", "jquery", "Plugin"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(exports, require("jquery"), require("Plugin"));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod.exports, global.jQuery, global.Plugin);
  11. global.PluginMaterial = mod.exports;
  12. }
  13. })(this, function (_exports, _jquery, _Plugin2) {
  14. "use strict";
  15. Object.defineProperty(_exports, "__esModule", {
  16. value: true
  17. });
  18. _exports.default = void 0;
  19. _jquery = babelHelpers.interopRequireDefault(_jquery);
  20. _Plugin2 = babelHelpers.interopRequireDefault(_Plugin2);
  21. var NAME = 'formMaterial';
  22. function isChar(e) {
  23. if (typeof e.which === 'undefined') {
  24. return true;
  25. } else if (typeof e.which === 'number' && e.which > 0) {
  26. return !e.ctrlKey && !e.metaKey && !e.altKey && e.which !== 8 && e.which !== 9;
  27. }
  28. return false;
  29. }
  30. var FormMaterial =
  31. /*#__PURE__*/
  32. function (_Plugin) {
  33. babelHelpers.inherits(FormMaterial, _Plugin);
  34. function FormMaterial() {
  35. babelHelpers.classCallCheck(this, FormMaterial);
  36. return babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(FormMaterial).apply(this, arguments));
  37. }
  38. babelHelpers.createClass(FormMaterial, [{
  39. key: "getName",
  40. value: function getName() {
  41. return NAME;
  42. }
  43. }, {
  44. key: "render",
  45. value: function render() {
  46. var $el = this.$el;
  47. var $control = this.$control = $el.find('.form-control'); // Add hint label if required
  48. if ($control.attr('data-hint')) {
  49. $control.after("<div class=hint>".concat($control.attr('data-hint'), "</div>"));
  50. }
  51. if ($el.hasClass('floating')) {
  52. // Add floating label if required
  53. if ($control.hasClass('floating-label')) {
  54. var placeholder = $control.attr('placeholder');
  55. $control.attr('placeholder', null).removeClass('floating-label');
  56. $control.after("<div class=floating-label>".concat(placeholder, "</div>"));
  57. } // Set as empty if is empty
  58. if ($control.val() === null || $control.val() === 'undefined' || $control.val() === '') {
  59. $control.addClass('empty');
  60. }
  61. } // Support for file input
  62. if ($control.next().is('[type=file]')) {
  63. $el.addClass('form-material-file');
  64. }
  65. this.$file = $el.find('[type=file]');
  66. this.bind();
  67. $el.data('formMaterialAPI', this);
  68. }
  69. }, {
  70. key: "bind",
  71. value: function bind() {
  72. var _this = this;
  73. var $el = this.$el;
  74. var $control = this.$control = $el.find('.form-control');
  75. $el.on('keydown.site.material paste.site.material', '.form-control', function (e) {
  76. if (isChar(e)) {
  77. $control.removeClass('empty');
  78. }
  79. }).on('keyup.site.material change.site.material', '.form-control', function () {
  80. if ($control.val() === '' && typeof $control[0].checkValidity !== 'undefined' && $control[0].checkValidity()) {
  81. $control.addClass('empty');
  82. } else {
  83. $control.removeClass('empty');
  84. }
  85. });
  86. if (this.$file.length > 0) {
  87. this.$file.on('focus', function () {
  88. _this.$el.find('input').addClass('focus');
  89. }).on('blur', function () {
  90. _this.$el.find('input').removeClass('focus');
  91. }).on('change', function () {
  92. var $this = (0, _jquery.default)(this);
  93. var value = '';
  94. _jquery.default.each($this[0].files, function (i, file) {
  95. value += "".concat(file.name, ", ");
  96. });
  97. value = value.substring(0, value.length - 2);
  98. if (value) {
  99. $this.prev().removeClass('empty');
  100. } else {
  101. $this.prev().addClass('empty');
  102. }
  103. $this.prev().val(value);
  104. });
  105. }
  106. }
  107. }]);
  108. return FormMaterial;
  109. }(_Plugin2.default);
  110. _Plugin2.default.register(NAME, FormMaterial);
  111. var _default = FormMaterial;
  112. _exports.default = _default;
  113. });