Skip to content

Commit 083dcd7

Browse files
committed
fix multi tab
1 parent fb6c008 commit 083dcd7

File tree

6 files changed

+104
-106
lines changed

6 files changed

+104
-106
lines changed

src/gas-hub.js

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ function initContext() {
105105
context.bindType = item.bindType || {};
106106
context.bindPattern = item.bindPattern || {};
107107
context.bindConfig = item.bindConfig || {};
108-
context.config = context.bindConfig[id] || {};
109-
context.config.filetype = context.config.filetype || context.bindType[id] || '.gs';
110-
context.config.ignorePattern = context.config.ignorePattern || context.bindPattern[id] || [];
111-
context.config.manifestEnabled = context.config.manifestEnabled || false;
112-
context.gist = context.bindRepo[id] && context.bindRepo[id].gist;
113108
resolve(scm);
114109
});
115110
})
@@ -125,7 +120,7 @@ function initContext() {
125120
$('#repo-owner-list').append(content);
126121
if (first) {
127122
$('#selected-repo-owner').text(owner);
128-
first =false;
123+
first = false;
129124
}
130125
});
131126
return scm;
@@ -254,9 +249,19 @@ function initPageEvent() {
254249
})
255250

256251
$(document).on('click', '#config-button', () => {
257-
$('#filetype').text(context.config.filetype);
258-
$('#manage-manifest').prop("checked", context.config.manifestEnabled);
259-
$('#ignore-pattern').val(context.config.ignorePattern.join(';'));
252+
let config = getConfig();
253+
if (!config) {
254+
config = {
255+
filetype: '.gs',
256+
ignorePattern: [],
257+
manifestEnabled: true,
258+
}
259+
}
260+
if (config) {
261+
$('#selected-suffix').text(config.filetype);
262+
$('#manage-manifest').prop("checked", config.manifestEnabled);
263+
$('#ignore-pattern').val(config.ignorePattern.join(';'));
264+
}
260265
changeModalState('config', true);
261266
});
262267

@@ -274,10 +279,12 @@ function initPageEvent() {
274279
})
275280

