Skip to content

Commit

Permalink
a couple longevity fixes
Browse files Browse the repository at this point in the history
- Make sourcephrase create/update calls async w/deferred. Ran into a stack overflow error at the pile I had encountered some duplicate entries -- this _might_ have been the cause of the dups. Still need to run tests for a while.
- Replace some obsolete event methods with trigger() calls.
  • Loading branch information
eb1 committed Aug 20, 2024
1 parent dfbc5c9 commit 0bbe1b8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
39 changes: 25 additions & 14 deletions www/js/models/sql/sourcephrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ define(function (require) {
return deferred.promise();
},

cleanDuplicates = function() {
//DELETE FROM sourcephrase WHERE rowid NOT IN (SELECT min(rowid) FROM sourcephrase GROUP BY spid);
window.Application.db.transaction(function (tx) {
tx.executeSql("DELETE FROM sourcephrase WHERE rowid NOT IN (SELECT min(rowid) FROM sourcephrase GROUP BY spid);");
}, function (err) {
console.log("cleanDuplicates() error: " + err.message);
});
},

SourcePhrase = Backbone.Model.extend({
// default values
defaults: {
Expand Down Expand Up @@ -71,27 +62,37 @@ define(function (require) {

},
create: function () {
var deferred = $.Deferred();
var attributes = this.attributes;
var sql = "INSERT INTO sourcephrase (spid, norder, chapterid, vid, markers, orig, prepuncts, midpuncts, follpuncts, flags, texttype, gloss, freetrans, note, srcwordbreak, tgtwordbreak, source, target) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
window.Application.db.transaction(function (tx) {
tx.executeSql(sql, [attributes.spid, attributes.norder, attributes.chapterid, attributes.vid, attributes.markers, attributes.orig, attributes.prepuncts, attributes.midpuncts, attributes.follpuncts, attributes.flags, attributes.texttype, attributes.gloss, attributes.freetrans, attributes.note, attributes.srcwordbreak, attributes.tgtwordbreak, attributes.source, attributes.target], function (tx, res) {
attributes.id = res.insertId;
// console.log("INSERT ok: " + res.toString());
console.log("sourcephrase INSERT ok.");
}, function (tx, err) {
console.log("SELECT error: " + err.message);
console.log("sourcephrase INSERT error: " + err.message);
});
}, function (e) {
deferred.reject(e);
}, function () {
deferred.resolve();
});
},
update: function () {
var deferred = $.Deferred();
var attributes = this.attributes;
var sql = 'UPDATE sourcephrase SET norder=?, chapterid=?, vid=?, markers=?, orig=?, prepuncts=?, midpuncts=?, follpuncts=?, flags=?, texttype=?, gloss=?, freetrans=?, note=?, srcwordbreak=?, tgtwordbreak=?, source=?, target=? WHERE spid=?;';
window.Application.db.transaction(function (tx) {
tx.executeSql(sql, [attributes.norder, attributes.chapterid, attributes.vid, attributes.markers, attributes.orig, attributes.prepuncts, attributes.midpuncts, attributes.follpuncts, attributes.flags, attributes.texttype, attributes.gloss, attributes.freetrans, attributes.note, attributes.srcwordbreak, attributes.tgtwordbreak, attributes.source, attributes.target, attributes.spid], function (tx, res) {
// console.log("INSERT ok: " + res.toString());
console.log("sourcephrase UPDATE ok.");
}, function (tx, err) {
console.log("SELECT error: " + err.message);
console.log("sourcephrase UPDATE error: " + err.message);
});
});
}, function (e) {
deferred.reject(e);
}, function () {
deferred.resolve();
});
},
destroy: function (options) {
var attributes = this.attributes;
Expand Down Expand Up @@ -172,6 +173,16 @@ define(function (require) {
}
},

// Deletes all duplicate entries from the table
cleanDuplicates: function() {
//DELETE FROM sourcephrase WHERE rowid NOT IN (SELECT min(rowid) FROM sourcephrase GROUP BY spid);
window.Application.db.transaction(function (tx) {
tx.executeSql("DELETE FROM sourcephrase WHERE rowid NOT IN (SELECT min(rowid) FROM sourcephrase GROUP BY spid);");
}, function (err) {
console.log("cleanDuplicates() error: " + err.message);
});
},

// Removes all sourcephrases from the collection (and database)
clearAll: function () {
window.Application.db.transaction(function (tx) {
Expand Down
24 changes: 15 additions & 9 deletions www/js/views/AdaptViews.js
Original file line number Diff line number Diff line change
Expand Up @@ -1050,18 +1050,18 @@ define(function (require) {
if (editorMode === editorModeEnum.ADAPTING) {
// adapting
selectedEnd = selectedStart = next_edit;
$(next_edit).find(".target").focus();
$(next_edit).find(".target").mouseup();
$(next_edit).find(".target").trigger('focus');
$(next_edit).find(".target").trigger('mouseup');
} else if (editorMode === editorModeEnum.GLOSSING) {
// glossing
selectedEnd = selectedStart = next_edit;
$(next_edit).find(".gloss").focus();
$(next_edit).find(".gloss").mouseup();
$(next_edit).find(".gloss").trigger('focus');
$(next_edit).find(".gloss").trigger('mouseup');
} else {
// free translation
// set focus on the FT text area
$("#fteditor").focus();
$("#fteditor").mouseup();
$("#fteditor").trigger('focus');
$("#fteditor").trigger('mouseup');
}
} else {
// the user is either at the first or last pile. Select it,
Expand Down Expand Up @@ -2833,9 +2833,9 @@ define(function (require) {
selectedEnd = selectedStart;
}
if (event.shiftKey) {
$("#PrevSP").mouseup(); // trigger prev SP button event
$("#PrevSP").trigger('mouseup'); // trigger prev SP button event
} else {
$("#NextSP").mouseup(); // trigger next SP button event
$("#NextSP").trigger('mouseup'); // trigger next SP button event
}
} else {
// any other key - set the dirty bit
Expand Down Expand Up @@ -3469,6 +3469,7 @@ define(function (require) {
selectedObj.save();
$(selectedStart).find(".target").html(selectedObj.get('target'));
}
console.log("togglePHBefore - creating spid: plc-" + newID);
phObj = new spModels.SourcePhrase({ spid: ("plc-" + newID), source: src, chapterid: selectedObj.get('chapterid'), vid: selectedObj.get('vid'), norder: nOrder, markers: mkrs, prepuncts: prePuncts});
phObj.save();
this.collection.add(phObj, {at: this.collection.indexOf(selectedObj)});
Expand Down Expand Up @@ -3564,6 +3565,7 @@ define(function (require) {
selectedObj.save();
$(selectedStart).find('.source').html(selectedObj.get('source'));
}
console.log("togglePHAfter - creating spid: pla-" + newID);
phObj = new spModels.SourcePhrase({ spid: ("pla-" + newID), source: src, chapterid: selectedObj.get('chapterid'), vid: selectedObj.get('vid'), norder: nOrder, follpuncts: follPuncts});
phObj.save();
// add to the model and UI _after_ the selected position
Expand Down Expand Up @@ -3711,6 +3713,7 @@ define(function (require) {
follpuncts = selectedObj.get('follpuncts');
// now build the new sourcephrase from the string
// model object itself
console.log("togglePhrase - creating spid: phr-" + newID);
phObj = new spModels.SourcePhrase({ spid: ("phr-" + newID), markers: phraseMarkers.trim(), source: phraseSource, target: phraseSource, orig: origTarget, prepuncts: prepuncts, follpuncts: follpuncts});
strID = $(selectedStart).attr('id');
strID = strID.substr(strID.indexOf("-") + 1); // remove "pile-"
Expand Down Expand Up @@ -3823,6 +3826,7 @@ define(function (require) {
theSource = value; // don't strip punctuation
// theSource = value.substr(startIdx, (endIdx) - startIdx);
// recreate the sourcephrase
console.log("togglePhrase - creating (rebuilt) spid: " + newID);
phObj = new spModels.SourcePhrase({ spid: (newID), norder: nOrder, source: theSource, target: phraseTarget, chapterid: selectedObj.get('chapterid'), prepuncts: prepuncts, follpuncts: follpuncts});
if (index === 0) {
// transfer any marker back (would be the first in the list)
Expand Down Expand Up @@ -3950,6 +3954,7 @@ define(function (require) {
follpuncts = selectedObj.get('follpuncts');
// now build the new sourcephrase from the string
// model object
console.log("toggleRetranslation - creating spid: ret-" + newID);
phObj = new spModels.SourcePhrase({ spid: ("ret-" + newID), markers: retMarkers.trim(), source: this.stripPunctuation(RetSource, true), target: RetSource, orig: origTarget, prepuncts: prepuncts, follpuncts: follpuncts});
strID = $(selectedStart).attr('id');
strID = strID.substr(strID.indexOf("-") + 1); // remove "pile-"
Expand Down Expand Up @@ -4037,6 +4042,7 @@ define(function (require) {
}
theSource = value.substr(startIdx, (endIdx) - startIdx);
// recreate the sourcephrase
console.log("toggleRetranslation - creating (rebuilt) spid: " + newID);
phObj = new spModels.SourcePhrase({ spid: (newID), norder: nOrder, source: theSource, target: RetTarget, chapterid: selectedObj.get('chapterid'), prepuncts: prepuncts, follpuncts: follpuncts});
if (index === 0) {
// transfer any marker back (would be the first in the list)
Expand Down Expand Up @@ -4111,7 +4117,6 @@ define(function (require) {
Backbone.history.loadUrl(Backbone.history.fragment);
},
checkAutoMerge: function () {
console.log("checkAutoMerge / keydown event");
if (!(event.target.id === "main")) {
// not our event (we want the window event)-- exit
return;
Expand All @@ -4120,6 +4125,7 @@ define(function (require) {
// we only care if the user is trying to adapt
return;
}
console.log("checkAutoMerge / keydown event");
if ((selectedStart !== null) && (selectedEnd !== null) && (selectedStart !== selectedEnd)) {
// user has selected more than one pile, then pressed a key -
// if it's a "normal" key, pocket the event and merge the piles (we'll handle the keydown event
Expand Down

0 comments on commit 0bbe1b8

Please sign in to comment.