setting-check.vue 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <template>
  2. <input type="checkbox" v-model="value" :disabled="disabled">
  3. </template>
  4. <script>
  5. import options from '../options';
  6. import hookSetting from '../hook-setting';
  7. export default {
  8. props: {
  9. name: String,
  10. disabled: Boolean,
  11. sync: {
  12. type: Boolean,
  13. default: true,
  14. },
  15. },
  16. data() {
  17. return {
  18. value: null,
  19. };
  20. },
  21. methods: {
  22. onChange(value) {
  23. // Maxthon is recognized as Chrome in Vue.js.
  24. // Due to vuejs/vue#4521, model is updated actually on click.
  25. // Normally `click` event should be fired before `change` event.
  26. // But Maxthon 4.4 sucks, `change` event is fired first, which breaks everything!
  27. // And this is fixed in later versions, so we watch the value instead of
  28. // listening to `change` event to keep the code consistent.
  29. if (this.sync) {
  30. options.set(this.name, value);
  31. }
  32. this.$emit('change', value);
  33. },
  34. },
  35. created() {
  36. options.ready(() => {
  37. this.value = options.get(this.name);
  38. this.revoke = hookSetting(this.name, (value) => {
  39. this.value = value;
  40. });
  41. this.$watch('value', this.onChange);
  42. });
  43. },
  44. beforeDestroy() {
  45. if (this.revoke) this.revoke();
  46. },
  47. };
  48. </script>