This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
71 lines (63 loc) · 2.01 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//**************************
// add some functionality
//**************************
//create and add buttons for common atg xml tags
addButton(createButton("query items", addQueryItems));
addButton(createButton("add item", addItem));
addButton(createButton("remove item", removeItem));
addButton(createButton("print item", printItem));
//get textarea
function getTextarea(){
var textarea = document.getElementsByName("xmltext")[0];
return textarea;
}
/**
This function create button and add it to element.
*/
function createButton(name, action){
var button = document.createElement("input");
button.type = "button";
button.value = name;
button.onclick = action;
button.setAttribute('id', 'custom_' + name);
return button;
}
function addButton(button){
var root = getTextarea().parentNode;
var div = root.getElementsByTagName("div");
if(div.length>0){
div[0].appendChild(button);
}else{
div = document.createElement("div");
root.insertBefore(div, getTextarea());
div.appendChild(button);
}
}
function addQueryItems(){
//console.log("addQueryItems");
var oldData = getTextarea().value;
getTextarea().value = oldData+"<query-items item-descriptor=\"\"></query-items>";
setCursor(getTextarea(), oldData.length+30, oldData.length+30);
}
function addItem(){
//console.log("addItem");
var oldData = getTextarea().value;
getTextarea().value = oldData+"<add-item item-descriptor=\"\"></add-item>";
setCursor(getTextarea(), oldData.length+27, oldData.length+27);
}
function removeItem(){
//console.log("removeItem");
var oldData = getTextarea().value;
getTextarea().value = oldData+"<remove-item item-descriptor=\"\" id=\"\" />";
setCursor(getTextarea(), oldData.length+30, oldData.length+30);
}
function printItem(){
//console.log("printItem");
var oldData = getTextarea().value;
getTextarea().value = oldData+"<print-item item-descriptor=\"\" id=\"\" />";
setCursor(getTextarea(), oldData.length+29, oldData.length+29);
}
function setCursor(el, startPos, EndPos){
el.focus();
el.setSelectionRange(startPos,EndPos);
}