LibsPage.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div></div>
  3. </template>
  4. <script>
  5. //-----------------------------------------------------------------------------
  6. import Vue from 'vue';
  7. import Component from 'vue-class-component';
  8. import Window from '../../share/Window.vue';
  9. import * as utils from '../../../share/utils';
  10. //import rstore from '../../../store/modules/reader';
  11. export default @Component({
  12. components: {
  13. Window
  14. },
  15. watch: {
  16. libs: function() {
  17. this.sendLibs();
  18. },
  19. }
  20. })
  21. class LibsPage extends Vue {
  22. created() {
  23. this.popupWindow = null;
  24. this.commit = this.$store.commit;
  25. //this.commit('reader/setLibs', rstore.libsDefaults);
  26. }
  27. init() {
  28. this.childReady = false;
  29. const subdomain = (window.location.protocol != 'http:' ? 'b.' : '');
  30. this.origin = `http://${subdomain}${window.location.host}`;
  31. this.messageListener = (event) => {
  32. if (event.origin !== this.origin)
  33. return;
  34. //console.log(event.data);
  35. this.recvMessage(event.data);
  36. };
  37. this.popupWindow = window.open(`${this.origin}/#/external-libs`);
  38. if (this.popupWindow) {
  39. window.addEventListener('message', this.messageListener);
  40. //Проверка закрытия окна
  41. (async() => {
  42. while(this.popupWindow) {
  43. if (this.popupWindow && this.popupWindow.closed)
  44. this.close();
  45. await utils.sleep(1000);
  46. }
  47. })();
  48. //Установление связи с окном
  49. (async() => {
  50. let i = 0;
  51. while(!this.childReady && this.popupWindow && i < 100) {
  52. this.sendMessage({type: 'mes', data: 'hello'});
  53. await utils.sleep(100);
  54. i++;
  55. }
  56. this.sendLibs();
  57. })();
  58. }
  59. }
  60. recvMessage(d) {
  61. if (d.type == 'mes') {
  62. switch(d.data) {
  63. case 'ready':
  64. this.childReady = true;
  65. break;
  66. }
  67. } else if (d.type == 'libs') {
  68. this.commit('reader/setLibs', d.data);
  69. } else if (d.type == 'close') {
  70. this.close();
  71. }
  72. }
  73. sendMessage(d) {
  74. if (this.popupWindow)
  75. this.popupWindow.postMessage(Object.assign({}, {from: 'LibsPage'}, d), this.origin);
  76. }
  77. done() {
  78. window.removeEventListener('message', this.messageListener);
  79. if (this.popupWindow) {
  80. this.popupWindow.close();
  81. this.popupWindow = null;
  82. }
  83. }
  84. get libs() {
  85. return this.$store.state.reader.libs;
  86. }
  87. sendLibs() {
  88. this.sendMessage({type: 'libs', data: this.libs});
  89. }
  90. /* submitUrl() {
  91. if (this.bookUrl) {
  92. this.$emit('load-book', {url: this.addProtocol(this.bookUrl), force: true});
  93. this.bookUrl = '';
  94. }
  95. }*/
  96. close() {
  97. this.$emit('libs-close');
  98. }
  99. }
  100. //-----------------------------------------------------------------------------
  101. </script>
  102. <style scoped>
  103. .separator {
  104. height: 1px;
  105. background-color: #A0A0A0;
  106. }
  107. </style>