UserHotKeys.vue 8.5 KB

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