|
| 1 | +import { wantsNewWindow } from 'discourse/lib/intercept-click'; |
| 2 | +import { longDateNoYear } from 'discourse/lib/formatter'; |
| 3 | +import computed from 'ember-addons/ember-computed-decorators'; |
| 4 | +import Sharing from 'discourse/lib/sharing'; |
| 5 | + |
| 6 | +export default Ember.Component.extend({ |
| 7 | + elementId: 'share-link', |
| 8 | + classNameBindings: ['visible'], |
| 9 | + link: null, |
| 10 | + visible: null, |
| 11 | + |
| 12 | + @computed |
| 13 | + sources() { |
| 14 | + return Sharing.activeSources(this.siteSettings.share_links); |
| 15 | + }, |
| 16 | + |
| 17 | + @computed('type', 'postNumber') |
| 18 | + shareTitle(type, postNumber) { |
| 19 | + if (type === 'topic') { return I18n.t('share.topic'); } |
| 20 | + if (postNumber) { |
| 21 | + return I18n.t('share.post', { postNumber }); |
| 22 | + } |
| 23 | + return I18n.t('share.topic'); |
| 24 | + }, |
| 25 | + |
| 26 | + @computed('date') |
| 27 | + displayDate(date) { |
| 28 | + return longDateNoYear(new Date(date)); |
| 29 | + }, |
| 30 | + |
| 31 | + _focusUrl() { |
| 32 | + const link = this.get('link'); |
| 33 | + if (!this.capabilities.touch) { |
| 34 | + const $linkInput = $('#share-link input'); |
| 35 | + $linkInput.val(link); |
| 36 | + |
| 37 | + // Wait for the fade-in transition to finish before selecting the link: |
| 38 | + window.setTimeout(() => $linkInput.select().focus(), 160); |
| 39 | + } else { |
| 40 | + const $linkForTouch = $('#share-link .share-for-touch a'); |
| 41 | + $linkForTouch.attr('href', link); |
| 42 | + $linkForTouch.html(link); |
| 43 | + const range = window.document.createRange(); |
| 44 | + range.selectNode($linkForTouch[0]); |
| 45 | + window.getSelection().addRange(range); |
| 46 | + } |
| 47 | + }, |
| 48 | + |
| 49 | + _showUrl($target, url) { |
| 50 | + const $currentTargetOffset = $target.offset(); |
| 51 | + const $this = this.$(); |
| 52 | + |
| 53 | + if (Ember.isEmpty(url)) { return; } |
| 54 | + |
| 55 | + // Relative urls |
| 56 | + if (url.indexOf("/") === 0) { |
| 57 | + url = window.location.protocol + "//" + window.location.host + url; |
| 58 | + } |
| 59 | + |
| 60 | + const shareLinkWidth = $this.width(); |
| 61 | + let x = $currentTargetOffset.left - (shareLinkWidth / 2); |
| 62 | + if (x < 25) { x = 25; } |
| 63 | + if (x + shareLinkWidth > $(window).width()) { |
| 64 | + x -= shareLinkWidth / 2; |
| 65 | + } |
| 66 | + |
| 67 | + const header = $('.d-header'); |
| 68 | + let y = $currentTargetOffset.top - ($this.height() + 20); |
| 69 | + if (y < header.offset().top + header.height()) { |
| 70 | + y = $currentTargetOffset.top + 10; |
| 71 | + } |
| 72 | + |
| 73 | + $this.css({top: "" + y + "px"}); |
| 74 | + |
| 75 | + if (!this.site.mobileView) { |
| 76 | + $this.css({left: "" + x + "px"}); |
| 77 | + } |
| 78 | + this.set('link', url); |
| 79 | + this.set('visible', true); |
| 80 | + |
| 81 | + Ember.run.scheduleOnce('afterRender', this, this._focusUrl); |
| 82 | + }, |
| 83 | + |
| 84 | + didInsertElement() { |
| 85 | + this._super(); |
| 86 | + |
| 87 | + const $html = $('html'); |
| 88 | + $html.on('mousedown.outside-share-link', e => { |
| 89 | + // Use mousedown instead of click so this event is handled before routing occurs when a |
| 90 | + // link is clicked (which is a click event) while the share dialog is showing. |
| 91 | + if (this.$().has(e.target).length !== 0) { return; } |
| 92 | + this.send('close'); |
| 93 | + return true; |
| 94 | + }); |
| 95 | + |
| 96 | + $html.on('click.discoure-share-link', '[data-share-url]', e => { |
| 97 | + // if they want to open in a new tab, let it so |
| 98 | + if (wantsNewWindow(e)) { return true; } |
| 99 | + |
| 100 | + e.preventDefault(); |
| 101 | + |
| 102 | + const $currentTarget = $(e.currentTarget); |
| 103 | + const url = $currentTarget.data('share-url'); |
| 104 | + const postNumber = $currentTarget.data('post-number'); |
| 105 | + const postId = $currentTarget.closest('article').data('post-id'); |
| 106 | + const date = $currentTarget.children().data('time'); |
| 107 | + |
| 108 | + this.setProperties({ postNumber, date, postId }); |
| 109 | + this._showUrl($currentTarget, url); |
| 110 | + return false; |
| 111 | + }); |
| 112 | + |
| 113 | + $html.on('keydown.share-view', e => { |
| 114 | + if (e.keyCode === 27) { |
| 115 | + this.send('close'); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + this.appEvents.on('share:url', (url, $target) => this._showUrl($target, url)); |
| 120 | + }, |
| 121 | + |
| 122 | + willDestroyElement() { |
| 123 | + this._super(); |
| 124 | + $('html').off('click.discoure-share-link') |
| 125 | + .off('mousedown.outside-share-link') |
| 126 | + .off('keydown.share-view'); |
| 127 | + }, |
| 128 | + |
| 129 | + actions: { |
| 130 | + replyAsNewTopic() { |
| 131 | + const postStream = this.get("topic.postStream"); |
| 132 | + const postId = this.get("postId") || postStream.findPostIdForPostNumber(1); |
| 133 | + const post = postStream.findLoadedPost(postId); |
| 134 | + this.sendAction('replyAsNewTopic', post); |
| 135 | + this.send("close"); |
| 136 | + }, |
| 137 | + |
| 138 | + close() { |
| 139 | + this.setProperties({ |
| 140 | + link: null, |
| 141 | + postNumber: null, |
| 142 | + postId: null, |
| 143 | + visible: false |
| 144 | + }); |
| 145 | + }, |
| 146 | + |
| 147 | + share(source) { |
| 148 | + var url = source.generateUrl(this.get('link'), this.get('title')); |
| 149 | + if (source.shouldOpenInPopup) { |
| 150 | + window.open(url, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=600,height=' + (source.popupHeight || 315)); |
| 151 | + } else { |
| 152 | + window.open(url, '_blank'); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | +}); |
0 commit comments