Skip to content

Commit

Permalink
Work on adapt-it#539
Browse files Browse the repository at this point in the history
Refactoring in progress -- moving import persistence to the model object (project::fromString()).
  • Loading branch information
eb1 committed Dec 29, 2023
1 parent e074adf commit bae8063
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 16 deletions.
173 changes: 173 additions & 0 deletions www/js/models/sql/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,179 @@ define(function (require) {
initialize: function () {
this.on('change', this.save, this);
},
// populate this project object from an .aic file / string
fromString: function (str) {
var value = "",
value2 = "",
value3 = "",
value4 = "",
i = 0,
s = null,
t = null,
arrPunct = [],
arrCases = [];
// helper method to convert .aic color values to an html hex color string:
// .aic --> bbggrr (in base 10)
// .html --> #rrggbb (in hex)
var getColorValue = function (strValue) {
var intValue = parseInt(strValue, 10);
var rValue = ("00" + (intValue & 0xff).toString(16)).slice(-2);
var gValue = ("00" + ((intValue >> 8) & 0xff).toString(16)).slice(-2);
var bValue = ("00" + ((intValue >> 16) & 0xff).toString(16)).slice(-2);
// format in html hex, padded with leading zeroes
var theValue = "#" + rValue + gValue + bValue;
return theValue;
};
// Helper method to pull out the value corresponding to the named setting from the .aic file contents
// (the array "lines"). If the named setting isn't found at that line, it searches FORWARD to the end --
// returning an empty string if not found.
var getSettingValue = function (expectedIndex, aicSetting) {
var i = 0,
value = "";
if (lines[expectedIndex].indexOf(aicSetting) !== -1) {
// the value is the rest of the line AFTER the aicsetting + space
value = lines[expectedIndex].substr(aicSetting.length + 1);
} else {
// This setting is NOT at the line we expected. It could be on a different
// line, or not in the .aic file at all
for (i = 0; i < lines.length; i++) {
if (lines[i].indexOf(aicSetting) === 0) {
// Found! The value is the rest of the line AFTER the aicsetting + space
value = lines[i].substr(aicSetting.length + 1);
// finish searching
break;
}
}
}
return value;
};
// split out the .aic file into an array (one entry per line of the file)
lines = str.split("\n");
// first off, a couple of sanity checks:
// 1. Is this .aic file an Adapt It project file?
var srcLangName = getSettingValue(55, "SourceLanguageName");
var tgtLangName = getSettingValue(56, "TargetLanguageName");
if ((srcLangName.length === 0) || (tgtLangName.length === 0)) {
// source or target language name not found -- we can't parse this as a project file
return false; // no message, as this might be parsed as just regular text later
}
// 2. Is this for a file we've already configured or imported (i.e., do the source and target languages
// match a project in our project list)?
if (window.ProjectList) {
// we've got some projects -- see if our source and target match one of them
window.Application.ProjectList.each(function (model, index) {
if (SourceLanguageName === srcLangName && TargetLanguageName === tgtLangName) {
// stop import -- this file matches an existing project in our list
// errMsg = i18n.t("view.dscErrDuplicateFile");
return false;
}
});
}
// We've successfully opened an Adapt It project file (.aic), and it's not a duplicate -
// populate our AIM model object with values from the file
SourceLanguageName = srcLangName;
TargetLanguageName = tgtLangName;
SourceLanguageCode = getSettingValue(59, "SourceLanguageCode");
TargetLanguageCode = getSettingValue(60, "TargetLanguageCode");
SourceDir = (getSettingValue(115, "SourceIsRTL") === "1") ? "rtl" : "ltr";
TargetDir = (getSettingValue(116, "TargetIsRTL") === "1") ? "rtl" : "ltr";
value = getSettingValue(124, "ProjectName");
if (value.length > 0) {
name = value;
} else {
// project name not found -- build it from the source & target languages
name = i18n.t("view.lblSourceToTargetAdaptations", {
source: (SourceVariant.length > 0) ? SourceVariant : SourceLanguageName,
target: (TargetVariant.length > 0) ? TargetVariant : TargetLanguageName
});
}
// filters (USFM only -- other settings are ignored)
value = getSettingValue(124, "UseSFMarkerSet");
if (value === "UsfmOnly") {
value = getSettingValue(123, "UseFilterMarkers");
if (value !== FilterMarkers) {
UseCustomFilters = "true";
FilterMarkers = value;
}
}
value = window.Application.generateUUID();
projectid = value;
// The following settings require some extra work
// Punctuation pairs
value = getSettingValue(79, "PunctuationPairsSourceSet(stores space for an empty cell)");
value2 = getSettingValue(80, "PunctuationPairsTargetSet(stores space for an empty cell)");
for (i = 0; i < value.length; i++) {
s = value.charAt(i);
t = value2.charAt(i);
if (s && s.length > 0) {
arrPunct[arrPunct.length] = {s: s, t: t};
}
}
// add double punctuation pairs as well
value = getSettingValue(81, "PunctuationTwoCharacterPairsSourceSet(ditto)");
value2 = getSettingValue(82, "PunctuationTwoCharacterPairsTargetSet(ditto)");
i = 0;
while (i < value.length) {
s = value.substr(i, 2);
t = value2.substr(i, 2);
if (s && s.length > 0) {
arrPunct[arrPunct.length] = {s: s, t: t};
}
i = i + 2; // advance to the next item (each set is 2 chars in length)
}
PunctPairs = arrPunct;
// Auto capitalization
value = getSettingValue(115, "LowerCaseSourceLanguageChars");
value2 = getSettingValue(116, "UpperCaseSourceLanguageChars");
value3 = getSettingValue(117, "LowerCaseTargetLanguageChars");
value4 = getSettingValue(118, "UpperCaseTargetLanguageChars");
for (i = 0; i < value.length; i++) {
s = value.charAt(i) + value2.charAt(i);
t = value3.charAt(i) + value4.charAt(i);
if (s && s.length > 0) {
arrCases[arrCases.length] = {s: s, t: t};
}
}
CasePairs = arrCases;
value = getSettingValue(121, "AutoCapitalizationFlag");
AutoCapitalization = (value === "1") ? "true" : "false";
value = getSettingValue(122, "SourceHasUpperCaseAndLowerCase");
SourceHasUpperCase = (value === "1") ? "true" : "false";

// Fonts, if they're installed on this device (getFontList is async)
if (navigator.Fonts) {
navigator.Fonts.getFontList(
function (fontList) {
if (fontList) {
// Source Font
value = getSettingValue(16, "FaceName");
if ($.inArray(value, fontList) > -1) {
SourceFont = value;
}
// Target Font
value = getSettingValue(34, "FaceName");
if ($.inArray(value, fontList) > -1) {
TargetFont = value;
}
}
},
function (error) {
console.log("FontList error: " + error);
}
);
}
// font colors
SourceColor = getColorValue(getSettingValue(17, "Color"));
TargetColor = getColorValue(getSettingValue(34, "Color"));
NavColor = getColorValue(getSettingValue(53, "Color"));
SpecialTextColor = getColorValue(getSettingValue(87, "SpecialTextColor"));
RetranslationColor = getColorValue(getSettingValue(88, "RetranslationTextColor"));
TextDifferencesColor = getColorValue(getSettingValue(89, "TargetDifferencesTextColor"));
projectid = window.Application.generateUUID();

// succeeded -- return true
return true;
},
fetch: function () {
var deferred = $.Deferred();
var obj = this;
Expand Down
26 changes: 10 additions & 16 deletions www/js/views/DocumentViews.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ define(function (require) {
Backbone = require('backbone'),
Marionette = require('marionette'),
i18n = require('i18n'),
ProjectViews = require('app/views/ProjectViews'),
projModel = require('app/models/project'),
tplLoadingPleaseWait = require('text!tpl/LoadingPleaseWait.html'),
tplImportDoc = require('text!tpl/CopyOrImport.html'),
tplExportDoc = require('text!tpl/Export.html'),
Expand Down Expand Up @@ -3861,9 +3861,9 @@ define(function (require) {
}
}
} else if (fileName.toLowerCase().indexOf(".aic") > 0) {
// project settings file reader is held in ProjectViews - call it
result = ProjectViews.importSettingsFile({model: proj});
return result; // in this case, we've already notified the user in ProjectViews::importFail();
// create a new project object and populate it from the file contents
var newProj = new projModel.Project();
result = projModel.fromString(contents);
} else {
if (isClipboard === true) {
// this came from the clipboard -- we'll need to do some tests to try to identify the content type.
Expand Down Expand Up @@ -3927,9 +3927,9 @@ define(function (require) {
// _probably_ \lx data for the KB
result = readSFMLexDoc(contents);
} else if (contents.indexOf("PunctuationTwoCharacterPairsSourceSet") >= 0) {
// project settings file reader is held in ProjectViews - call it
result = ProjectViews.importSettingsFile({model: proj});
return result; // in this case, we've already notified the user in ProjectViews::importFail();
// create a new project object and populate it from the clipboard contents
var newProj = new projModel.Project();
result = projModel.fromString(contents);
} else {
// unknown -- try reading it as a text document
result = readTextDoc(contents);
Expand Down Expand Up @@ -6249,14 +6249,8 @@ define(function (require) {
$("#btnCancel").show();
$("#status").html(i18n.t("view.dscStatusReading", {document: fileName}));
$("#btnOK").hide();
// is this an .aic file?
if (fileName.toLowerCase().indexOf(".aic") > 0) {
// .aic project settings file reader is held in ProjectViews - call it
ProjectViews.importSettingsFile(file, this.model);
} else {
// import the specified file
importFile(file, this.model);
}
// import the specified file
importFile(file, this.model);
},
// Handler for when the user clicks the Select button (browser only) -
// (this is the html <input type=file> element displayed for the browser only) --
Expand Down Expand Up @@ -6341,7 +6335,7 @@ define(function (require) {
var clipboardFile = new Blob([text], {type: "text/plain"});
$("#status").html(i18n.t("view.dscStatusReading", {document: i18n.t("view.lblCopyClipboardText")}));
fileName = i18n.t("view.lblText") + "-" + (window.Application.generateUUID());
importFile(clipboardFile, model);
importFile(clipboardFile, model);
} else {
console.log("No data to import");
// No data to import -- tell the user to copy something to the clipboard
Expand Down

0 comments on commit bae8063

Please sign in to comment.