-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Get rid of var - Use class syntax rather than prototype syntax for classes - Remove unused param - Split classes to their own files
- Loading branch information
Showing
3 changed files
with
134 additions
and
131 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
export class GitSync { | ||
constructor(baseUrl, repo, branch, depth, targetpath, path) { | ||
// Class that talks to the API backend & emits events as appropriate | ||
this.baseUrl = baseUrl; | ||
this.repo = repo; | ||
this.branch = branch; | ||
this.depth = depth; | ||
this.targetpath = targetpath; | ||
this.redirectUrl = baseUrl + path; | ||
|
||
this.callbacks = {}; | ||
} | ||
|
||
addHandler(event, cb) { | ||
if (this.callbacks[event] == undefined) { | ||
this.callbacks[event] = [cb]; | ||
} else { | ||
this.callbacks[event].push(cb); | ||
} | ||
} | ||
|
||
_emit(event, data) { | ||
if (this.callbacks[event] == undefined) { return; } | ||
for(let ev of this.callbacks[event]) { | ||
ev(data); | ||
} | ||
|
||
} | ||
|
||
start() { | ||
// Start git pulling handled by SyncHandler, declared in handlers.py | ||
let syncUrlParams = new URLSearchParams({ | ||
repo: this.repo, | ||
targetpath: this.targetpath | ||
}); | ||
if (typeof this.depth !== 'undefined' && this.depth != undefined) { | ||
syncUrlParams.append('depth', this.depth); | ||
} | ||
if (typeof this.branch !== 'undefined' && this.branch != undefined) { | ||
syncUrlParams.append('branch', this.branch); | ||
} | ||
const syncUrl = this.baseUrl + 'git-pull/api?' + syncUrlParams.toString(); | ||
|
||
this.eventSource = new EventSource(syncUrl); | ||
this.eventSource.addEventListener('message', (ev) => { | ||
const data = JSON.parse(ev.data); | ||
if (data.phase == 'finished' || data.phase == 'error') { | ||
this.eventSource.close(); | ||
} | ||
this._emit(data.phase, data); | ||
}); | ||
this.eventSource.addEventListener('error',(error) => { | ||
this._emit('error', error); | ||
}); | ||
} | ||
} |
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,63 @@ | ||
|
||
import { Terminal } from 'xterm'; | ||
import { FitAddon } from 'xterm-addon-fit'; | ||
|
||
export class GitSyncView{ | ||
constructor(termSelector, progressSelector, termToggleSelector) { | ||
// Class that encapsulates view rendering as much as possible | ||
this.term = new Terminal({ | ||
convertEol: true | ||
}); | ||
this.fit = new FitAddon(); | ||
this.term.loadAddon(this.fit); | ||
|
||
this.visible = false; | ||
this.progress = document.querySelector(progressSelector); | ||
|
||
this.termToggle = document.querySelector(termToggleSelector); | ||
this.termElement = document.querySelector(termSelector); | ||
|
||
this.termToggle.onclick = () => this.setTerminalVisibility(!this.visible) | ||
} | ||
|
||
setTerminalVisibility(visible) { | ||
if (visible) { | ||
this.termElement.parentElement.classList.remove('hidden'); | ||
} else { | ||
this.termElement.parentElement.classList.add('hidden'); | ||
} | ||
this.visible = visible; | ||
if (visible) { | ||
// See https://github.com/jupyterhub/nbgitpuller/pull/46 on why this is here. | ||
if (!this.term.element) { | ||
this.term.open(this.termElement); | ||
} | ||
this.fit.fit(); | ||
} | ||
} | ||
|
||
setProgressValue(val) { | ||
this.progress.setAttribute('aria-valuenow', val); | ||
this.progress.style.width = val + '%'; | ||
} | ||
|
||
getProgressValue() { | ||
return parseFloat(this.progress.getAttribute('aria-valuenow')); | ||
} | ||
|
||
setProgressText(text) { | ||
this.progress.querySelector('span').innerText = text; | ||
} | ||
|
||
getProgressText() { | ||
return this.progress.querySelector('span').innerText; | ||
} | ||
|
||
setProgressError(isError) { | ||
if (isError) { | ||
this.progress.classList.add('progress-bar-danger'); | ||
} else { | ||
this.progress.classList.remove('progress-bar-danger'); | ||
} | ||
} | ||
} |
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