Skip to content

Commit

Permalink
Disentangle JS modules and fix Flow (fixes #80)
Browse files Browse the repository at this point in the history
* [WIP] Disentangle JS modules (#225)

* Disentangle JS modules

* Fix imports

* Use flow on individual files

* Declare modules to Flow

* Run Flow directly

* Cleanup

* Fix some import/export issues

* Make the build work

* Tweak flow decl

* Move around the config decl to make build work
  • Loading branch information
sushain97 authored Dec 16, 2017
1 parent 780e615 commit 9f4129d
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 212 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

# Install development dependencies
- run: sudo apt-get install python3 python3-pip
- run: sudo npm install -g jsonlint eslint htmlhint sass-lint
- run: sudo npm install -g jsonlint eslint flow flow-bin htmlhint sass-lint
- run: sudo pip3 install flake8

# Run tests
Expand All @@ -17,6 +17,7 @@ jobs:
- run: htmlhint --config .htmlhintrc.json *.html
- run: sass-lint --config assets/css/.sass-lint.yml --verbose --no-exit --max-warnings 0
- run: flake8 --config tools/.flake8.ini *.py **/**.py **/**/*.py
- run: flow check

# Build project
- run: cp config.conf.example config.conf
Expand Down
2 changes: 1 addition & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[ignore]
build/
.*/build/.*

[include]

Expand Down
12 changes: 4 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ debug: debugjs debugcss build/index.debug.html build/not-found.html fonts build/
prod: js css html fonts build/sitemap.xml build/manifest.json build/strings/locales.json localhtml images

js: build/js/min.js build/js/compat.js build/js/jquery.min.js build/js/bootstrap.min.js debugjs
debugjs: build/js/jquery.jsonp-2.4.0.min.js build/js/config.js build/js/util.js build/js/store.js build/js/persistence.js build/js/localization.js build/js/translator.js build/js/analyzer.js build/js/generator.js build/js/sandbox.js
debugjs: build/js/jquery.jsonp-2.4.0.min.js build/js/config.js build/js/util.js build/js/init.js build/js/store.js build/js/persistence.js build/js/localization.js build/js/translator.js build/js/analyzer.js build/js/generator.js build/js/sandbox.js
css: build/css/min.css build/css/font-awesome.min.css build/css/bootstrap-rtl.min.css debugcss
debugcss: build/css/bootstrap.css build/css/style.css
html: build/index.html build/index.debug.html build/not-found.html
Expand All @@ -31,12 +31,12 @@ check-deps:
### JS ###
JSFILES= \
assets/js/strict.js \
assets/js/flow.js \
assets/js/jquery.jsonp-2.4.0.min.js \
build/js/config.js \
build/js/locales.js \
build/js/listrequests.js \
assets/js/util.js \
assets/js/init.js \
assets/js/store.js \
assets/js/persistence.js \
assets/js/localization.js \
Expand Down Expand Up @@ -102,7 +102,7 @@ build/js/%.js: assets/js/%.js build/js/.d
### MANIFEST ###
build/manifest.json: assets/manifest.json build/.d
cp $< $@

### HTML ###
build/index.debug.html: index.html.in debug-head.html build/l10n-rel.html build/.PIWIK_URL build/.PIWIK_SITEID build/strings/eng.json $(CONFIG) tools/read-conf.py tools/localise-html.py build/.d
sed -e '/@include_head@/r debug-head.html' -e '/@include_head@/r build/l10n-rel.html' -e '/@include_head@/d' -e "s%@include_piwik_url@%$(shell cat build/.PIWIK_URL)%" -e "s%@include_piwik_siteid@%$(shell cat build/.PIWIK_SITEID)%" $< > $@
Expand Down Expand Up @@ -226,10 +226,6 @@ build/img/%: assets/img/%

images: $(IMAGES_BUILD)

### Typechecking ###
# grab the bin from /~https://github.com/facebook/flow/releases
flow: build/js/all.js
grep -Ev '/\*:: *(ex|im)port ' $< | flow check-contents

### Test server ###
server:
Expand All @@ -240,4 +236,4 @@ server:
### Clean ###
clean:
rm -rf build/

6 changes: 6 additions & 0 deletions assets/js/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,9 @@ function formatUnit(unit) {
function handleAnalyzeErrorResponse(xOptions, error) {
$('#morphAnalyzerOutput').text(error).removeClass('blurred');
}

/*:: import {config} from "./config.js" */
/*:: import {modeEnabled, ajaxSend, ajaxComplete, filterLangList, allowedLang, sendEvent, callApy, apyRequestTimeout} from "./util.js" */
/*:: import {ENTER_KEY_CODE} from "./util.js" */
/*:: import {localizeInterface, getLangByCode} from "./localization.js" */
/*:: import {persistChoices, restoreChoices, readCache, cache} from "./persistence.js" */
181 changes: 181 additions & 0 deletions assets/js/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/* global config, persistChoices, iso639Codes, iso639CodesInverse, populateTranslationList, showTranslateWebpageInterface */
/* global ajaxSend, ajaxComplete, debounce, resizeFooter, synchronizeTextareaHeights */

var BACK_TO_TOP_BUTTON_ACTIVATION_HEIGHT = 300;

$(document).ajaxSend(ajaxSend);
$(document).ajaxComplete(ajaxComplete);
$(document).ajaxError(ajaxComplete);

$.jsonp.setup({
callbackParameter: 'callback'
});

$(document).ready(function () {
$('#noscript').hide();
$('.navbar').css('margin-top', '0px');
$('body > .container').css('margin-top', '0px');

if(config.SUBTITLE) {
$('.apertiumSubLogo')
.text(config.SUBTITLE)
.show();
if(config.SUBTITLE_COLOR) {
$('.apertiumSubLogo').css('color', config.SUBTITLE_COLOR);
}
}
else {
$('.apertiumSubLogo').hide();
}

if(config.SHOW_NAVBAR) {
if(config.ENABLED_MODES === null) {
$('.nav a').removeClass('hide');
}
else {
$.each(config.ENABLED_MODES, function () {
$('.nav a[data-mode=' + this + ']').removeClass('hide');
});
}
}
else {
$('.navbar-default .navbar-toggle').hide();
$('.navbar-default .nav').hide();
}

var hash = parent.location.hash;

try {
if(!hash || !$(hash + 'Container')) {
hash = '#' + config.DEFAULT_MODE;
parent.location.hash = hash;
}
}
catch(e) {
console.error('Invalid hash: ' + e);
hash = '#' + config.DEFAULT_MODE;
parent.location.hash = hash;
}

try {
if(hash === '#webpageTranslation') {
hash = '#translation';
showTranslateWebpageInterface();
}
else if(!hash || !$(hash + 'Container').length) {
hash = '#' + config.DEFAULT_MODE;
parent.location.hash = hash;
}
}
catch(e) {
console.error('Invalid hash: ' + e);
hash = '#' + config.DEFAULT_MODE;
parent.location.hash = hash;
}

$('.modeContainer' + hash + 'Container').show();
$('.navbar-default .nav li > a[data-mode=' + hash.substring(1) + ']').parent().addClass('active');

$('.navbar-default .nav a').click(function () {
var mode = $(this).data('mode');
$('.nav li').removeClass('active');
$(this).parent('li').addClass('active');
$('.modeContainer:not(#' + mode + 'Container)').stop().hide({
queue: false
});
$('#' + mode + 'Container').stop().show({
queue: false
});
synchronizeTextareaHeights();
});

resizeFooter();
$(window)
.on('hashchange', persistChoices)
.resize(debounce(function () {
populateTranslationList();
resizeFooter();
}));

if(config.ALLOWED_LANGS) {
var withIso = [];
$.each(config.ALLOWED_LANGS, function () {
if(iso639Codes[this]) {
withIso.push(iso639Codes[this]);
}
if(iso639CodesInverse[this]) {
withIso.push(iso639CodesInverse[this]);
}
});
Array.prototype.push.apply(config.ALLOWED_LANGS, withIso);
}

$('form').submit(function () {
return false;
});

$('.modal').on('show.bs.modal', function () {
$('a[data-target=#' + $(this).attr('id') + ']').parents('li').addClass('active');
$.each($(this).find('img[data-src]'), function () {
$(this).attr('src', $(this).attr('data-src'));
});
});

$('.modal').on('hide.bs.modal', function () {
$('a[data-target=#' + $(this).attr('id') + ']').parents('li').removeClass('active');
});

$('#backToTop').addClass('hide');
$(window).scroll(function () {
$('#backToTop').toggleClass('hide', $(window).scrollTop() < BACK_TO_TOP_BUTTON_ACTIVATION_HEIGHT);
});

$('#backToTop').click(function () {
$('html, body').animate({
scrollTop: 0
}, 'fast');
return false;
});

$('#installationNotice').addClass('hide');
});

if(config.PIWIK_SITEID && config.PIWIK_URL) {
var url = config.PIWIK_URL;
if(document.location.protocol === 'https:') {
url = url.replace(/^(http(s)?)?:/, 'https:');
}
// but if we're on plain http, we keep whatever was in the config
if(url.charAt(url.length - 1) !== '/') {
url += '/';
}

/* eslint-disable */
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u=url;
_paq = _paq || [];
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', config.PIWIK_SITEID]);
var d=document,
g=d.createElement('script'),
s=d.getElementsByTagName('script')[0];
g.type='text/javascript';
g.defer=true;
g.async=true;
g.src=u+'piwik.js';
if(s.parentNode) {
s.parentNode.insertBefore(g,s);
}
})();
/* eslint-enable */
}

/*:: export {_paq} */
/*:: import {config} from "./config.js" */
/*:: import {persistChoices} from "./persistence.js" */
/*:: import {iso639Codes, iso639CodesInverse} from "./localization.js" */
/*:: import {populateTranslationList, showTranslateWebpageInterface} from "./translator.js" */
/*:: import {ajaxSend, ajaxComplete, debounce, resizeFooter, synchronizeTextareaHeights} from "./util.js" */
28 changes: 17 additions & 11 deletions assets/js/translator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* @flow */
// @flow

var pairs = {}, chainedPairs = {}, originalPairs = pairs;
var srcLangs = [], dstLangs = [];
var curSrcLang, curDstLang;
var recentSrcLangs = [], recentDstLangs = [];
var curSrcLang/*: string */, curDstLang/*: string */;
var recentSrcLangs /*: string[] */ = [], recentDstLangs /*: string[] */ = [];
var droppedFile;
var translateRequest;

Expand All @@ -24,7 +24,7 @@ var PUNCTUATION_KEY_CODES = [46, 33, 58, 63, 47, 45, 190, 171, 49]; // eslint-di
/* global config, modeEnabled, synchronizeTextareaHeights, persistChoices, getLangByCode, sendEvent, onlyUnique, restoreChoices
getDynamicLocalization, locale, ajaxSend, ajaxComplete, localizeInterface, filterLangList, cache, readCache, iso639Codes,
callApy, apyRequestTimeout, isURL */
/* global SPACE_KEY_CODE, ENTER_KEY_CODE, HTTP_OK_CODE, XHR_LOADING, XHR_DONE, HTTP_OK_CODE, HTTP_BAD_REQUEST_CODE */
/* global SPACE_KEY_CODE, ENTER_KEY_CODE, HTTP_OK_CODE, XHR_LOADING, XHR_DONE, HTTP_BAD_REQUEST_CODE */

if(modeEnabled('translation')) {
$(document).ready(function () {
Expand Down Expand Up @@ -421,7 +421,9 @@ function getPairs() {
break;
}
for(var i = 0; i < TRANSLATION_LIST_BUTTONS; i++) {
// $FlowFixMe
recentSrcLangs.push(i < srcLangs.length ? srcLangs[i] : undefined);
// $FlowFixMe
recentDstLangs.push(i < dstLangs.length ? dstLangs[i] : undefined);
}

Expand Down Expand Up @@ -674,7 +676,9 @@ function translateText(ignoreIfEmpty) {
request = {'langpair': curSrcLang + '|' + curDstLang};
}

// $FlowFixMe
request.q = originalText; // eslint-disable-line id-length
// $FlowFixMe
request.markUnknown = $('#markUnknown').prop('checked') ? 'yes' : 'no';
translateRequest = callApy({
data: request,
Expand Down Expand Up @@ -725,6 +729,7 @@ function translateDoc() {
$('input#fileInput').prop('disabled', true);
$('button#translate').prop('disabled', true);

// $FlowFixMe
var xhr = new XMLHttpRequest({mozSystem: true});
xhr.addEventListener('progress', updateProgressBar, false);
if(xhr.upload) {
Expand Down Expand Up @@ -758,6 +763,7 @@ function translateDoc() {
reader.onload = function () {
$('a#fileDownload')
.click(function () {
// $FlowFixMe
window.location.href = reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;');
});
};
Expand Down Expand Up @@ -803,7 +809,7 @@ function translateDoc() {
docTranslateError(getDynamicLocalization('Not_Available'));
}

function updateProgressBar(ev) {
function updateProgressBar(ev /*: ProgressEvent | any */) {
var progress = 0.0;
if(ev instanceof ProgressEvent) {
progress = ev.loaded / ev.total;
Expand Down Expand Up @@ -934,7 +940,7 @@ function translateWebpage(ignoreIfEmpty) {
}
}

function showTranslateWebpageInterface(url, ignoreIfEmpty) {
function showTranslateWebpageInterface(url /*: ?string */, ignoreIfEmpty /*: ?boolean */) {
$('#srcLangSelectors').removeClass('col-sm-11').addClass('col-sm-9');

$('div#translateText').fadeOut('fast', function () {
Expand Down Expand Up @@ -1074,10 +1080,10 @@ function autoSelectDstLang() {
}
}

/*:: import {synchronizeTextareaHeights, modeEnabled, ajaxSend, ajaxComplete, filterLangList, onlyUnique, getLangByCode,
callApy, apyRequestTimeout} from "./util.js" */
/*:: export {populateTranslationList, showTranslateWebpageInterface} */
/*:: import {synchronizeTextareaHeights, modeEnabled, ajaxSend, ajaxComplete, filterLangList, onlyUnique, sendEvent, callApy,
apyRequestTimeout, SPACE_KEY_CODE, ENTER_KEY_CODE, HTTP_OK_CODE, XHR_LOADING, XHR_DONE, HTTP_BAD_REQUEST_CODE} from "./util.js" */
/*:: import {persistChoices, restoreChoices} from "./persistence.js" */
/*:: import localizeInterface from "./localization.js" */
/*:: import {readCache,cache} from "./cache.js" */
/*:: import {config} from "./config.js" */
/*:: import {localizeInterface, getLangByCode, getDynamicLocalization, locale, iso639Codes} from "./localization.js" */
/*:: import {readCache, cache} from "./persistence.js" */
/*:: import {isURL} from "./util.js" */
Loading

0 comments on commit 9f4129d

Please sign in to comment.