Skip to content

Commit

Permalink
[WIP] Add http to copied url and move function to componentWillReceiv…
Browse files Browse the repository at this point in the history
…eProps (#1780)

* Add http to copied url and move function to componentWillReceiveProps

* Added getText() to CopyToClipbaord to enable ajax calls for getting copy text

* Set ajax call to synchronous (document.execCommand only works in synchronous mode
  • Loading branch information
vera-liu authored Dec 7, 2016
1 parent c155857 commit 74edb93
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
19 changes: 3 additions & 16 deletions superset/assets/javascripts/SqlLab/components/CopyQueryTabUrl.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ const propTypes = {
};

export default class CopyQueryTabUrl extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
shortUrl: '',
};
}

componentWillMount() {
getUrl(callback) {
const qe = this.props.queryEditor;
const params = [];
if (qe.dbId) params.push('dbid=' + qe.dbId);
Expand All @@ -25,27 +18,21 @@ export default class CopyQueryTabUrl extends React.PureComponent {

const queryString = params.join('&');
const queryLink = window.location.pathname + '?' + queryString;
getShortUrl(queryLink, this.onShortUrlSuccess.bind(this));
}

onShortUrlSuccess(data) {
this.setState({
shortUrl: data,
});
getShortUrl(queryLink, callback);
}

render() {
return (
<CopyToClipboard
inMenu
text={this.state.shortUrl}
copyNode={(
<div>
<i className="fa fa-clipboard" /> <span>share query</span>
</div>
)}
tooltipText="copy URL to clipboard"
shouldShowText={false}
getText={this.getUrl.bind(this)}
/>
);
}
Expand Down
19 changes: 13 additions & 6 deletions superset/assets/javascripts/components/CopyToClipboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { Tooltip, OverlayTrigger, MenuItem } from 'react-bootstrap';

const propTypes = {
copyNode: PropTypes.node,
getText: PropTypes.func,
onCopyEnd: PropTypes.func,
shouldShowText: PropTypes.bool,
text: PropTypes.string.isRequired,
text: PropTypes.string,
inMenu: PropTypes.bool,
tooltipText: PropTypes.string,
};
Expand Down Expand Up @@ -36,12 +37,19 @@ export default class CopyToClipboard extends React.Component {
setTimeout(this.resetTooltipText, 200);
}

onClick() {
if (this.props.getText) {
this.props.getText((d) => { this.copyToClipboard(d); });
} else {
this.copyToClipboard(this.props.text);
}
}

resetTooltipText() {
this.setState({ hasCopied: false });
}

copyToClipboard() {
const textToCopy = this.props.text;
copyToClipboard(textToCopy) {
const textArea = document.createElement('textarea');

textArea.style.position = 'fixed';
Expand All @@ -50,7 +58,6 @@ export default class CopyToClipboard extends React.Component {

document.body.appendChild(textArea);
textArea.select();

try {
if (!document.execCommand('copy')) {
throw new Error('Not successful');
Expand Down Expand Up @@ -87,7 +94,7 @@ export default class CopyToClipboard extends React.Component {
overlay={this.renderTooltip()}
trigger={['hover']}
bsStyle="link"
onClick={this.copyToClipboard}
onClick={this.onClick.bind(this)}
onMouseOut={this.onMouseOut}
>
{this.props.copyNode}
Expand All @@ -101,7 +108,7 @@ export default class CopyToClipboard extends React.Component {
<OverlayTrigger placement="top" overlay={this.renderTooltip()} trigger={['hover']}>
<MenuItem>
<span
onClick={this.copyToClipboard}
onClick={this.onClick.bind(this)}
onMouseOut={this.onMouseOut}
>
{this.props.copyNode}
Expand Down
6 changes: 4 additions & 2 deletions superset/assets/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ export function getParamsFromUrl() {
return newParams;
}

export function getShortUrl(longUrl, callBack) {
export function getShortUrl(longUrl, callback) {
$.ajax({
type: 'POST',
url: '/r/shortner/',
async: false,
data: {
data: '/' + longUrl,
},
success: (data) => {
callBack(data);
callback(data);
},
error: (error) => {
/* eslint no-console: 0 */
if (console && console.warn) {
console.warn('Something went wrong...');
console.warn(error);
}
callback(longUrl);
},
});
}
2 changes: 1 addition & 1 deletion superset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ def shortner(self):
obj = models.Url(url=url)
db.session.add(obj)
db.session.commit()
return("{request.headers[Host]}/r/{obj.id}".format(
return("http://{request.headers[Host]}/r/{obj.id}".format(
request=request, obj=obj))

@expose("/msg/")
Expand Down

0 comments on commit 74edb93

Please sign in to comment.