276281
$(document).on('click', '#save-config', () => {
277-
context.config.filetype = $('#filetype').text();
278-
context.config.manifestEnabled = $('#manage-manifest').prop("checked");
279-
context.config.ignorePattern = $('#ignore-pattern').val().split(';').filter(p => p !== '');
280-
context.bindConfig[getId()] = context.config;
282+
const config = {
283+
filetype: $('#selected-suffix').text(),
284+
manifestEnabled: $('#manage-manifest').prop("checked"),
285+
ignorePattern: $('#ignore-pattern').val().split(';').filter(p => p !== '')
286+
}
287+
context.bindConfig[getId()] = config;
281288
try {
282289
chrome.storage.sync.set({
283290
bindConfig: context.bindConfig
@@ -298,25 +305,22 @@ function initPageEvent() {
298305
let label;
299306
switch (type) {
300307
case 'repo':
301-
if (getRepo() && target.attr('data') === context.repo.fullName) return;
302-
//update context.repo with name and fullName
308+
if (getRepo() && target.attr('data') === getRepo().fullName) return;
303309
const fullName = target.attr('data');
304310
content = {
305311
fullName: fullName,
306312
gist: fullName === 'gist'
307313
}
308314
label = fullName;
309-
context.gist = content.gist;
310315
break;
311316
case 'branch':
312-
if (context[type] && target.text() === context[type]) return;
317+
if (getBranch() && target.text() === getBranch()) return;
313318
content = target.attr('data');
314319
label = target.attr('data');
315320
break;
316321
default:
317322
return;
318323
}
319-
context[type] = content;
320324
const bindName = `bind${type.capitalize()}`;
321325
Object.assign(context[bindName], {
322326
[getId()]: content
@@ -378,14 +382,15 @@ function showDiff(code, type) {
378382
})
379383
.concat(gasFiles)
380384
.filter(file => {
381-
if (context.config.manifestEnabled && file === 'appsscript.json') {
385+
const config = getConfig();
386+
if (config.manifestEnabled && file === 'appsscript.json') {
382387
return true;
383388
}
384-
for (let i = 0; i < context.config.ignorePattern.length; i++) {
385-
let p = new RegExp(context.config.ignorePattern[i]);
386-
if (p.test(file)) return false;
389+
for (let i = 0; i < config.ignorePattern.length; i++) {
390+
let p = new RegExp(svconfig.ignorePattern[i]);
391+
if (svp.test(file)) return false;
387392
}
388-
const regex = new RegExp(`(.*?)(${context.config.filetype}|\.html)$`)
393+
const regex = new RegExp(`(.*?)(${context.bindConfig[getId()].filetype}|\.html)$`)
389394
const match = file.match(regex);
390395
return match && match[1] && match[2];
391396
})
@@ -434,7 +439,7 @@ function showDiff(code, type) {
434439
$('#scm-diff-handler').prop('disabled', true);
435440
$('.scm-comment').hide();
436441
} else {
437-
if (type === 'push' && !context.gist) { //push to repo must have commit comment
442+
if (type === 'push' && !isGist()) { //push to repo must have commit comment
438443
$('.scm-comment').show();
439444
$('.gist-desc').hide();
440445
$('#scm-diff-handler').prop('disabled', true);
@@ -472,18 +477,16 @@ function updateRepo(repos) {
472477
$('.repo-menu').append(content);
473478
});
474479
showLog("Repository updated");
475-
if (context.repo) {
476-
$('#scm-bind-repo').text(context.repo.fullName);
477-
478-
//highlight current repo in repos list
479-
// $(`[data="${context.repo.fullName}"]`).css('background-color', 'lightgray');
480-
return context.repo.fullName;
480+
const repo = getRepo();
481+
if (repo) {
482+
$('#scm-bind-repo').text(repo.fullName);
483+
return repo.fullName;
481484
}
482485
return null;
483486
}
484487

485488
function updateGist() {
486-
if (!context.gist) {
489+
if (!isGist()) {
487490
return null;
488491
}
489492
return scm.getAllGists()
@@ -500,7 +503,6 @@ function updateGist() {
500503
}
501504
$('#scm-bind-branch').text(gist);
502505
//update context and storage
503-
context.branch = gist;
504506
Object.assign(context.bindBranch, {
505507
[getId()]: gist
506508
});
@@ -512,7 +514,7 @@ function updateGist() {
512514
}
513515

514516
function updateBranch() {
515-
if (!context.repo || context.gist) {
517+
if (!getRepo() || isGist()) {
516518
return null;
517519
}
518520
return scm.getAllBranches()
@@ -535,7 +537,6 @@ function updateBranch() {
535537
}
536538
$('#scm-bind-branch').text(branch);
537539
//update context and storage
538-
context.branch = branch;
539540
Object.assign(context.bindBranch, {
540541
[getId()]: branch
541542
});

src/gas/script-api.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const API_URL = "https://script.googleapis.com/v1/projects";
44

55
class ScriptApi {
66
pull(code) {
7+
const config = getConfig();
78
const changed = $('.diff-file:checked').toArray().map(elem => elem.value);
89
const updatedFiles = changed.filter(f => code.scm[f]).map(f => {
910
const suffix = f.substr(f.lastIndexOf("."));
@@ -16,7 +17,7 @@ class ScriptApi {
1617
case ".json":
1718
type = "JSON";
1819
break;
19-
case context.config.filetype:
20+
case config.filetype:
2021
type = "SERVER_JS";
2122
break;
2223
}
@@ -40,7 +41,7 @@ class ScriptApi {
4041
case ".json":
4142
type = "JSON";
4243
break;
43-
case context.config.filetype:
44+
case config.filetype:
4445
type = "SERVER_JS";
4546
break;
4647
}
@@ -102,7 +103,7 @@ class ScriptApi {
102103
type = ".json"
103104
break;
104105
case "SERVER_JS":
105-
type = context.config.filetype
106+
type = getConfig().filetype
106107
break;
107108
}
108109
hash[elem.name + type] = elem.source;

src/scm/bitbucket.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ class Bitbucket {
8989
});
9090
const deleteFiles = changed.filter(f => !code.gas[f]);
9191
const comment = $('#commit-comment').val();
92-
93-
this.commitFiles(context.repo.fullName, context.branch, null, files, deleteFiles, comment)
92+
const repo = getRepo();
93+
this.commitFiles(repo.fullName, getBranch(), null, files, deleteFiles, comment)
9494
.then(() => {
95-
showLog(`Successfully push to ${context.branch} of ${context.repo.fullName}`);
95+
showLog(`Successfully push to ${getBranch()} of ${repo.fullName}`);
9696
})
9797
.catch((err) => {
9898
showLog('Failed to push', LEVEL_ERROR);
@@ -105,7 +105,7 @@ class Bitbucket {
105105
return getAllItems(Promise.resolve({
106106
token: accessToken,
107107
items: [],
108-
url: `${this.baseUrl}/repositories/${context.repo.fullName}/refs/branches?access_token=${accessToken}`
108+
url: `${this.baseUrl}/repositories/${getRepo().fullName}/refs/branches?access_token=${accessToken}`
109109
}),
110110
this.followPaginate,
111111
'bitbucket'
@@ -117,7 +117,7 @@ class Bitbucket {
117117
return this.getAccessToken()
118118
.then(accessToken => {
119119
return $.getJSON(
120-
`${this.baseUrl}/repositories/${context.repo.fullName}/refs/branches/${context.branch}`, {
120+
`${this.baseUrl}/repositories/${getRepo().fullName}/refs/branches/${getBranch()}`, {
121121
access_token: accessToken
122122
}
123123
)
@@ -127,7 +127,7 @@ class Bitbucket {
127127
token: this.accessToken,
128128
items: [],
129129
urls: [],
130-
url: `${this.baseUrl}/repositories/${context.repo.fullName}/src/${response.target.hash}/?access_token=${this.accessToken}`
130+
url: `${this.baseUrl}/repositories/${getRepo().fullName}/src/${response.target.hash}/?access_token=${this.accessToken}`
131131
}),
132132
this.followDirectory,
133133
'bitbucket'
@@ -194,13 +194,7 @@ class Bitbucket {
194194
);
195195
})
196196
.then(response => {
197-
const repos = response.map(repo => repo.full_name);
198-
//if current bind still existed, use it
199-
const repo = context.bindRepo[getId()];
200-
if (repo && $.inArray(repo.fullName, repos) >= 0) {
201-
context.repo = repo;
202-
}
203-
return repos;
197+
return response.map(repo => repo.full_name);
204198
});
205199
}
206200

@@ -233,7 +227,6 @@ class Bitbucket {
233227
const repo = {
234228
fullName: response.full_name
235229
};
236-
context.repo = repo;
237230
Object.assign(context.bindRepo, {
238231
[getId()]: repo
239232
});
@@ -265,17 +258,16 @@ class Bitbucket {
265258
return this.getAccessToken()
266259
.then(() => {
267260
return $.getJSON(
268-
`${this.baseUrl}/repositories/${context.repo.fullName}/refs/branches/${context.branch}`, {
261+
`${this.baseUrl}/repositories/${getRepo().fullName}/refs/branches/${getBranch()}`, {
269262
access_token: this.accessToken
270263
}
271264
);
272265
})
273266
.then(res => {
274267
const parent = res.target ? res.target.hash : null;
275-
return this.commitFiles(context.repo.fullName, branch, parent, [], null, `create new branch ${branch}`);
268+
return this.commitFiles(getRepo().fullName, branch, parent, [], null, `create new branch ${branch}`);
276269
})
277270
.then(() => {
278-
context.branch = branch;
279271
Object.assign(context.bindBranch, {
280272
[getId()]: branch
281273
});
@@ -317,7 +309,8 @@ class Bitbucket {
317309
}).map(dir => {
318310
return `${dir.links.self.href}?access_token=${data.token}`;
319311
})
320-
const re = new RegExp(`(\\${context.config.filetype}|\\.html${context.config.manifestEnabled ? '|^appsscript.json' : ''})$`);
312+
const config = getConfig();
313+
const re = new RegExp(`(\\${config.filetype}|\\.html${config.manifestEnabled ? '|^appsscript.json' : ''})$`);
321314
const files = response.values.filter(src => {
322315
return src.type === 'commit_file' && re.test(src.path);
323316
});

0 commit comments

Comments
 (0)