Skip to content

Commit

Permalink
Merge pull request #41 from ftlabs/parser
Browse files Browse the repository at this point in the history
Parser
  • Loading branch information
Lily2point0 authored Jun 1, 2017
2 parents a29874d + 74acd22 commit 292d6aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
25 changes: 21 additions & 4 deletions src/js/crossword_parser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Using UMD (Universal Module Definition), see https://github.com/umdjs/umd, and Jake,
// for a js file to be included as-is in Node code and in browser code.
(function (root, factory) {
if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.CrosswordDSL = factory();
}
}(this, function () {
// given the DSL, ensure we have all the relevant pieces,
// and assume there will be subsequent checking to ensure they are valid
function parseDSL(text){
Expand Down Expand Up @@ -468,25 +481,29 @@
return crossword;
}

function parseWhateverItIsIntoSpecText(text) {
function parseWhateverItIsIntoSpecJson(text) {
// returns spec or errors as JSON
var crossword = parseWhateverItIs(text);

var responseObj;
if (crossword.errors.length == 0) {
console.log("parseWhateverItIsIntoSpecText: no errors found");
console.log("parseWhateverItIsIntoSpecJson: no errors found");
responseObj = crossword.spec;
} else {
responseObj = {
errors: crossword.errors,
text : text
}
console.log("parseWhateverItIsIntoSpecText: errors found:\n", crossword.errors.join("\n"), "\ntext=\n", text);
console.log("parseWhateverItIsIntoSpecJson: errors found:\n", crossword.errors.join("\n"), "\ntext=\n", text);
}

var jsonText = JSON.stringify( responseObj );

return jsonText;
}

module.exports = parseWhateverItIsIntoSpecText;
return {
'whateverItIs' : parseWhateverItIs,
'intoSpecJson' : parseWhateverItIsIntoSpecJson
};
}));
34 changes: 17 additions & 17 deletions src/js/oCrossword.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function buildGrid(

let count = 0;

if(across[3].length > 1) {
if(across[3].length > 1) {
for(var j = 0; j < across[3].length; ++j) {
if(j%2 === 1) {
count += parseInt(across[3][j-1]);
Expand All @@ -132,7 +132,7 @@ function buildGrid(
} else if(across[3][j] === ',') {
separator.innerHTML = '&nbsp;';
}

if(i === count && separator.innerHTML !== '') {
tempPartial.appendChild(separator);
}
Expand Down Expand Up @@ -172,7 +172,7 @@ function buildGrid(

let count = 0;

if(down[3].length > 1) {
if(down[3].length > 1) {
for(var j = 0; j < down[3].length; ++j) {
if(j%2 === 1) {
count += parseInt(down[3][j-1]);
Expand Down Expand Up @@ -256,11 +256,11 @@ function OCrossword(rootEl) {
resolve( this.rootEl.dataset.oCrosswordData );
}
})
.then(text => crosswordParser(text))
.then(text => crosswordParser.intoSpecJson(text))
.then(specText => JSON.parse(specText) )
.then( json => {
if (json.errors){
console.log(`Found Errors after invoking crosswordParser:\n${json.errors.join("\n")}` );
console.log(`Found Errors after invoking crosswordParser.intoSpecJson:\n${json.errors.join("\n")}` );
writeErrorsAsClues(rootEl, json);
return Promise.reject("Failed to parse crossword data, so cannot generate crossword display");
} else {
Expand Down Expand Up @@ -432,7 +432,7 @@ OCrossword.prototype.assemble = function assemble() {
return;
}
}

progress();
});

Expand All @@ -443,9 +443,9 @@ OCrossword.prototype.assemble = function assemble() {
e.preventDefault();
timer = 0;
}

let gridSync = getCellFromClue(e.target);


if (e.keyCode === 13) { //enter
e.target.blur();
Expand All @@ -471,15 +471,15 @@ OCrossword.prototype.assemble = function assemble() {
setTimeout(function(){
e.target.value = '';
gridSync.grid.textContent = e.target.value;

if(gridSync.defSync) {
let defSync = cluesEl.querySelector('input[data-link-identifier="' + gridSync.defSyncInput +'"]');
defSync.value = e.target.value;
}

nextInput(e.target, -1);
}, timer);

return;
}

Expand All @@ -503,7 +503,7 @@ OCrossword.prototype.assemble = function assemble() {

setTimeout(function(){
gridSync.grid.textContent = e.target.value;

if(gridSync.defSync) {
let defSync = cluesEl.querySelector('input[data-link-identifier="' + gridSync.defSyncInput +'"]');
defSync.value = e.target.value;
Expand Down Expand Up @@ -535,7 +535,7 @@ OCrossword.prototype.assemble = function assemble() {
let defDirection = (inputIdentifier.slice(0,1) === 'A')?'across':'down';
let defNum = inputIdentifier.slice(1,inputIdentifier.length).split('-')[0];
let defIndex = parseInt(inputIdentifier.split('-')[1]);

let cells = gridEl.querySelectorAll('td:not(.empty)');
let selectedCell = {};

Expand Down Expand Up @@ -664,7 +664,7 @@ OCrossword.prototype.assemble = function assemble() {
const onResize = function onResize(init) {
var isMobile = false;
const cellSizeMax = 40;

if (window.innerWidth <= 739) {
isMobile = true;
} else if (window.innerWidth > window.innerHeight && window.innerHeight <=739 ) { //rotated phones and small devices, but not iOS
Expand Down Expand Up @@ -776,7 +776,7 @@ OCrossword.prototype.assemble = function assemble() {
function unsetClue(number, direction) {
const el = cluesEl.querySelector(`li[data-o-crossword-number="${number}"][data-o-crossword-direction="${direction}"]`);
const els = Array.from(gridEl.querySelectorAll('td[data-o-crossword-highlighted]'));

for (const o of els) {
delete o.dataset.oCrosswordHighlighted;
}
Expand All @@ -800,7 +800,7 @@ OCrossword.prototype.assemble = function assemble() {
let linkName = gridItems[i].direction[0].toUpperCase() + gridItems[i].number + '-' + gridItems[i].answerPos;
targets.push(cluesEl.querySelector('input[data-link-identifier="'+linkName+'"]'));
}

Array.from(targets).forEach((target) => {
target.value = letter;
});
Expand Down Expand Up @@ -865,7 +865,7 @@ OCrossword.prototype.assemble = function assemble() {
if (!toggleClueSelection(clueDetails)) {
return;
}

const el = gridEl.querySelector(`td[data-o-crossword-number="${clueDetails.number}"]`);
target = el;
}
Expand Down Expand Up @@ -1007,4 +1007,4 @@ function isiOS() {
function isAndroid() {
var android = navigator.userAgent.toLowerCase().indexOf("android") > -1;
return android;
}
}

0 comments on commit 292d6aa

Please sign in to comment.