UserHotKeys.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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>')}<br><br>
  135. Возможно неожиданное поведение.`, 'Предупреждение');
  136. }
  137. }
  138. removeCode(action, code) {
  139. let codes = Array.from(this.value[action]);
  140. const index = codes.indexOf(code);
  141. if (index >= 0) {
  142. codes.splice(index, 1);
  143. const newValue = Object.assign({}, this.value, {[action]: codes});
  144. this.$emit('input', newValue);
  145. }
  146. }
  147. async addHotKey(action) {
  148. if (this.value[action].length >= this.maxCodesLength)
  149. return;
  150. try {
  151. const result = await this.$root.stdDialog.getHotKey(`Добавить сочетание для:<br><b>${rstore.readerActions[action]}</b>`, '');
  152. if (result) {
  153. let codes = Array.from(this.value[action]);
  154. if (codes.indexOf(result) < 0) {
  155. codes.push(result);
  156. const newValue = Object.assign({}, this.value, {[action]: codes});
  157. this.$emit('input', newValue);
  158. this.$nextTick(() => {
  159. this.collisionWarning(result);
  160. });
  161. }
  162. }
  163. } catch (e) {
  164. //
  165. }
  166. }
  167. async defaultHotKey(action) {
  168. try {
  169. if (await this.$root.stdDialog.confirm(`Подтвердите сброс сочетаний клавиш<br>в значения по умолчанию для команды:<br><b>${rstore.readerActions[action]}</b>`, ' ')) {
  170. const codes = Array.from(rstore.settingDefaults.userHotKeys[action]);
  171. const newValue = Object.assign({}, this.value, {[action]: codes});
  172. this.$emit('input', newValue);
  173. }
  174. } catch (e) {
  175. //
  176. }
  177. }
  178. async defaultHotKeyAll() {
  179. try {
  180. if (await this.$root.stdDialog.confirm('Подтвердите сброс сочетаний клавиш<br>для ВСЕХ команд в значения по умолчанию:', ' ')) {
  181. const newValue = Object.assign({}, rstore.settingDefaults.userHotKeys);
  182. this.$emit('input', newValue);
  183. }
  184. } catch (e) {
  185. //
  186. }
  187. }
  188. }
  189. //-----------------------------------------------------------------------------
  190. </script>
  191. <style scoped>
  192. .table {
  193. border-left: 1px solid grey;
  194. border-top: 1px solid grey;
  195. }
  196. .table-row {
  197. border-right: 1px solid grey;
  198. border-bottom: 1px solid grey;
  199. }
  200. .table-row:nth-child(even) {
  201. background-color: #f7f7f7;
  202. }
  203. .table-row:hover {
  204. background-color: #f0f0f0;
  205. }
  206. .desc {
  207. width: 130px;
  208. overflow-wrap: break-word;
  209. word-wrap: break-word;
  210. white-space: normal;
  211. }
  212. .hotKeys {
  213. border-left: 1px solid grey;
  214. }
  215. .button {
  216. font-size: 25px;
  217. border-radius: 25px;
  218. cursor: pointer;
  219. }
  220. </style>