diff --git a/controllers/preview-pane.js b/controllers/preview-pane.js new file mode 100644 index 000000000..b8d4115f5 --- /dev/null +++ b/controllers/preview-pane.js @@ -0,0 +1,56 @@ +const dom = require('@nymag/dom'), + _ = require('lodash'), + previewSizes = { + small: { w: 375, h: 660 }, + medium: { w: 768, h: 1024 }, + large: { w: 1024, h: 768 } + }; + +module.exports = function () { + function Constructor(el) { + // share elements + this.link = dom.find(el, '.share-input'); + this.button = dom.find(el, '.share-copy'); + } + + Constructor.prototype = { + events: { + '.preview-link click': 'onPreview', + '.share-copy click': 'onCopy', + }, + + onPreview: function (e) { + var link = e.currentTarget, + url = link.getAttribute('href'), + size = _.find(Object.keys(previewSizes), (s) => link.classList.contains(s)); + + e.preventDefault(); + window.open(url, `Preview${size}`, `resizable=yes,scrollbars=yes,width=${previewSizes[size].w},height=${previewSizes[size].h}`); + }, + + onCopy: function () { + var link = this.link, + button = this.button, + success; + + try { + link.select(); + success = document.execCommand('copy'); + + if (success) { + button.classList.remove('error'); + button.classList.add('success'); + } else { + button.classList.remove('success'); + button.classList.add('error'); + } + } catch (e) { + // some browsers can't do this. + button.classList.remove('success'); + button.classList.add('error'); + console.error(e.message, e.stack); + } + } + }; + return Constructor; +}; diff --git a/media/copy.svg b/media/copy.svg new file mode 100644 index 000000000..ae23b1f32 --- /dev/null +++ b/media/copy.svg @@ -0,0 +1,3 @@ + diff --git a/media/preview-large.svg b/media/preview-large.svg new file mode 100644 index 000000000..1f1c3d47e --- /dev/null +++ b/media/preview-large.svg @@ -0,0 +1,3 @@ + diff --git a/media/preview-medium.svg b/media/preview-medium.svg new file mode 100644 index 000000000..52d45d588 --- /dev/null +++ b/media/preview-medium.svg @@ -0,0 +1,3 @@ + diff --git a/media/preview-small.svg b/media/preview-small.svg new file mode 100644 index 000000000..ac54be792 --- /dev/null +++ b/media/preview-small.svg @@ -0,0 +1,3 @@ + diff --git a/services/pane.js b/services/pane.js index e2fbb25c9..01edbfc10 100644 --- a/services/pane.js +++ b/services/pane.js @@ -13,6 +13,7 @@ var _ = require('lodash'), paneController = require('../controllers/pane'), filterableList = require('./filterable-list'), publishPaneController = require('../controllers/publish-pane'), + previewController = require('../controllers/preview-pane'), references = require('./references'), addComponent = require('./components/add-component'), select = require('./components/select'), @@ -468,27 +469,31 @@ function addPreview(preview) { } /** - * open preview dialog pane + * open preview + share pane + * @returns {Element} */ function openPreview() { - var header = 'Preview Link', - innerEl = document.createDocumentFragment(), - previewUrl = edit.getPageUrl(), - pageActionsSubTemplate = tpl.get('.preview-actions-template'), - previewInput; - - if (pageActionsSubTemplate) { - previewInput = dom.find(pageActionsSubTemplate, '.preview-input'); - } + var pageUrl = edit.getPageUrl(), + previewHeader = 'Preview', + previewContent = tpl.get('.preview-actions-template'), + shareHeader = 'Shareable Link', + shareContent = tpl.get('.share-actions-template'), + el; + + // set the page url into the responsive preview items + _.each(dom.findAll(previewContent, 'a'), function (link) { + link.setAttribute('href', pageUrl); + }); - if (previewInput) { - previewInput.setAttribute('value', previewUrl); - } + // set the page url into the share tab + dom.find(shareContent, '.share-input').setAttribute('value', pageUrl); - // append actions to the doc fragment - innerEl.appendChild(pageActionsSubTemplate); - // create the root pane element - open([{header: header, content: innerEl}]); + el = open([{ header: previewHeader, content: previewContent }, { header: shareHeader, content: shareContent }]); + + // init controller + ds.controller('preview-pane', previewController); + ds.get('preview-pane', el); + return el; } /** diff --git a/services/pane.test.js b/services/pane.test.js index d92804923..aa5c34bf8 100644 --- a/services/pane.test.js +++ b/services/pane.test.js @@ -56,10 +56,36 @@ function stubCustomUrlTemplate() { } function stubPreviewActionsTemplate() { - return dom.create(`