-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mariusz Jurowicz
committed
Jan 30, 2018
1 parent
57f8b95
commit 4bb7087
Showing
8 changed files
with
199 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2017 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare interface Window { | ||
beakerx: any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2017 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Dialog } from '@jupyterlab/apputils'; | ||
import { Widget } from '@phosphor/widgets'; | ||
import { ServerConnection } from "@jupyterlab/services"; | ||
import { PageConfig } from "@jupyterlab/coreutils"; | ||
import gistPublishModalTemplate from './modalTemplate'; | ||
|
||
export default class GistPublishModal { | ||
private settingsUrl: string; | ||
private serverSettings: ServerConnection.ISettings; | ||
|
||
constructor() { | ||
this.serverSettings = ServerConnection.makeSettings(); | ||
this.settingsUrl = `${PageConfig.getBaseUrl()}beakerx/settings`; | ||
} | ||
|
||
show(submitCallback: Function): void { | ||
this.getGithubPersonalAccessToken() | ||
.then(personalAccessToken => { | ||
this.create(submitCallback, personalAccessToken); | ||
}); | ||
} | ||
|
||
create(submitCallback, personalAccessToken = ''): Promise<any> { | ||
const bodyWidget = this.createBodyWidget(); | ||
const personalAccessTokenInput = bodyWidget.node.querySelector('input'); | ||
const form = bodyWidget.node.querySelector('form'); | ||
const cancelButton = Dialog.cancelButton({ label: 'Cancel' }); | ||
const publishButton = Dialog.okButton({ label: 'Publish' }); | ||
|
||
const submitHandler = (result: Dialog.IResult<any>) => { | ||
if (result.button === cancelButton) { | ||
return; | ||
} | ||
|
||
submitCallback(result.value); | ||
this.storePersonalAccessToken(result.value); | ||
}; | ||
|
||
if (personalAccessTokenInput && form) { | ||
personalAccessTokenInput.value = personalAccessToken; | ||
} | ||
|
||
const modal = new Dialog({ | ||
title : 'Publish to a GitHub Gist', | ||
body: bodyWidget, | ||
defaultButton: 1, | ||
focusNodeSelector: 'input', | ||
buttons: [ cancelButton, publishButton ] | ||
}); | ||
|
||
return modal.launch().then(({ button }) => { | ||
submitHandler({ button, value: personalAccessTokenInput.value }); | ||
}); | ||
} | ||
|
||
createBodyWidget(): Widget { | ||
const modalContent = document.createElement('div'); | ||
|
||
modalContent.innerHTML = gistPublishModalTemplate; | ||
|
||
return new Widget({ node: modalContent }); | ||
} | ||
|
||
storePersonalAccessToken(githubPersonalAccessToken = ''): Promise<any> { | ||
return this.getStoredSettings() | ||
.then(storedSettings => | ||
ServerConnection.makeRequest( | ||
this.settingsUrl, | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
...storedSettings, | ||
githubPersonalAccessToken | ||
}) | ||
}, | ||
this.serverSettings | ||
).catch(reason => { console.log(reason); }) | ||
); | ||
} | ||
|
||
getGithubPersonalAccessToken(): Promise<any> { | ||
return this.getStoredSettings() | ||
.then(settings => settings.githubPersonalAccessToken || ''); | ||
} | ||
|
||
getStoredSettings(): Promise<any> { | ||
return ServerConnection.makeRequest( | ||
this.settingsUrl, | ||
{ method: 'GET' }, | ||
this.serverSettings | ||
) | ||
.then(response => response.json()) | ||
.catch(reason => { console.log(reason); }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2017 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export default ` | ||
<div> | ||
<p class="alert alert-primary bg-info"> | ||
<strong>Leave the Personal Access Token field empty to publish as an anonymous gist.</strong> | ||
<br /> | ||
<strong>Press enter or click the "Publish" button below. A | ||
window will open with the results, which you can share like | ||
any URL.</strong> | ||
</p> | ||
<form> | ||
<div class="form-group"> | ||
<label>Personal Access Token</label> | ||
<input type="password" class="form-control"> | ||
</div> | ||
</form> | ||
<p class="help-block"> | ||
<span>Enter a <a target="_blank" href="https://github.com/settings/tokens">Personal Access Token</a> to publish the notebook as a gist in your GitHub account.</span><br /> | ||
<span>We recommend your Personal Access Token have only the <strong><i>gists</i></strong> scope.</span><br /> | ||
<span>You can read about scopes <a href="https://developer.github.com/apps/building-oauth-apps/scopes-for-oauth-apps/">here</a></span> | ||
</p> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters