UserHotKeys.vue 7.7 KB

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