1- import { Terminal } from 'xterm ' ;
2- import { FitAddon } from 'xterm-addon-fit ' ;
1+ import { GitSync } from './gitsync ' ;
2+ import { GitSyncView } from './gitsyncview ' ;
33import css from '../../../node_modules/xterm/css/xterm.css' ;
44
5- function GitSync ( baseUrl , repo , branch , depth , targetpath , path ) {
6- // Class that talks to the API backend & emits events as appropriate
7- this . baseUrl = baseUrl ;
8- this . repo = repo ;
9- this . branch = branch ;
10- this . depth = depth ;
11- this . targetpath = targetpath ;
12- this . redirectUrl = baseUrl + path ;
13-
14- this . callbacks = { } ;
15- }
16-
17- GitSync . prototype . addHandler = function ( event , cb ) {
18- if ( this . callbacks [ event ] == undefined ) {
19- this . callbacks [ event ] = [ cb ] ;
20- } else {
21- this . callbacks [ event ] . push ( cb ) ;
22- }
23- } ;
24-
25- GitSync . prototype . _emit = function ( event , data ) {
26- if ( this . callbacks [ event ] == undefined ) { return ; }
27- for ( let ev of this . callbacks [ event ] ) {
28- ev ( data ) ;
29- }
30- } ;
31-
32-
33- GitSync . prototype . start = function ( ) {
34- // Start git pulling handled by SyncHandler, declared in handlers.py
35- let syncUrlParams = new URLSearchParams ( {
36- repo : this . repo ,
37- targetpath : this . targetpath
38- } ) ;
39- if ( typeof this . depth !== 'undefined' && this . depth != undefined ) {
40- syncUrlParams . append ( 'depth' , this . depth ) ;
41- }
42- if ( typeof this . branch !== 'undefined' && this . branch != undefined ) {
43- syncUrlParams . append ( 'branch' , this . branch ) ;
44- }
45- var syncUrl = this . baseUrl + 'git-pull/api?' + syncUrlParams . toString ( ) ;
46-
47- this . eventSource = new EventSource ( syncUrl ) ;
48- var that = this ;
49- this . eventSource . addEventListener ( 'message' , function ( ev ) {
50- var data = JSON . parse ( ev . data ) ;
51- if ( data . phase == 'finished' || data . phase == 'error' ) {
52- that . eventSource . close ( ) ;
53- }
54- that . _emit ( data . phase , data ) ;
55- } ) ;
56- this . eventSource . addEventListener ( 'error' , function ( error ) {
57- console . log ( arguments ) ;
58- that . _emit ( 'error' , error ) ;
59- } ) ;
60- } ;
61-
62- function GitSyncView ( termSelector , progressSelector , termToggleSelector ) {
63- // Class that encapsulates view rendering as much as possible
64- this . term = new Terminal ( {
65- convertEol : true
66- } ) ;
67- this . fit = new FitAddon ( ) ;
68- this . term . loadAddon ( this . fit ) ;
69-
70- this . visible = false ;
71- this . progress = document . querySelector ( progressSelector ) ;
72-
73- this . termToggle = document . querySelector ( termToggleSelector ) ;
74- this . termElement = document . querySelector ( termSelector ) ;
75-
76- this . termToggle . onclick = ( ) => this . setTerminalVisibility ( ! this . visible )
77- }
78-
79- GitSyncView . prototype . setTerminalVisibility = function ( visible ) {
80- if ( visible ) {
81- this . termElement . parentElement . classList . remove ( 'hidden' ) ;
82- } else {
83- this . termElement . parentElement . classList . add ( 'hidden' ) ;
84- }
85- this . visible = visible ;
86- if ( visible ) {
87- // See https://github.com/jupyterhub/nbgitpuller/pull/46 on why this is here.
88- if ( ! this . term . element ) {
89- this . term . open ( this . termElement ) ;
90- }
91- this . fit . fit ( ) ;
92- }
93-
94- }
95-
96- GitSyncView . prototype . setProgressValue = function ( val ) {
97- this . progress . setAttribute ( 'aria-valuenow' , val ) ;
98- this . progress . style . width = val + '%' ;
99- } ;
100-
101- GitSyncView . prototype . getProgressValue = function ( ) {
102- return parseFloat ( this . progress . getAttribute ( 'aria-valuenow' ) ) ;
103- } ;
104-
105- GitSyncView . prototype . setProgressText = function ( text ) {
106- this . progress . querySelector ( 'span' ) . innerText = text ;
107- } ;
108-
109- GitSyncView . prototype . getProgressText = function ( ) {
110- return this . progress . querySelector ( 'span' ) . innerText ;
111- } ;
112-
113- GitSyncView . prototype . setProgressError = function ( isError ) {
114- if ( isError ) {
115- this . progress . classList . add ( 'progress-bar-danger' ) ;
116- } else {
117- this . progress . classList . remove ( 'progress-bar-danger' ) ;
118- }
119- } ;
120-
121- var get_body_data = function ( key ) {
5+ const getBodyData = ( key ) => {
1226 /**
1237 * get a url-encoded item from body.data and decode it
1248 * we should never have any encoded URLs anywhere else in code
@@ -131,16 +15,16 @@ var get_body_data = function(key) {
13115 return decodeURIComponent ( val ) ;
13216} ;
13317
134- var gs = new GitSync (
135- get_body_data ( 'base-url' ) ,
136- get_body_data ( 'repo' ) ,
137- get_body_data ( 'branch' ) ,
138- get_body_data ( 'depth' ) ,
139- get_body_data ( 'targetpath' ) ,
140- get_body_data ( 'path' )
18+ const gs = new GitSync (
19+ getBodyData ( 'base-url' ) ,
20+ getBodyData ( 'repo' ) ,
21+ getBodyData ( 'branch' ) ,
22+ getBodyData ( 'depth' ) ,
23+ getBodyData ( 'targetpath' ) ,
24+ getBodyData ( 'path' )
14125) ;
14226
143- var gsv = new GitSyncView (
27+ const gsv = new GitSyncView (
14428 '#status-details' ,
14529 '#status-panel-title' ,
14630 '#status-panel-toggle'
@@ -149,11 +33,11 @@ var gsv = new GitSyncView(
14933gs . addHandler ( 'syncing' , function ( data ) {
15034 gsv . term . write ( data . output ) ;
15135} ) ;
152- gs . addHandler ( 'finished' , function ( data ) {
36+ gs . addHandler ( 'finished' , function ( ) {
15337 progressTimers . forEach ( function ( timer ) { clearInterval ( timer ) ; } ) ;
15438 gsv . setProgressValue ( 100 ) ;
15539 gsv . setProgressText ( 'Sync finished, redirecting...' ) ;
156- window . location . href = gs . redirectUrl ;
40+ // window.location.href = gs.redirectUrl;
15741} ) ;
15842gs . addHandler ( 'error' , function ( data ) {
15943 progressTimers . forEach ( function ( timer ) { clearInterval ( timer ) ; } ) ;
@@ -168,7 +52,7 @@ gs.addHandler('error', function(data) {
16852gs . start ( ) ;
16953
17054// Make sure we provide plenty of appearances of progress!
171- var progressTimers = [ ] ;
55+ let progressTimers = [ ] ;
17256progressTimers . push ( setInterval ( function ( ) {
17357 gsv . setProgressText ( substatus_messages [ Math . floor ( Math . random ( ) * substatus_messages . length ) ] ) ;
17458} , 3000 ) ) ;
@@ -182,7 +66,7 @@ progressTimers.push(setInterval(function() {
18266} , 900 ) ) ;
18367
18468
185- var substatus_messages = [
69+ const substatus_messages = [
18670 "Adding Hidden Agendas" ,
18771 "Adjusting Bell Curves" ,
18872 "Aesthesizing Industrial Areas" ,
0 commit comments