Skip to content

Commit

Permalink
Merge pull request #830 from GrosPoulet/master
Browse files Browse the repository at this point in the history
Factorization of JSON search functions
  • Loading branch information
GrosPoulet authored Dec 5, 2021
2 parents 98795ed + 6b666f2 commit 4441387
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 130 deletions.
132 changes: 132 additions & 0 deletions js/hoverzoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3087,6 +3087,138 @@ var hoverZoom = {

cTimeEnd(msg);
},

// Find key(s) in JSON object and return corresponding value(s) and path(s)
// If key not found then return []
// Search is NOT case-sensitive
// https://gist.github.com/killants/569c4af5f2983e340512916e15a48ac0
getKeysInJsonObject:function (jsonObj, searchKey, isRegex, maxDeepLevel, currDeepLevel) {

var bShowInfo = false;

maxDeepLevel = ( maxDeepLevel || maxDeepLevel == 0 ) ? maxDeepLevel : 100;
currDeepLevel = currDeepLevel ? currDeepLevel : 1 ;
isRegex = isRegex ? isRegex : false;

// check RegEx validity if needed
var re;
if (isRegex) {
try {
re = new RegExp(searchKey);
} catch (e) {
cLog(e);
return [];
}
}

if ( currDeepLevel > maxDeepLevel ) {
return [];
} else {

var keys = [];

for ( var curr in jsonObj ) {
var currElem = jsonObj[curr];

if ( currDeepLevel == 1 && bShowInfo ) { cLog("getKeysInJsonObject : Looking property \"" + curr + "\" ") }

if ( isRegex ? re.test(curr) : curr.toLowerCase() === searchKey.toLowerCase() ){
var r = {};
r.key = curr;
r.value = currElem;
r.path = '["' + curr + '"]';
r.depth = currDeepLevel;
keys.push( r );
}

if ( typeof currElem == "object" ) { // object is "object" and "array" is also in the eyes of "typeof"
// search again :D
var deepKeys = hoverZoom.getKeysInJsonObject( currElem, searchKey, isRegex, maxDeepLevel, currDeepLevel + 1 );

for ( var e = 0; e < deepKeys.length; e++ ) {
// update path backwards
deepKeys[e].path = '["' + curr + '"]' + deepKeys[e].path;
keys.push( deepKeys[e] );
}
}
}
return keys;
}
},

// Find value(s) in JSON object and return corresponding key(s) and path(s)
// If value not found then return []
// Search is NOT case-sensitive
// ref: https://gist.github.com/killants/569c4af5f2983e340512916e15a48ac0
getValuesInJsonObject:function (jsonObj, searchValue, isRegex, isPartialMatch, isFirstMatchOnly, maxDeepLevel, currDeepLevel) {

var bShowInfo = false;

maxDeepLevel = ( maxDeepLevel || maxDeepLevel == 0 ) ? maxDeepLevel : 100;
currDeepLevel = currDeepLevel ? currDeepLevel : 1 ;
isRegex = isRegex ? isRegex : false;

// check RegEx validity if needed
var re;
if ( isRegex ) {
try {
re = new RegExp(searchValue);
} catch (e) {
cLog(e);
return [];
}
} else {
searchValue = searchValue.toString().toLowerCase();
}

if ( currDeepLevel > maxDeepLevel ) {
return [];
} else {

var keys = [];

for ( var curr in jsonObj ) {
var currElem = jsonObj[curr];

if ( currDeepLevel == 1 && bShowInfo ) { cLog("getValuesInJsonObject : Looking property \"" + curr + "\" ") }

if ( typeof currElem == "undefined" ) continue;

if ( typeof currElem == "object" ) { // object is "object" and "array" is also in the eyes of "typeof"
// search again :D
var deepKeys = hoverZoom.getValuesInJsonObject( currElem, searchValue, isRegex, isPartialMatch, isFirstMatchOnly, maxDeepLevel, currDeepLevel + 1 );
for ( var e = 0; e < deepKeys.length; e++ ) {
// update path backwards
deepKeys[e].path = '["' + curr + '"]' + deepKeys[e].path;
keys.push( deepKeys[e] );
}
} else {

if ( isRegex ? re.test(currElem) : ( isPartialMatch ? currElem.toString().toLowerCase().indexOf(searchValue) != -1 : currElem.toString().toLowerCase() === searchValue ) ){

var r = {};
r.key = curr;
r.value = currElem;
r.path = '["' + curr + '"]';
r.depth = currDeepLevel;
keys.push( r );
if (isFirstMatchOnly) return keys;
}
}
}
return keys;
}
},

// Return JSON object corresponding to path, without using the Evil eval
// path syntax: [key1][key2][key3]...
getJsonObjectFromPath:function (jsonObj, path) {
if (!path || path.length < 4) return jsonObj;
const keys = path.substr(2, path.length - 4).split('"]["');
let result = jsonObj;
keys.forEach(key => result = result[key]);
return result;
}
};

