clean up helper file

pull/1097/merge
johnyma22 2012-10-03 20:56:52 +01:00
parent 649a28b6c6
commit 54a77458d6
1 changed files with 67 additions and 57 deletions

View File

@ -1,80 +1,90 @@
var testHelper = {}; var testHelper = {};
(function(){ (function () {
var $iframeContainer = $("#iframe-container"), $iframe; var $iframeContainer = $("#iframe-container"),
$iframe;
testHelper.randomString = function randomString(len) testHelper.randomString = function randomString(len) {
{ var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var randomstring = '';
var randomstring = ''; for (var i = 0; i < len; i++) {
for (var i = 0; i < len; i++) var rnum = Math.floor(Math.random() * chars.length);
{ randomstring += chars.substring(rnum, rnum + 1);
var rnum = Math.floor(Math.random() * chars.length); }
randomstring += chars.substring(rnum, rnum + 1); return randomstring;
} }
return randomstring;
}
testHelper.newPad = function(cb){ testHelper.newPad = function (cb) {
var padName = "FRONTEND_TEST_" + testHelper.randomString(20); var padName = "FRONTEND_TEST_" + testHelper.randomString(20);
$iframe = $("<iframe src='/p/" + padName + "'></iframe>") $iframe = $("<iframe src='/p/" + padName + "'></iframe>")
$iframeContainer.empty().append($iframe); $iframeContainer.empty()
.append($iframe);
var checkInterval; var checkInterval;
$iframe.load(function(){ $iframe.load(function () {
checkInterval = setInterval(function(){ checkInterval = setInterval(function () {
var loaded = false; var loaded = false;
try { try {
//check if loading div is hidden //check if loading div is hidden
loaded = !testHelper.$getPadChrome().find("#editorloadingbox").is(":visible"); loaded = !testHelper.$getPadChrome()
} catch(e){} .find("#editorloadingbox")
.is(":visible");
}
catch (e) {}
if(loaded){ if (loaded) {
clearTimeout(timeout); clearTimeout(timeout);
clearInterval(checkInterval); clearInterval(checkInterval);
cb(null, {name: padName}); cb(null, {
} name: padName
}, 100); });
}); }
}, 100);
});
var timeout = setTimeout(function(){ var timeout = setTimeout(function () {
if(checkInterval) clearInterval(checkInterval); if (checkInterval) clearInterval(checkInterval);
cb(new Error("Pad didn't load in 10 seconds")); cb(new Error("Pad didn't load in 10 seconds"));
}, 10000); }, 10000);
return padName; return padName;
} }
testHelper.$getPadChrome = function(){ testHelper.$getPadChrome = function () {
return $iframe.contents() return $iframe.contents()
} }
testHelper.$getPadOuter = function(){ testHelper.$getPadOuter = function () {
return testHelper.$getPadChrome().find('iframe.[name="ace_outer"]').contents(); return testHelper.$getPadChrome()
} .find('iframe.[name="ace_outer"]')
.contents();
}
testHelper.$getPadInner = function(){ testHelper.$getPadInner = function () {
return testHelper.$getPadOuter().find('iframe.[name="ace_inner"]').contents(); return testHelper.$getPadOuter()
} .find('iframe.[name="ace_inner"]')
.contents();
}
// copied from http://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse // copied from http: //stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse
testHelper.selectText = function(element){ testHelper.selectText = function (element) {
var doc = document, range, selection; var doc = document,
range, selection;
if (doc.body.createTextRange) { //ms if (doc.body.createTextRange) { //ms
range = doc.body.createTextRange(); range = doc.body.createTextRange();
range.moveToElementText(element); range.moveToElementText(element);
range.select(); range.select();
} else if (window.getSelection) { //all others
selection = window.getSelection();
range = doc.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
} }
} else if (window.getSelection) { //all others
selection = window.getSelection();
range = doc.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
}
})() })()