demo.js 6.2 KB
Newer Older
C
codecalm 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
'use strict';

class TablerDemo {
  constructor() {
    this.init();

    this.form = document.querySelector('.js-layout-form');

    if(this.form) {
      this.form.addEventListener('submit', (e) => {
        e.preventDefault();
        this.onSubmitForm();
      });

      var inputs = this.form.querySelectorAll('input[type="radio"]');
      for (var i = 0; i < inputs.length; i++) {
        inputs[i].addEventListener('change', () => {
          this.onSubmitForm();
        });
      }
    }
C
codecalm 已提交
22 23

    this.initFormControls();
C
codecalm 已提交
24 25 26 27 28 29
  };

  init() {
    this.config = this.getConfig();
  };

A
Anton 已提交
30
  getConfig() {
C
codecalm 已提交
31 32 33
    return {
      colorScheme: (localStorage.getItem('tablerColorScheme')) ? localStorage.getItem('tablerColorScheme') : 'light',
      navPosition: (localStorage.getItem('tablerNavPosition')) ? localStorage.getItem('tablerNavPosition') : 'side',
C
codecalm 已提交
34 35 36
      headerColor: (localStorage.getItem('tablerHeaderColor')) ? localStorage.getItem('tablerHeaderColor') : 'light',
      headerFixed: (localStorage.getItem('tablerHeaderFixed')) ? localStorage.getItem('tablerHeaderFixed') : 'default',
      sidebarColor: (localStorage.getItem('tablerSidebarColor')) ? localStorage.getItem('tablerSidebarColor') : 'dark',
C
codecalm 已提交
37
      sidebarSize: (localStorage.getItem('tablerSidebarSize')) ? localStorage.getItem('tablerSidebarSize') : 'default',
C
codecalm 已提交
38 39
      sidebarPosition: (localStorage.getItem('tablerSidebarPosition')) ? localStorage.getItem('tablerSidebarPosition') : 'left',
      sidebarFixed: (localStorage.getItem('tablerSidebarFixed')) ? localStorage.getItem('tablerSidebarFixed') : 'fixed',
C
codecalm 已提交
40 41 42
    };
  };

A
Anton 已提交
43
  setConfig(key, value, availableValues, onSuccess) {
C
codecalm 已提交
44 45 46 47 48 49 50 51 52 53 54
    if (availableValues && availableValues.indexOf(value) !== -1) {
      key = 'tabler' + key.charAt(0).toUpperCase() + key.slice(1);

      localStorage.setItem(key, value);

      onSuccess && onSuccess(value);
    }

    return this.getConfig();
  };

A
Anton 已提交
55
  onSubmitForm() {
C
codecalm 已提交
56 57 58 59
    const form = this.form;

    this.toggleColorScheme(form.querySelector('[name="color-scheme"]:checked').value);
    this.toggleNavPosition(form.querySelector('[name="nav-position"]:checked').value);
C
codecalm 已提交
60
    this.toggleHeaderColor(form.querySelector('[name="header-color"]:checked').value);
C
codecalm 已提交
61
    // this.toggleHeaderFixed(form.querySelector('[name="header-fixed"]:checked').value);
C
codecalm 已提交
62 63
    this.toggleSidebarSize(form.querySelector('[name="sidebar-size"]:checked').value);
    this.toggleSidebarColor(form.querySelector('[name="sidebar-color"]:checked').value);
C
codecalm 已提交
64 65
    this.toggleSidebarPosition(form.querySelector('[name="sidebar-position"]:checked').value);
    this.toggleSidebarFixed(form.querySelector('[name="sidebar-fixed"]:checked').value);
C
codecalm 已提交
66 67
  };

A
Anton 已提交
68
  initFormControls() {
C
codecalm 已提交
69 70 71 72
    const config = this.getConfig();

    this.toggleColorScheme(config.colorScheme);
    this.toggleNavPosition(config.navPosition);
C
codecalm 已提交
73
    this.toggleHeaderColor(config.headerColor);
C
codecalm 已提交
74
    // this.toggleHeaderFixed(config.headerFixed);
C
codecalm 已提交
75 76 77
    this.toggleSidebarPosition(config.sidebarPosition);
    this.toggleSidebarSize(config.sidebarSize);
    this.toggleSidebarColor(config.sidebarColor);
C
codecalm 已提交
78
    this.toggleSidebarFixed(config.sidebarFixed);
C
codecalm 已提交
79 80
  };

A
Anton 已提交
81
  setFormValue(name, value) {
C
codecalm 已提交
82 83
    if(this.form) {
      let elements = this.form.querySelectorAll(`[name="${name}"]`);
C
codecalm 已提交
84

C
codecalm 已提交
85 86 87 88
      if (elements) {
        elements.forEach((e) => e.checked = false);
        this.form.querySelector(`[name="${name}"][value="${value}"]`).checked = true;
      }
C
codecalm 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    }
  };