hoverZoom.loadHoverZoom();
132 changes: 2 additions & 130 deletions plugins/startpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,134 +27,6 @@ hoverZoomPlugins.push({
return undefined;
}

// Find key(s) in JSON object and return corresponding value(s) and path(s)
// If key not found then return []
// Search is NOT case-sensitive
// https://gist.github.com/killants/569c4af5f2983e340512916e15a48ac0
function getKeysInJsonObject(jsonObj, searchKey, isRegex, maxDeepLevel, currDeepLevel) {

var bShowInfo = false;

maxDeepLevel = ( maxDeepLevel || maxDeepLevel == 0 ) ? maxDeepLevel : 100;
currDeepLevel = currDeepLevel ? currDeepLevel : 1 ;
isRegex = isRegex ? isRegex : false;

// check RegEx validity if needed
var re;
if (isRegex) {
try {
re = new RegExp(searchKey);
} catch (e) {
cLog(e);
return [];
}
}

if ( currDeepLevel > maxDeepLevel ) {
return [];
} else {

var keys = [];

for ( var curr in jsonObj ) {
var currElem = jsonObj[curr];

if ( currDeepLevel == 1 && bShowInfo ) { cLog("getKeysInJsonObject : Looking property \"" + curr + "\" ") }

if ( isRegex ? re.test(curr) : curr.toLowerCase() === searchKey.toLowerCase() ){
var r = {};
r.key = curr;
r.value = currElem;
r.path = '["' + curr + '"]';
r.depth = currDeepLevel;
keys.push( r );
}

if ( typeof currElem == "object" ) { // object is "object" and "array" is also in the eyes of "typeof"
// search again :D
var deepKeys = getKeysInJsonObject( currElem, searchKey, isRegex, maxDeepLevel, currDeepLevel + 1 );

for ( var e = 0; e < deepKeys.length; e++ ) {
// update path backwards
deepKeys[e].path = '["' + curr + '"]' + deepKeys[e].path;
keys.push( deepKeys[e] );
}
}
}
return keys;
}
}

// Find value(s) in JSON object and return corresponding key(s) and path(s)
// If value not found then return []
// Search is NOT case-sensitive
// ref: https://gist.github.com/killants/569c4af5f2983e340512916e15a48ac0
function getValuesInJsonObject(jsonObj, searchValue, isRegex, isPartialMatch, isFirstMatchOnly, maxDeepLevel, currDeepLevel) {

var bShowInfo = false;

maxDeepLevel = ( maxDeepLevel || maxDeepLevel == 0 ) ? maxDeepLevel : 100;
currDeepLevel = currDeepLevel ? currDeepLevel : 1 ;
isRegex = isRegex ? isRegex : false;

// check RegEx validity if needed
var re;
if ( isRegex ) {
try {
re = new RegExp(searchValue);
} catch (e) {
cLog(e);
return [];
}
} else {
searchValue = searchValue.toString().toLowerCase();
}

if ( currDeepLevel > maxDeepLevel ) {
return [];
} else {

var keys = [];

for ( var curr in jsonObj ) {
var currElem = jsonObj[curr];

if ( currDeepLevel == 1 && bShowInfo ) { cLog("getValuesInJsonObject : Looking property \"" + curr + "\" ") }

if ( typeof currElem == "undefined" ) continue;

if ( typeof currElem == "object" ) { // object is "object" and "array" is also in the eyes of "typeof"
// search again :D
var deepKeys = getValuesInJsonObject( currElem, searchValue, isRegex, isPartialMatch, isFirstMatchOnly, maxDeepLevel, currDeepLevel + 1 );
for ( var e = 0; e < deepKeys.length; e++ ) {
// update path backwards
deepKeys[e].path = '["' + curr + '"]' + deepKeys[e].path;
keys.push( deepKeys[e] );
}
} else {

if ( isRegex ? re.test(currElem) : ( isPartialMatch ? currElem.toString().toLowerCase().indexOf(searchValue) != -1 : currElem.toString().toLowerCase() === searchValue ) ){

var r = {};
r.key = curr;
r.value = currElem;
r.path = '["' + curr + '"]';
r.depth = currDeepLevel;
keys.push( r );
if (isFirstMatchOnly) return keys;
}
}
}
return keys;
}
}

// Return JSON object corresponding to path, without using the Evil eval
// path syntax: [key1][key2][key3]...
function getJsonObjectFromPath(path) {
return new Function('return ' + spJson + path)();
}

//$('link[href*="proxy-image"]:not(.hoverZoomFetched)').addClass('hoverZoomFetched').each(function () {
$('img[src*="proxy-image"]:not(.hoverZoomFetched)').addClass('hoverZoomFetched').each(function () {
let link = $(this);
Expand All @@ -163,9 +35,9 @@ hoverZoomPlugins.push({
// search for thumbnail url among sp data
if (spJson.indexOf(src) == -1) return;

let values = getValuesInJsonObject(spData, src, false, true, true); // look for a partial match & stop after 1st match
let values = hoverZoom.getValuesInJsonObject(spData, src, false, true, true); // look for a partial match & stop after 1st match
if (values.length == 0) return;
let o = getJsonObjectFromPath(values[0].path.substring(0, values[0].path.lastIndexOf('[')));
let o = hoverZoom.getJsonObjectFromPath(spData, values[0].path.substring(0, values[0].path.lastIndexOf('[')));
// extract fullsize url from Object
let fullsizeUrl = o.clickUrl || o.anonImageViewUrl;
fullsizeUrl = fullsizeUrl.replace(/.*piurl=(.*)&sp=.*/, '$1');
Expand Down

0 comments on commit 4441387

Please sign in to comment.