Skip to content

Commit

Permalink
Work on adapt-it#539
Browse files Browse the repository at this point in the history
More refactoring of AIC file import...
  • Loading branch information
eb1 committed Dec 30, 2023
1 parent bae8063 commit ca373cb
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 52 deletions.
18 changes: 2 additions & 16 deletions www/js/models/sql/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,28 +704,14 @@ define(function (require) {
};
// 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?
// Is this string 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
// This is a project file string -- populate this project object
SourceLanguageName = srcLangName;
TargetLanguageName = tgtLangName;
SourceLanguageCode = getSettingValue(59, "SourceLanguageCode");
Expand Down
4 changes: 2 additions & 2 deletions www/js/views/DocumentViews.js
Original file line number Diff line number Diff line change
Expand Up @@ -3863,7 +3863,7 @@ define(function (require) {
} else if (fileName.toLowerCase().indexOf(".aic") > 0) {
// create a new project object and populate it from the file contents
var newProj = new projModel.Project();
result = projModel.fromString(contents);
result = newProj.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 @@ -3929,7 +3929,7 @@ define(function (require) {
} else if (contents.indexOf("PunctuationTwoCharacterPairsSourceSet") >= 0) {
// create a new project object and populate it from the clipboard contents
var newProj = new projModel.Project();
result = projModel.fromString(contents);
result = newProj.fromString(contents);
} else {
// unknown -- try reading it as a text document
result = readTextDoc(contents);
Expand Down
178 changes: 144 additions & 34 deletions www/js/views/ProjectViews.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,77 @@ define(function (require) {
}
return fullCode;
},


// Callback for when the file is imported / saved successfully
importSuccess = function (project) {
console.log("importSuccess()");
// hide / show UI elements
$("#selectControls").hide();
$("#LoadingStatus").hide();
$("#verifyNameControls").show();
$("#OKCancelButtons").show();
$("#lblVerify").hide();
$("#rowBookName").hide();
// tell the user the file was imported successfully
$("#lblDirections").html(i18n.t("view.dscStatusImportSuccess", {document: project.get("name")}));
// is there more than one project in our project list?
if (window.Application.ProjectList) {
// YES -- ask if they want to switch
if (navigator.notification) {
// on mobile device -- use notification plugin API
navigator.notification.confirm(
i18n.t('view.msgUseProject'),
function (btnIndex) {
if (btnIndex === 1) {
window.Application.currentProject = project;
localStorage.setItem("CurrentProjectID", project.get("projectid"));
// Clear out any local chapter/book/sourcephrase/KB stuff so it loads
// from our new project instead
window.Application.BookList.length = 0;
window.Application.ChapterList.length = 0;
window.Application.spList.length = 0;
window.Application.kbList.length = 0;
} else {
// No -- just exit
}
},
i18n.t('view.ttlMain'),
[i18n.t('view.lblYes'), i18n.t('view.lblNo')]
);
} else {
// in browser -- use window.confirm / window.alert
if (window.confirm(i18n.t('view.msgUseProject'))) {
window.Application.currentProject = project;
localStorage.setItem("CurrentProjectID", project.get("projectid"));
// Clear out any local chapter/book/sourcephrase/KB stuff so it loads
// from our new project instead
window.Application.BookList.length = 0;
window.Application.ChapterList.length = 0;
window.Application.spList.length = 0;
window.Application.kbList.length = 0;
} else {
// No -- just exit
}
}
}
},
// Callback for when the file failed to import
importFail = function (e) {
console.log("importFail(): " + e.message);
// update status with the failure message and code (if available)
var strReason = e.message;
if (e.code) {
strReason += " (code: " + e.code + ")";
}
// hide / show UI elements
$("#selectControls").hide();
$("#LoadingStatus").hide();
$("#verifyNameControls").show();
$("#OKCancelButtons").show();
$("#lblVerify").hide();
$("#rowBookName").hide();
// tell the user what went wrong
$("#lblDirections").html(i18n.t("view.ErrCopyProjectFailed", {reason: strReason}));
},
// Helper method to hide the prev/next buttons and increase the scroller size
// if the screen is too small
// (Issue #232)
Expand Down Expand Up @@ -132,16 +201,14 @@ define(function (require) {
i18n.t('view.msgUseProject'),
function (btnIndex) {
if (btnIndex === 1) {
// window.Application.currentProject = window.Application.ProjectList.at(index);
// localStorage.setItem("CurrentProjectID", window.Application.currentProject.get("projectid"));
// // Clear out any local chapter/book/sourcephrase/KB stuff so it loads
// // from our new project instead
// window.Application.BookList.length = 0;
// window.Application.ChapterList.length = 0;
// window.Application.spList.length = 0;
// window.Application.kbList.length = 0;
// // head back to the home page
// window.location.replace("");
window.Application.currentProject = project;
localStorage.setItem("CurrentProjectID", project.get("projectid"));
// Clear out any local chapter/book/sourcephrase/KB stuff so it loads
// from our new project instead
window.Application.BookList.length = 0;
window.Application.ChapterList.length = 0;
window.Application.spList.length = 0;
window.Application.kbList.length = 0;
} else {
// No -- just exit
}
Expand All @@ -152,16 +219,14 @@ define(function (require) {
} else {
// in browser -- use window.confirm / window.alert
if (window.confirm(i18n.t('view.msgUseProject'))) {
// window.Application.currentProject = window.Application.ProjectList.at(index);
// localStorage.setItem("CurrentProjectID", window.Application.currentProject.get("projectid"));
// // Clear out any local chapter/book/sourcephrase/KB stuff so it loads
// // from our new project instead
// window.Application.BookList.length = 0;
// window.Application.ChapterList.length = 0;
// window.Application.spList.length = 0;
// window.Application.kbList.length = 0;
// // head back to the home page
// window.location.replace("");
window.Application.currentProject = project;
localStorage.setItem("CurrentProjectID", project.get("projectid"));
// Clear out any local chapter/book/sourcephrase/KB stuff so it loads
// from our new project instead
window.Application.BookList.length = 0;
window.Application.ChapterList.length = 0;
window.Application.spList.length = 0;
window.Application.kbList.length = 0;
} else {
// No -- just exit
}
Expand Down Expand Up @@ -469,6 +534,7 @@ define(function (require) {
// copy the clipboard contents, and if they're not empty, try to import the contents as a file
onBtnClipboard: function () {
var model = this.model;
var errMsg = "";
// Are we in the browser or on a mobile device?
if (device && (device.platform !== "browser")) {
// mobile device
Expand All @@ -480,16 +546,34 @@ define(function (require) {
$("#selectControls").hide();
$("#LoadingStatus").html(Handlebars.compile(tplLoadingPleaseWait));
// Import can take a while, and potentially hang. Provide a way to cancel the operation
$("#btnCancel").show();
// EDB 12/19/2023: ? not sure if still true - ios has wkwebview now? need to test
// EDB 5/29 HACK: clipboard text -- create a blob instead of a file and read it:
// Cordova-ios uses an older web view that has a buggy / outdated JS engine w.r.t the File object;
// it places the contents in the name attribute. The FileReader does
// accept a Blob (the File object derives from Blob), which is why importFile works.
console.log("Clipboard selected. Creating ad hoc file from text.");
var clipboardFile = new Blob([text], {type: "text/plain"});
$("#btnCancel").show();
$("#status").html(i18n.t("view.dscStatusReading", {document: i18n.t("view.lblCopyClipboardText")}));
importSettingsFile(clipboardFile, model);
// create a new project object and populate it from the clipboard contents
var newProj = new projModel.Project();
result = newProj.fromString(text);
if (result == false) {
// this isn't project data -- error out
errMsg = i18n.t("view.ErrNotAIC");
importFail(new Error(errMsg));
} else {
// this is project data -- check to see if it's duplicate
if (window.Application.ProjectList) {
window.Application.ProjectList.each(function (model, index) {
if (model.get('SourceLanguageName') === newProj.get('SourceLanguageName') && model.get('TargetLanguageName') === newProj.get('TargetLanguageName')) {
// stop import -- this file matches an existing project in our list
errMsg = i18n.t("view.dscErrDuplicateFile");
importFail(new Error(errMsg)); // tell the user -- this can't be imported, period.
}
});
if (errMsg.length === 0) {
// success -- let the user know
importSuccess(newProj);
}
} else {
// success -- let the user know
importSuccess(newProj);
}
}
} else {
console.log("No data to import");
// No data to import -- tell the user to copy something to the clipboard
Expand All @@ -511,15 +595,39 @@ define(function (require) {
navigator.clipboard.readText().then(
(clipText) => {
if (clipText.length > 0) {
var clipboardFile = new Blob([clipText], {type: "text/plain"});
console.log("Non-empty clipboard selected. Creating ad hoc file from text.");
console.log("Non-empty clipboard selected.");
// replace the selection UI with the import UI
$("#selectControls").hide();
$("#LoadingStatus").html(Handlebars.compile(tplLoadingPleaseWait));
// Import can take a while, and potentially hang. Provide a way to cancel the operation
$("#btnCancel").show();
$("#status").html(i18n.t("view.dscStatusReading", {document: i18n.t("view.lblCopyClipboardText")}));
importSettingsFile(clipboardFile, model);
// create a new project object and populate it from the clipboard contents
var newProj = new projModel.Project();
result = newProj.fromString(clipText);
if (result == false) {
// this isn't project data -- error out
errMsg = i18n.t("view.ErrNotAIC");
importFail(new Error(errMsg));
} else {
// this is project data -- check to see if it's duplicate
if (window.Application.ProjectList) {
window.Application.ProjectList.each(function (model, index) {
if (model.get('SourceLanguageName') === newProj.get('SourceLanguageName') && model.get('TargetLanguageName') === newProj.get('TargetLanguageName')) {
// stop import -- this file matches an existing project in our list
errMsg = i18n.t("view.dscErrDuplicateFile");
importFail(new Error(errMsg)); // tell the user -- this can't be imported, period.
}
if (errMsg.length === 0) {
// success -- let the user know
importSuccess(newProj);
}
});
} else {
// success -- let the user know
importSuccess(newProj);
}
}
} else {
console.log("No data to import");
// No data to import -- tell the user to copy something to the clipboard
Expand Down Expand Up @@ -567,6 +675,8 @@ define(function (require) {
"click #AutoCapitalize": "onClickAutoCapitalize"
},
onFocusInput: function (event) {
// EDB - TODO: why was this cancelled out? Some weird side-effect?
// https://github.com/adapt-it/adapt-it-mobile/commit/fdd21f48cf30683b0ea02c02f726147b57864cc4
// HideTinyUI();
window.Application.scrollIntoViewCenter(event.currentTarget);
// event.currentTarget.scrollIntoView(true);
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions www/locales/dev/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@
"ErrImportNoProjectDefined": "Adapt It Mobile needs information about your translation project before you can import documents. Please create or copy a document, and then import documents into Adapt It Mobile.",
"ErrNoClipboard": "No text found on the clipboard. Please copy some text to the clipboard and try importing again.",
"msgUseProject": "Do you want to switch to this project?",
"ErrNotAIC": "Project data not found. (Is this an Adapt It desktop project (.aic) file?)",
"ErrCopyProjectFailed": "Unable to copy project. Reason: __reason__",
"_comment" : "****Add new strings BEFORE this line!****"
},
"help": {
Expand Down
2 changes: 2 additions & 0 deletions www/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@
"ErrImportNoProjectDefined": "Adapt It Mobile needs information about your translation project before you can import documents. Please create or copy a document, and then import documents into Adapt It Mobile.",
"ErrNoClipboard": "No text found on the clipboard. Please copy some text to the clipboard and try importing again.",
"msgUseProject": "Do you want to switch to this project?",
"ErrNotAIC": "Project data not found. (Is this an Adapt It desktop project (.aic) file?)",
"ErrCopyProjectFailed": "Unable to copy project. Reason: __reason__",
"_comment" : "****Add new strings BEFORE this line!****"
},
"help": {
Expand Down
Loading

0 comments on commit ca373cb

Please sign in to comment.