  /*
  Color scheme toggle
   */
  toggleColorScheme(color) {
    return this.setConfig('colorScheme', color, ['dark', 'light'], () => {
      if (color === 'dark') {
        document.body.classList.add('theme-dark');
      } else {
        document.body.classList.remove('theme-dark');
      }

      this.setFormValue('color-scheme', color);
    });
  };

  toggleNavPosition(position) {
    return this.setConfig('navPosition', position, ['top', 'side'], () => {
      this.setFormValue('nav-position', position);
    });
  };

  toggleSidebarPosition(position) {
    return this.setConfig('sidebarPosition', position, ['left', 'right'], () => {
      if (position === 'right') {
        document.querySelector('.js-sidebar').classList.add('navbar-right');
      } else {
        document.querySelector('.js-sidebar').classList.remove('navbar-right');
      }

      this.setFormValue('sidebar-position', position);
    });
  };

  toggleSidebarSize(size) {
    return this.setConfig('sidebarSize', size, ['default', 'folded'], () => {
      if (size === 'folded') {
        document.querySelector('.js-sidebar').classList.add('navbar-folded');
      } else {
        document.querySelector('.js-sidebar').classList.remove('navbar-folded');
      }

      this.setFormValue('sidebar-size', size);
    });
  };

  toggleSidebarColor(color) {
C
codecalm 已提交
138
    console.log('color', color);
C
codecalm 已提交
139 140 141 142 143 144 145 146 147 148 149
    return this.setConfig('sidebarColor', color, ['dark', 'light'], () => {
      if (color === 'dark') {
        document.querySelector('.js-sidebar').classList.add('navbar-dark');
      } else {
        document.querySelector('.js-sidebar').classList.remove('navbar-dark');
      }

      this.setFormValue('sidebar-color', color);
    });
  };

C
codecalm 已提交
150 151 152 153 154 155 156 157 158 159 160 161
  toggleSidebarFixed(fixed) {
    return this.setConfig('sidebarFixed', fixed, ['fixed', 'default'], () => {
      if (fixed === 'fixed') {
        document.querySelector('.js-sidebar').classList.add('navbar-fixed');
      } else {
        document.querySelector('.js-sidebar').classList.remove('navbar-fixed');
      }

      this.setFormValue('sidebar-fixed', fixed);
    });
  };

C
codecalm 已提交
162 163 164 165 166 167 168 169 170 171 172
  toggleHeaderColor(color) {
    return this.setConfig('headerColor', color, ['dark', 'light'], () => {
      if (color === 'dark') {
        document.querySelector('.js-header').classList.add('navbar-dark');
      } else {
        document.querySelector('.js-header').classList.remove('navbar-dark');
      }

      this.setFormValue('header-color', color);
    });
  };
C
codecalm 已提交
173 174 175 176 177 178 179 180 181 182 183 184

  toggleHeaderFixed(fixed) {
    return this.setConfig('headerFixed', fixed, ['fixed', 'default'], () => {
      if (fixed === 'fixed') {
        document.querySelector('.js-header').classList.add('navbar-fixed');
      } else {
        document.querySelector('.js-header').classList.remove('navbar-fixed');
      }

      this.setFormValue('header-fixed', fixed);
    });
  };
C
codecalm 已提交
185 186 187 188 189 190 191 192 193 194 195
}

/*
Init demo
 */


(function () {
  const demo = new TablerDemo();
  window.DEMO = demo;
})();