1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-21 05:51:41 +02:00

Fix issue in Selectize.js processwire/processwire-issues#520 where Selectize.js had bottleneck in measureString() function that caused a slowdown because of DOM manipulation (was noticed when Selectize paired with Uikit 3)

This commit is contained in:
Ryan Cramer
2018-03-09 09:17:04 -05:00
parent 5da4e17a27
commit 36bceb6ed2
2 changed files with 32 additions and 18 deletions

View File

@@ -993,10 +993,15 @@
} }
$to.css(styles); $to.css(styles);
}; };
// since measureString seems to get called multiple times for each event, keep a cache (added by ProcessWire)
var measureStringData = { str: '', width: 0 };
/** /**
* Measures the width of a string within a * Measures the width of a string within a
* parent element (in pixels). * parent element (in pixels).
*
* (updated by ProcessWire to avoid bottleneck of adding & deleting element from DOM on every call)
* *
* @param {string} str * @param {string} str
* @param {object} $parent * @param {object} $parent
@@ -1007,25 +1012,34 @@
return 0; return 0;
} }
var $test = $('<test>').css({ var $test = $('#selectize-measureString');
position: 'absolute',
top: -99999,
left: -99999,
width: 'auto',
padding: 0,
whiteSpace: 'pre'
}).text(str).appendTo('body');
transferStyles($parent, $test, [ if(!$test.length) {
'letterSpacing',
'fontSize', $test = $('<test>').attr('id', 'selectize-measureString').css({
'fontFamily', position: 'absolute',
'fontWeight', top: -99999,
'textTransform' left: -99999,
]); width: 'auto',
padding: 0,
whiteSpace: 'pre'
}).appendTo('body');
transferStyles($parent, $test, [
'letterSpacing',
'fontSize',
'fontFamily',
'fontWeight',
'textTransform'
]);
}
var width = $test.width(); if(str === measureStringData.str) {
$test.remove(); var width = measureStringData.width;
} else {
$test.text(str);
var width = $test.width();
}
return width; return width;
}; };

File diff suppressed because one or more lines are too long