Projekt

Obecné

Profil

Stáhnout (3.17 KB) Statistiky
| Větev: | Tag: | Revize:
1

    
2

    
3
function createElement(element) {
4
  if (typeof document.createElementNS != 'undefined') {
5
    return document.createElementNS('http://www.w3.org/1999/xhtml', element);
6
  }
7
  if (typeof document.createElement != 'undefined') {
8
    return document.createElement(element);
9
  }
10
  return false;
11
}
12

    
13
function mySplit(string, char) {
14
  var position = string.indexOf(char);
15
  if (position == -1) {
16
    return string;
17
  }
18
  else {
19
    var value = new Array(2);
20
    value[0] = string.substring( 0,position );
21
    value[1] = string.substring( position+1 );
22
    return value;
23
  }
24
}
25

    
26
function clearElement(element) {
27
  var toRemove = element.firstChild;
28

    
29
  try {
30
    while(element.firstChild) {
31
      element.removeChild(element.firstChild);
32
    }
33
  } 
34
  catch (e) { // IE hack
35
    toRemove.ParentNode.removeChild(toRemove); 
36
  }
37
}
38

    
39
function insertNode(place, node) {
40

    
41
  clearElement(place);
42

    
43
  place.appendChild(node);
44

    
45
}
46

    
47
function getOption(value, text) {
48
  var option = createElement('option');
49
  option.value = value;
50
  option.appendChild(document.createTextNode(text));
51
  
52
  return option;
53
}
54

    
55
function getSelect(selectCode) {
56
  var select = createElement('select');
57
  
58
  var options = selectCode.split('/');
59
  for(i=0; i < options.length; i++){
60
    select.appendChild(getOption(options[i], options[i]));
61
  }
62
  
63
  select.setAttribute("class", "arabic");
64
  
65
  //alert('yeahhh');
66
  return select;
67
}
68

    
69
function createNl2Br(inputText) {
70
  var parentNode = createElement('span');
71
  var tokens = inputText.split('\n');
72
  
73
  for(var i = 0; i < tokens.length; i++){
74
  	if(i != 0) {
75
  	  parentNode.appendChild(createElement('br'));
76
  	}
77
    parentNode.appendChild(document.createTextNode(tokens[i]));
78
  }
79
  return parentNode;
80
}
81

    
82
function createTree(code) {
83

    
84
  var currentText = code;
85
  
86
  var parent = createElement('div');
87

    
88
  while (currentText.indexOf('{') != -1) {
89
    
90
    prvniDeleni = mySplit( currentText, '{' );
91
    druheDeleni = mySplit( prvniDeleni[1], '}' );
92

    
93
    pretext = prvniDeleni[0];
94
    selectCode = druheDeleni[0];
95
    currentText = druheDeleni[1];
96
    
97
    parent.appendChild(createNl2Br( pretext ));
98
    parent.appendChild(getSelect( selectCode ));
99
    
100
  }
101
  
102
  parent.appendChild(createNl2Br( currentText ));
103
  return parent;
104
}
105

    
106
function render(fromName, toName) {
107
  //alert(objekt);
108
  to = document.getElementById(toName);
109
  from = document.getElementById(fromName);
110
  insertNode(to, createTree(from.value));
111
  //document.getElementById('platno');
112
}
113

    
114
function doGetCaretPosition (ctrl) {
115
  var CaretPos = 0;
116
  // IE Support
117
  if (document.selection) {
118
    ctrl.focus ();
119
    var Sel = document.selection.createRange ();
120
    Sel.moveStart ('character', -ctrl.value.length);
121
    CaretPos = Sel.text.length;
122
  }
123
  // Firefox support
124
  else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
125
    CaretPos = ctrl.selectionStart;
126
  }
127
  return (CaretPos);
128
}
129

    
130
function insertAtCaret (textArea, text) {
131
  var caretPos = doGetCaretPosition(textArea);
132
  textArea.value = textArea.value.substring(0, caretPos) + text + textArea.value.substring( caretPos);
133
}
134

    
135
function addSelect(kam) {
136
  var textarea = document.getElementById(kam);
137
  var text = '{right/x/y}';
138
  insertAtCaret(textarea, text);
139
  textarea.focus();
140
  render(kam, 'platno');
141
}
(1-1/3)