summaryrefslogtreecommitdiff
path: root/templates/assets/bootstrap-material-design/js/base.js
blob: 283b9769210d1f5d713c7106298871f270c95694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Util from './util'

const Base = (($) => {

  const ClassName = {
    BMD_FORM_GROUP: 'bmd-form-group',
    IS_FILLED: 'is-filled',
    IS_FOCUSED: 'is-focused'
  }

  const Selector = {
    BMD_FORM_GROUP: `.${ClassName.BMD_FORM_GROUP}`
  }

  const Default = {}

  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */
  class Base {

    /**
     *
     * @param element
     * @param config
     * @param properties - anything that needs to be set as this[key] = value.  Works around the need to call `super` before using `this`
     */
    constructor($element, config, properties = {}) {
      this.$element = $element
      this.config = $.extend(true, {}, Default, config)

      // set properties for use in the constructor initialization
      for (let key in properties) {
        this[key] = properties[key]
      }
    }

    dispose(dataKey) {
      this.$element.data(dataKey, null)
      this.$element = null
      this.config = null
    }

    // ------------------------------------------------------------------------
    // protected

    addFormGroupFocus() {
      if (!this.$element.prop('disabled')) {
        this.$bmdFormGroup.addClass(ClassName.IS_FOCUSED)
      }
    }

    removeFormGroupFocus() {
      this.$bmdFormGroup.removeClass(ClassName.IS_FOCUSED)
    }

    removeIsFilled() {
      this.$bmdFormGroup.removeClass(ClassName.IS_FILLED)
    }

    addIsFilled() {
      this.$bmdFormGroup.addClass(ClassName.IS_FILLED)
    }

    // Find bmd-form-group
    findMdbFormGroup(raiseError = true) {
      let mfg = this.$element.closest(Selector.BMD_FORM_GROUP)
      if (mfg.length === 0 && raiseError) {
        $.error(`Failed to find ${Selector.BMD_FORM_GROUP} for ${Util.describe(this.$element)}`)
      }
      return mfg
    }

    // ------------------------------------------------------------------------
    // private

    // ------------------------------------------------------------------------
    // static

  }

  return Base

})(jQuery)

export default Base