UserHotKeys.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <div class="table col column no-wrap">
  3. <!-- header -->
  4. <div class="table-row row">
  5. <div class="desc q-pa-sm bg-green-4">Действие</div>
  6. <div class="hotKeys col q-pa-sm bg-green-4 row no-wrap">
  7. <span>Сочетания клавиш</span>
  8. <q-input ref="input" outlined dense rounded bg-color="grey-4"
  9. placeholder="Найти"
  10. v-model="search"
  11. @click.stop
  12. />
  13. <div class="q-ml-sm column justify-center">
  14. <q-btn class="bg-grey-4 text-grey-6" style="height: 35px; width: 35px" rounded flat icon="la la-broom" @click="defaultHotKeyAll">
  15. <q-tooltip :delay="1000" anchor="top middle" self="bottom middle" content-style="font-size: 80%">
  16. Установить все сочетания по умолчанию
  17. </q-tooltip>
  18. </q-btn>
  19. </div>
  20. </div>
  21. </div>
  22. <!-- body -->
  23. <div class="table-row row" v-for="(action, index) in tableData" :key="index">
  24. <div class="desc q-pa-sm">{{ rstore.readerActions[action] }}</div>
  25. <div class="hotKeys col q-pa-sm">
  26. <q-chip
  27. :color="collisions[code] ? 'red' : 'grey-7'"
  28. removable :clickable="collisions[code] ? true : false"
  29. text-color="white" v-for="(code, index) in value[action]" :key="index" @remove="removeCode(action, code)"
  30. @click="collisionWarning(code)"
  31. >
  32. {{ code }}
  33. </q-chip>
  34. </div>
  35. <div class="column q-pa-xs">
  36. <q-icon
  37. name="la la-plus-circle"
  38. class="button bg-green-8 text-white"
  39. @click="addHotKey(action)"
  40. v-ripple
  41. >
  42. <q-tooltip :delay="1000" anchor="top middle" self="bottom middle" content-style="font-size: 80%">
  43. Добавить сочетание клавиш
  44. </q-tooltip>
  45. </q-icon>
  46. <q-icon
  47. name="la la-broom"
  48. class="button text-grey-5"
  49. @click="defaultHotKey(action)"
  50. v-ripple
  51. >
  52. <q-tooltip :delay="1000" anchor="top middle" self="bottom middle" content-style="font-size: 80%">
  53. По умолчанию
  54. </q-tooltip>
  55. </q-icon>
  56. </div>
  57. </div>
  58. </div>
  59. </template>
  60. <script>
  61. //-----------------------------------------------------------------------------
  62. import Vue from 'vue';
  63. import Component from 'vue-class-component';
  64. import rstore from '../../../../store/modules/reader';
  65. //import * as utils from '../../share/utils';
  66. const UserHotKeysProps = Vue.extend({
  67. props: {
  68. value: Object,
  69. //prop: { type: Number, default: 0 },
  70. }
  71. });
  72. export default @Component({
  73. watch: {
  74. search: function() {
  75. this.updateTableData();
  76. },
  77. value: function() {
  78. this.checkCollisions();
  79. this.updateTableData();
  80. }
  81. },
  82. })
  83. class UserHotKeys extends UserHotKeysProps {
  84. search = '';
  85. rstore = {};
  86. tableData = [];
  87. collisions = {};
  88. created() {
  89. this.rstore = rstore;
  90. }
  91. mounted() {
  92. this.checkCollisions();
  93. this.updateTableData();
  94. }
  95. updateTableData() {
  96. let result = rstore.hotKeys.map(hk => hk.name);
  97. const search = this.search.toLowerCase();
  98. const codesIncludeSearch = (action) => {
  99. for (const code of this.value[action]) {
  100. if (code.toLowerCase().includes(search))
  101. return true;
  102. }
  103. return false;
  104. };
  105. result = result.filter(item => {
  106. return !search ||
  107. rstore.readerActions[item].toLowerCase().includes(search) ||
  108. codesIncludeSearch(item)
  109. });
  110. this.tableData = result;
  111. }
  112. checkCollisions() {
  113. const cols = {};
  114. for (const [action, codes] of Object.entries(this.value)) {
  115. codes.forEach(code => {
  116. if (!cols[code])
  117. cols[code] = [];
  118. if (cols[code].indexOf(action) < 0)
  119. cols[code].push(action);
  120. });
  121. }
  122. const result = {};
  123. for (const [code, actions] of Object.entries(cols)) {
  124. if (actions.length > 1)
  125. result[code] = actions;
  126. }
  127. this.collisions = result;
  128. }
  129. collisionWarning(code) {
  130. if (this.collisions[code]) {
  131. const descs = this.collisions[code].map(action => `<b>${rstore.readerActions[action]}</b>`);
  132. this.$root.stdDialog.alert(`Сочетание '${code}' одновременно назначено<br>следующим действиям:<br>${descs.join('<br>')}`, 'Предупреждение');
  133. }
  134. }
  135. removeCode(action, code) {
  136. let codes = Array.from(this.value[action]);
  137. const index = codes.indexOf(code);
  138. if (index >= 0) {
  139. codes.splice(index, 1);
  140. const newValue = Object.assign({}, this.value, {[action]: codes});
  141. this.$emit('input', newValue);
  142. }
  143. }
  144. async addHotKey(action) {
  145. try {
  146. const result = await this.$root.stdDialog.getHotKey(`Добавить сочетание для:<br><b>${rstore.readerActions[action]}</b>`, '');
  147. if (result) {
  148. let codes = Array.from(this.value[action]);
  149. if (codes.indexOf(result) < 0) {
  150. codes.push(result);
  151. const newValue = Object.assign({}, this.value, {[action]: codes});
  152. this.$emit('input', newValue);
  153. this.$nextTick(() => {
  154. this.collisionWarning(result);
  155. });
  156. }
  157. }
  158. } catch (e) {
  159. //
  160. }
  161. }
  162. async defaultHotKey(action) {
  163. try {
  164. if (await this.$root.stdDialog.confirm(`Подтвердите установку клавиш по умолчанию для:<br><b>${rstore.readerActions[action]}</b>`, ' ')) {
  165. const codes = Array.from(rstore.settingDefaults.userHotKeys[action]);
  166. const newValue = Object.assign({}, this.value, {[action]: codes});
  167. this.$emit('input', newValue);
  168. }
  169. } catch (e) {
  170. //
  171. }
  172. }
  173. async defaultHotKeyAll() {
  174. try {
  175. if (await this.$root.stdDialog.confirm('Подтвердите установку ВСЕХ <br>сочетаний клавиш по умолчанию:', ' ')) {
  176. const newValue = Object.assign({}, rstore.settingDefaults.userHotKeys);
  177. this.$emit('input', newValue);
  178. }
  179. } catch (e) {
  180. //
  181. }
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. </script>
  186. <style scoped>
  187. .table {
  188. border-left: 1px solid grey;
  189. border-top: 1px solid grey;
  190. }
  191. .table-row {
  192. border-right: 1px solid grey;
  193. border-bottom: 1px solid grey;
  194. }
  195. .desc {
  196. width: 100px;
  197. }
  198. .hotKeys {
  199. border-left: 1px solid grey;
  200. }
  201. .button {
  202. font-size: 25px;
  203. border-radius: 25px;
  204. cursor: pointer;
  205. }
  206. </style>