Skip to content

Commit 7ecce48

Browse files
committed
- Added Utils.getHashCode();
- Implemented in the ExpressionModel in place of getState()
1 parent aa28123 commit 7ecce48

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

js/net/ExpressionModel.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
var EventDispatcher = require('../events/EventDispatcher');
2626
var ServerModel = require('./ServerModel');
2727
var Settings = require('../Settings');
28+
var Utils = require('../utils/Utils');
2829

2930
var s = {};
3031
EventDispatcher.initialize(s);
@@ -44,12 +45,12 @@ s.savePattern = function (tags, name, pattern, content, replace, description, au
4445
};
4546

4647
s.saveState = function () {
47-
s._saveState = s.getState();
48+
s._saveState = s.getStateHash();
4849
s._lastId = null;
4950
};
5051

5152
s.isDirty = function () {
52-
var dirty = s._saveState !== s.getState();
53+
var dirty = s._saveState !== s.getStateHash();
5354
if (dirty && s.id) {
5455
s._lastId = s.id;
5556
s.id = null;
@@ -60,15 +61,12 @@ s.isDirty = function () {
6061
return dirty;
6162
};
6263

63-
s.getState = function () {
64-
// TODO: the state changes whenever the selected tool changes
65-
// TODO: you may edit the toolValue, then change tools & not register as dirty.
66-
// TODO: may be easier to just use "change" events, even if can provide false positives.
64+
s.getStateHash = function () {
6765
var state =
6866
s.docView.getExpression() +
6967
s.docView.getText() +
7068
JSON.stringify(s.docView.getState());
71-
return state;
69+
return Utils.getHashCode(state);
7270
};
7371

7472
s.handleSaveSuccess = function (result) {

js/utils/Utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,19 @@ Utils.div = function(content, className) {
335335
return div;
336336
};
337337

338+
/**
339+
* Implementation of Java’s String.hashCode() method
340+
* Original Source: http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/
341+
*
342+
* @param s
343+
* @returns {number}
344+
*/
345+
Utils.getHashCode = function(s) {
346+
var hash = 0, l = s.length, i;
347+
for (i = 0; i < l; i++ ) {
348+
hash = ((hash << 5) - hash) + s.charCodeAt(i) | 0;
349+
}
350+
return hash;
351+
};
352+
338353
module.exports = Utils;

0 commit comments

Comments
 (0)