I am having two pages. The next button in first pages brings the second page. The focus in second page is always moving down. So I need to use scroll bar to bring the bring the cursor top. I want to bring the focus to the top. my focus-handler.js is as follows:
var lastFocusedControlId = "";
function focusHandler(e) {
document.activeElement = e.originalTarget;
}
function appInit() {
if (typeof (window.addEventListener) !== "undefined") {
window.addEventListener("focus", focusHandler, true);
}
setFirstControl()
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);
}
function pageLoadingHandler(sender, args) {
lastFocusedControlId = typeof (document.activeElement) === "undefined"
? "" : document.activeElement.id;
}
function focusControl(targetControl) {
if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
var focusTarget = targetControl;
if (focusTarget && (typeof (focusTarget.contentEditable) !== "undefined")) {
oldContentEditableSetting = focusTarget.contentEditable;
focusTarget.contentEditable = false;
}
else {
focusTarget = null;
}
try {
targetControl.focus();
if (focusTarget) {
focusTarget.contentEditable = oldContentEditableSetting;
}
}
catch (err) { }
}
else {
targetControl.focus();
}
}
function pageLoadedHandler(sender, args) {
if (typeof (lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") {
var newFocused = $get(lastFocusedControlId);
if (newFocused) {
focusControl(newFocused);
}
}
}
function setFirstControl() {
var bFound = false;
// for each form
for (f = 0; f < document.forms.length; f++) {
// for each element in each form
for (i = 0; i < document.forms[f].length; i++) {
// if it's not a hidden element
if (document.forms[f][i].type != "hidden") {
// and it's not disabled
if (document.forms[f][i].disabled != true) {
try {
// set the focus to it
document.forms[f][i].focus();
var bFound = true;
}
catch (er) {
}
}
}
// if found in this element, stop looking
if (bFound == true)
break;
}
// if found in this form, stop looking
if (bFound == true)
break;
}
}
Sys.Application.add_init(appInit);
Related
I got these two functions, and they work great.
But since I only call global.replaceFields from global.translateAll then I want to get rid of global.replaceFields and put its functionality inside global.translateAll
How would you go about merging global.replaceFields into global.translateAll without losing the current functionality?
Thanks :)
// Translate everything in that field
global.translateAll = (textfield, usersLanguage) => {
for (var property in textfield) {
if (!textfield.hasOwnProperty(property)) {
return false;
} else if (typeof textfield[property] !== "object") {
textfield[property] = global.replaceFields(textfield[property], usersLanguage);
} else {
global.translateAll(textfield[property], usersLanguage);
}
}
}
// Translate everything in that field
global.replaceFields = (textfield, usersLanguage) => {
// Keep running until all fields are replaced
while (textfield.indexOf("{{") != -1) {
// isolate the field
let fromField = textfield.substring((textfield.indexOf("{{") + 2), (textfield.indexOf("}}")));
let toField = ""
// If its a translated text
if (fromField.indexOf("trans") != -1) {
toField = usersLanguage[fromField];
textfield = textfield.replace("{{" + fromField + "}}", toField);
}
}
return (textfield);
}
This should work
global.translateAll = (textfield, usersLanguage) => {
var replaceFields = (textfield, usersLanguage) => {
// Keep running until all fields are replaced
while (textfield.indexOf("{{") != -1) {
// isolate the field
let fromField = textfield.substring((textfield.indexOf("{{") + 2), (textfield.indexOf("}}")));
let toField = ""
// If its a translated text
if (fromField.indexOf("trans") != -1) {
toField = usersLanguage[fromField];
textfield = textfield.replace("{{" + fromField + "}}", toField);
}
}
return (textfield);
}
for (var property in textfield) {
if (!textfield.hasOwnProperty(property)) {
return false;
} else if (typeof textfield[property] !== "object") {
textfield[property] = replaceFields(textfield[property], usersLanguage);
} else {
global.translateAll(textfield[property], usersLanguage);
}
}
}
This is a Binary Tree I've made.
The problem: When i am deleting a data, it doesn't stop running when going from Left to Right, so it only goes in one direction. it deletes properly when it deletes all Left only and also, all Right only.
This runs properly:
BST.insert(10);
BST.insert(9);
BST.insert(8);
BST.insert(7);
BST.insert(6);
BST.insert(5);
BST.insert(4);
BST.Delete(4);
or
BST.insert(10);
BST.insert(11);
BST.insert(12);
BST.insert(13);
BST.insert(14);
BST.Delete(14);
but if you run this it stays in the loop like all conditions inside are false
BST.insert(10)
BST.insert(5)
BST.insert(8)
BST.insert(7)
BST.insert(6)
BST.insert(5)
BST.Delete(6)
You can try it yourself:
var BST = (function(){
// PRIVATE
var root = null;
// NODE OBJECT
var Node = function (data, left, right) {
this.data = data;
this.left = left;
this.right = right;
}
// PUBLIC
return {
insert: function(data) {
const node = new Node(data,null, null);
if (root === null) {
root = node;
} else {
let current = root;
while (true) {
if (current.data > data) {
if (current.left === null) {
current.left = node;
console.log(root+ " 1");
break;
}
current = current.left;
} else {
if (current.right === null) {
current.right = node;
console.log(root+ " 2");
break;
}
current = current.right;
}
}
}
},
Delete: function(data) {
if (root === null) {
return console.log("Nothings in Tree");
} else if (root.data === data ) {
root = null;
return console.log("found values");
}
// condition
let current = root;
while (true) {
//CHECK If a value exist to the left or right
if (current.data > data) {
if (current.left === null) {
return console.log("Could not find value");
}
} else if (current.data < data) {
if (current.right === null) {
return console.log("Could not find value");
}
}
// if left exist
if(current.left !== null){
if (current.left.data === data) { // if found data
console.log(`${data} has been remove from the node`);
current.left = null;
break;
} else if (current.left.data > data) {
//MOVE to the left==============
console.log(current.left.data+ " .Great");
current = current.left;// moves to the left
}
}
if (current.right !== null) {
if (current.right.data === data) {
console.log(`${data} has been remove from the node`);
current.right = null;
break;
} else if (current.right.data < data) {
//MOVE to the right==============
console.log(current.right.data +".Less");
current = current.right;
}
}
}
},
show: function() {
console.log(root);
},
test: function() {
console.log("test is working");
}
}
})();
It appears that i have over look a condition.
// if left exist
if(current.left !== null){
if (current.left.data === data) { // if found data
console.log(`${data} has been remove from the node`);
current.left = null;
break;
} else if (current.left.data > data) {
//MOVE to the left==============
console.log(current.left.data+ " .Great");
current = current.left;// moves to the left
}
}
The "else if (current.left.data > data)" should be replace with just "else" same with the right side. The answer is below.
var BST = (function(){
// PRIVATE
var root = null;
// NODE OBJECT
var Node = function (data,left,right){
this.data = data;
this.left = left;
this.right = right;
}
/*function currentToNull(current, data){
if(current === null){ // repeated operation
current = data;
}
}*/
// PUBLIC
return {
insert: function(data){
const node = new Node(data,null, null);
if(root === null){
root = node;
}else{
let current = root;
while(true){
if(current.data > data){
if(current.left === null){
current.left = node;
console.log(root+ " 1");
break;
}
current = current.left;
}else{
if(current.right === null){
current.right = node;
console.log(root+ " 2");
break;
}
current = current.right;
}
}
}
},
Delete: function(data){
if(root === null){
return console.log("Nothings in Tree");
}
// condition
let current = root, right_leaf, left_leaf;
while(true){
right_leaf = current.right; // keep track of left
left_leaf =current.left; // keep track of right
//CHECK If a value exist to the left or right
if(current.data > data){
if(left_leaf === null){
return console.log("Could not find value");
}
}else if(current.data < data){
if(right_leaf === null){
return console.log("Could not find value");
}
} else if(current.data === data){
current = null;
return console.log("data found");
}
if(left_leaf !== null){// L Leaf Data exist
if(left_leaf.data === data){ // if found data
console.log(`${data} has been remove from the node`);
current.left = null;
break;
}else if(left_leaf.data > data){ //MOVE to the left======================
console.log(" Move Left of left");
current = current.left;// moves to the left
} else if(left_leaf.data < data ){
console.log(" Move Right of left");
current = left_leaf;
}
}
if (right_leaf !== null){
if(right_leaf.data === data){
console.log(`${data} has been remove from the node`);
right_leaf = null;
break;
}else if(right_leaf.data < data){ //MOVE to the right=======================
console.log(" Move Right of right");
current = right_leaf;
} else if(right_leaf.data > data ){
console.log(" Move Left of right");
current = right_leaf;
}
}
}
}
,
show: function(){
console.log(root);
},
test: function (){
console.log("test is working");
}
}
})();
//show();
I tried to write a code that prevents 2 creeps (harvester) from going to the same destination and binds them to that destination until its full.
on line 55 I get an error which is understandable
55: creep.memory.targetID = targets[checkTarget].id;
for a reason that I am not seeing targets[checkTarget] is null
Can anyone tell me what i am doing wrong?
var roleHarvester = {
/** #param {Creep} creep **/
run: function(creep) {
if (!creep.memory.targetID) creep.memory.targetID = "not given";
console.log(creep.memory.targetID);
if (creep.carry.energy < creep.carryCapacity) {
var nearestSource = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
if (creep.harvest(nearestSource) == ERR_NOT_IN_RANGE) {
creep.moveTo(nearestSource, {
visualizePathStyle: {
stroke: '#ffaa00'
}
});
}
} else {
if (creep.memory.targetID != "not given") {
console.log("should not be not given");
if (Game.getObjectById(creep.memory.targetID).hits == Game.getObjectById(creep.memory.targetID).hitsMax) {
creep.memory.targetID = "not given";
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_TOWER) && structure.energy < structure.energyCapacity;
}
});
} else {
if (creep.transfer(Game.getObjectById(creep.memory.targetID), RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(Game.getObjectById(creep.memory.targetID), {
visualizePathStyle: {
stroke: '#ffffff'
}
});
}
}
} else {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_TOWER) && structure.energy < structure.energyCapacity;
}
});
console.log(targets);
for (var checkTarget in targets) {
console.log(checkTarget);
console.log(targets[checkTarget]);
for (var name in Game.creeps) {
if (targets[checkTarget] = Game.getObjectById(Game.creeps[name].memory.targetID)) {
var fail = true;
break;
}
var fail = false;
}
if (fail == true) continue;
console.log(Game.getObjectById(targets[checkTarget]));
creep.memory.targetID = targets[checkTarget].id;
break;
}
}
}
}
};
module.exports = roleHarvester;
Something wrong in the following if condition:
for(var name in Game.creeps) {
if(targets[checkTarget] = Game.getObjectById(Game.creeps[name].memory.targetID)){
Probably you mean == ?
I currently have this code built in JS, but it's really, really ugly.
Is there any better way to approach it?
The way it works basically is pushing a string like app.chat.test to be the key, and value like teststr.
I test the lengths to see if the "parent" key is there, otherwise we build it.
function constructJson(jsonKey, jsonValue){
//REWRITE!!!!!!!!
let jsonObj = langFile;
let jsonKeyArr = jsonKey.split('.')
if (jsonKeyArr.length === 1) {
if (valToAdd === undefined) {
if (jsonObj[jsonKey] === undefined) {
jsonObj[jsonKey] = {}
}
} else {
if (jsonObj[jsonKey] === undefined) {
jsonObj[jsonKey] = valToAdd
}
}
} else if (jsonKeyArr.length === 2) {
if (jsonObj[jsonKeyArr[0]] === undefined) {
jsonObj[jsonKeyArr[0]] = {}
}
if (jsonObj[jsonKeyArr[0]][jsonKeyArr[1]] === undefined) {
jsonObj[jsonKeyArr[0]][jsonKeyArr[1]] = jsonValue
}
} else if (jsonKeyArr.length === 3) {
if (jsonObj[jsonKeyArr[0]] === undefined) {
jsonObj[jsonKeyArr[0]] = {}
}
if (jsonObj[jsonKeyArr[0]][jsonKeyArr[1]] === undefined) {
jsonObj[jsonKeyArr[0]][jsonKeyArr[1]] = {}
}
if (jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]] === undefined) {
jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]] = jsonValue
}
} else if (jsonKeyArr.length === 4) {
if (jsonObj[jsonKeyArr[0]] === undefined) {
jsonObj[jsonKeyArr[0]] = {}
}
if (jsonObj[jsonKeyArr[0]][jsonKeyArr[1]] === undefined) {
jsonObj[jsonKeyArr[0]][jsonKeyArr[1]] = {}
}
if (jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]] === undefined) {
jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]] = {}
}
if (jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]][jsonKeyArr[3]] === undefined) {
jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]][jsonKeyArr[3]] = jsonValue
}
} else if (jsonKeyArr.length === 5) {
if (jsonObj[jsonKeyArr[0]] === undefined) {
jsonObj[jsonKeyArr[0]] = {}
}
if (jsonObj[jsonKeyArr[0]][jsonKeyArr[1]] === undefined) {
jsonObj[jsonKeyArr[0]][jsonKeyArr[1]] = {}
}
if (jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]] === undefined) {
jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]] = {}
}
if (jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]][jsonKeyArr[3]] === undefined) {
jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]][jsonKeyArr[3]] = {}
}
if (jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]][jsonKeyArr[3]][jsonKeyArr[4]] === undefined) {
jsonObj[jsonKeyArr[0]][jsonKeyArr[1]][jsonKeyArr[2]][jsonKeyArr[3]][jsonKeyArr[4]] = jsonValue
}
} else if (jsonKeyArr.length > 5) {
return console.log("Length over 5 not supported yet!")
}
return jsonObj;
}
Regards.
OF course it's possible, a simple loop will perfeclty do the job.
function constructJson(jsonKey, jsonValue){
//REWRITE!!!!!!!!
langFile = {a:{}, foo:{}};// remove this for your own code
var jsonObj = langFile;
var jsonKeyArr = jsonKey.split('.');
var currentValue = jsonObj;
for(var i = 0; i < jsonKeyArr.length;i++){
if(currentValue[jsonKeyArr[i]]===undefined){
currentValue[jsonKeyArr[i]] = {};
}
if(i < jsonKeyArr.length-1){
currentValue = currentValue[jsonKeyArr[i]];
}else{
currentValue[jsonKeyArr[i]] = jsonValue;
}
}
return jsonObj;
}
alert(JSON.stringify(constructJson("a.b.cd.ef", "toto")));
I just assigning to a temporary variable each sublevel. When i'm on the last i'm assigning the value.
Yes you can, using the javascript reduce function on the array created from the splitted string.
function namespaceCreateExceptLast(representationOfElementToCreate, baseNamespace) {
var tokens;
if (typeof representationOfElementToCreate !== 'string')
throw new Error('Expecting string as first parameter');
if (baseNamespace === undefined)
baseNamespace = window;
tokens = representationOfElementToCreate.split('.');
// Remove the last element (which will contain the value)
tokens.pop();
// Use reduce to create part by part your new object
return tokens.reduce(function (prev, property) {
if (typeof prev !== 'object') {
throw Error('One property is already defined but not an object, namespace creation has failed', property);
return undefined;
} else {
if (!prev[property])
prev[property] = {};
return prev[property];
}
}, baseNamespace);
};
Then you can have:
function constructJson(jsonKey, jsonValue){
let jsonObj = langFile;
var lastItem = namespaceCreateExceptLast(jsonKey, jsonObj);
var lastKey = jsonKey.substring(jsonKey.lastIndexOf('.') + 1);
lastItem[lastKey] = jsonValue;
}
I have added some comments and exceptions to help you understand how it's done, but it's mainly based on the reduce function which you can easily get help for (https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/reduce).
I couldn't find out why my code doesn't work correctly
I have this function:
function checkIt(input_name, input_value){
if (input_name === 'firstname') {
y = regs[0];
} else if (input_name === 'lastname') {
y = regs[1];
} else if (input_name === 'phone') {
y = regs[2];
} else if (input_name === 'mail') {
y = regs[3];
} else {
y = regs[4];
}
z = input_value.match(y1);
if (z !== null) {
rez = true;
} else {
rez = false;
}
return rez;
}
and then im doing this:
$(document).on("blur", "input[name=firstname]", function(){
p = $(this).prop("name");
q = $(this).val();
r = checkIt(p, q);
if (r) {
// something
} else {
// something
}
});
and my problem is that my code doesn't work on first blur. it's starting executing on second blur and does everything perfecty... :/
try using on("blur", "input", function) without parameter name, because do u have the var p with prop of this input.
$(document).on("blur", "input", function(){
p = $(this).prop("name");
q = $(this).val();
r = checkIt(p, q);
if (r) {
// something
} else {
// something
}
});