Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
definition_list.inc
1 <?php
18 global $ROOT_PATH;
19 include_once($ROOT_PATH.'wysiwyg_plugin.inc');
20 require_once dirname(__FILE__).'/../../../../core/include/init.inc';
21 
35 {
36 
44  function definition_list(&$wysiwyg)
45  {
46  wysiwyg_plugin::wysiwyg_plugin($wysiwyg);
47  $this->_add_button('matrix_dt','Matrix_DT','Insert Definition Term','false','63');
48  $this->_add_button('matrix_dd','Matrix_DD','Insert Definition Description','false','62');
49 
50  }//end constructor
51 
60  function paint_generic()
61  {
62  ?>
63  <script type="text/javascript" language="Javascript">
64 
65  HTMLArea.prototype._definitionListFilter = function(node) {
66  switch (node.tagName) {
67  case 'P':
68  case 'DT':
69  case 'DD' :
70  return true;
71  break;
72  default :
73  return false;
74  break;
75  }
76  }
77  // Generate the definition list.
78  // This function is called when some text is selected in the WYSIWYG.
79  // It attempts to create the definition list from the selected text.
80  HTMLArea.prototype._definitionListWithSelection = function(type) {
81  var editor = this; // for nested functions
82  var sel = this._getSelection();
83  var range = this._createRange(sel);
84  var parent = this.getParentElement();
85  var type_string = ((type == "DT") ? "term" : "definition");
86 
87  var selected_html = "";
88  if (HTMLArea.is_gecko) {
89  var parent_node = range.startContainer.parentNode;
90 
91  var start_node = range.startContainer;
92  var end_node = range.endContainer;
93  var start_text = "";
94  var end_text = "";
95  if (start_node.nodeType == 1) {
96  start_text = start_node.innerHTML;
97  } else if (start_node.nodeType == 3) {
98  start_text = start_node.nodeValue;
99  }
100  if (end_node.nodeType == 1) {
101  end_text = end_node.innerHTML;
102  } else if (end_node.nodeType == 3) {
103  end_text = end_node.nodeValue;
104  }
105  while (this._definitionListFilter(parent_node)) {
106  parent_node = parent_node.parentNode;
107  }
108  var start_index = 0;
109  var end_index = 0;
110  var children = parent.childNodes;
111  var valid_node_indexes = Array();
112  for (var i=0; i < children.length; i++) {
113  if (children[i].nodeType == 1 && children[i].innerHTML != "") {
114  if (children[i].innerHTML == start_text) start_index = i;
115  else if (children[i].innerHTML == end_text) end_index = i;
116  valid_node_indexes.push(i);
117  } else if (children[i].nodeType == 3) {
118  var tmp = children[i].nodeValue.replace(/\s/g, "");
119  if (tmp != "") {
120  if (tmp == start_text.replace(/\s/g, "")) start_index = i;
121  else if (tmp == end_text.replace(/\s/g, "")) end_index = i;
122  valid_node_indexes.push(i);
123  }
124  }
125  }
126  // Create a definition list
127  dl = this._doc.createElement("DL");
128  var remove_list = Array();
129  for (var j=0; j < valid_node_indexes.length; j++) {
130  var index = valid_node_indexes[j];
131  if (index >= start_index && index <= end_index) {
132  if (children[index].tagName == "DT" || children[index].tagName == "DD") {
133  if (type_string == children[index].tagName) continue;
134  tmp = this._doc.createElement(type);
135  tmp.innerHTML = children[index].innerHTML;
136  parent_node.replaceChild(tmp, children[index]);
137  } else {
138  tmp = this._doc.createElement(type);
139  if (children[index].nodeType == 1 || children[index].nodeType == 3) {
140  switch (children[index].nodeType) {
141  case 1:
142  tmp.innerHTML = children[index].innerHTML;
143  break;
144  case 3:
145  tmp.innerHTML = children[index].nodeValue;
146  break;
147  }
148  dl.appendChild(tmp);
149  remove_list.push(children[index]);
150  }
151  }
152  }
153  }
154  if (dl.childNodes.length > 0) {
155  parent_node.insertBefore(dl, children[start_index]);
156  for (var k=0; k < remove_list.length; k++) {
157  parent_node.removeChild(remove_list[k]);
158  }
159  }
160  } else if (HTMLArea.is_ie) {
161  var selected_html = range.htmlText;
162  var tmp = "";
163  var new_html = "";
164 
165  var e = '<(P|DT|DD)>(.+)<\/(P|DT|DD)>|([^>]+)<BR>|([^>]+)';
166  var re = new RegExp(e, "ig");
167  var p_re = new RegExp('<(P)>([^>]+)<\/(P)>', "ig");
168  var d_re = new RegExp('<(DT|DD)>([^>]+)<\/(DT|DD)>', "ig");
169  var b_re = new RegExp('([^>]+)<BR>', "ig");
170  var n_re = new RegExp('([^>]+)', "ig");
171 
172  var surround_dl = false;
173  var warning = false;
174 
175  var result = selected_html.match(re);
176  for (var i = 0; i < result.length; i++) {
177  var curr_type = result[i].replace(re, "$1");
178 
179  if (result[i].match(p_re)) {
180  tmp += result[i].replace(p_re, "<" + type + ">$2<\/" + type + ">");
181  surround_dl = true;
182  } else if (result[i].match(d_re)) {
183  tmp += result[i].replace(d_re, "<" + type + ">$2<\/" + type + ">");
184  if (surround_dl) {
185  warning = true;
186  break;
187  }
188  } else if (result[i].match(b_re)) {
189  tmp += result[i].replace(b_re, "<" + type + ">$1<\/" + type + ">");
190  surround_dl = true;
191  } else if (result[i].match(n_re)) {
192  tmp += result[i].replace(n_re, "<" + type + ">$1<\/" + type + ">");
193  surround_dl = true;
194  }
195  }
196 
197  if (!warning) {
198  if (surround_dl) {
199  new_html = '<DL>' + tmp + '</DL>';
200  } else if (!surround_dl) {
201  new_html = tmp;
202  }
203  range.pasteHTML(new_html);
204  } else {
205  alert("Please do not select DD or DT tags with other types of tags.");
206  }
207  }
208  }
209 
210  // Generate the definition list.
211  // This function is called when there is no selection in the WYSIWYG.
212  // It creates a new definition list
213  HTMLArea.prototype._definitionList = function(type) {
214  var sel = this._getSelection();
215  var range = this._createRange(sel);
216  var parent = this.getParentElement();
217  var type_string = ((type == "DT") ? "term" : "definition");
218 
219  if (parent.tagName != "DT" && parent.tagName != "DD") {
220 
221  // start a new definition list
222  var dl = this._doc.createElement("dl");
223  var child = this._doc.createElement(type.toLowerCase());
224 
225  var text = this._doc.createTextNode("Type the " + type_string);
226 
227  child.appendChild(text);
228  dl.appendChild(child);
229  parent.appendChild(dl);
230  if (HTMLArea.is_gecko) {
231  range.selectNode(text);
232  //range.collapse(true);
233  sel.removeAllRanges();
234  sel.addRange(range);
235  } else if (HTMLArea.is_ie) {
236  range.findText("Type the " + type_string);
237  range.select();
238  }
239 
240  } else {
241 
242  var dl = parent.parentNode;
243  var dl_parent = dl.parentNode;
244  if (HTMLArea.is_gecko) {
245  var data = parent.firstChild.data;
246  } else if (HTMLArea.is_ie) {
247  var data = parent.innerText;
248  }
249  if (type == parent.tagName) {
250  // the same button is pressed, remove any tag
251  dl.removeChild(parent);
252  if (data != null && data != "Type the " + type_string) {
253  var new_text = this._doc.createTextNode(data);
254  dl_parent.appendChild(new_text);
255  }
256  if (dl.childNodes.length == 0) {
257  dl_parent.removeChild(dl);
258  }
259  } else if (type != parent.tagName) {
260  // the different tag is pressed, add a new one
261  var new_child = this._doc.createElement(type);
262  var next_type_string = ((type == "DT") ? "term" : "definition");
263  var prev_type_string = ((type == "DT") ? "definition" : "term");
264  if (data != ("Type the " + prev_type_string)) {
265  var new_text = this._doc.createTextNode(data);
266  } else {
267  var new_text = this._doc.createTextNode("Type the " + next_type_string);
268  }
269 
270  new_child.appendChild(new_text);
271  dl.replaceChild(new_child, parent);
272  }
273  }
274  }
275  HTMLArea.prototype._handleDefinitionShortcut = function() {
276  var editor = this; // for nested functions
277  var sel = this._getSelection();
278  var range = this._createRange(sel);
279  var parent = this.getParentElement();
280 
281  var dl = parent.parentNode;
282  if (HTMLArea.is_gecko) {
283  var data = parent.firstChild.data;
284  } else if (HTMLArea.is_ie) {
285  var data = parent.innerText;
286  }
287 
288  var next_type = (parent.tagName == "DT") ? "DD" : "DT";
289  var prev_type_string = ((next_type == "DT") ? "definition" : "term");
290  var type_string = ((next_type == "DT") ? "term" : "definition");
291 
292  if (dl.tagName == "DL") {
293  if (data == "" || data == "Type the " + prev_type_string) {
294  // exit
295  var dl_parent = dl.parentNode;
296  dl.removeChild(parent);
297  if (data != null && data != "Type the " + prev_type_string) {
298  var new_text = this._doc.createTextNode(data);
299  dl_parent.appendChild(new_text);
300  }
301  if (dl.childNodes.length == 0) {
302  dl_parent.removeChild(dl);
303  }
304  } else {
305  var new_child = this._doc.createElement(next_type.toLowerCase());
306  var new_text = this._doc.createTextNode("Type the " + type_string);
307  new_child.appendChild(new_text);
308  dl.appendChild(new_child);
309 
310  if (HTMLArea.is_gecko) {
311  // open a new tag
312  range.selectNode(new_text);
313  sel.removeAllRanges();
314  sel.addRange(range);
315  } else if (HTMLArea.is_ie) {
316  range.findText("Type the " + type_string);
317  range.select();
318  }
319  }
320  }
321  }
322 
323  </script>
324  <?php
325 
326  }//end paint_generic()
327 
328 
336  {
337  ?>
338 
339  case "matrix_dt":
340  var sel = this._getSelection();
341  var range = this._createRange(sel);
342  var range_comp = "";
343  if (HTMLArea.is_gecko) {
344  range_comp = range;
345  } else if (HTMLArea.is_ie) {
346  range_comp = range.text;
347  }
348  if (range_comp == "" || range_comp == "Type the term" || range_comp == "Type the definition") {
349  this._definitionList("DT");
350  } else {
351  this._definitionListWithSelection("DT");
352  }
353  break;
354  case "matrix_dd":
355  var sel = this._getSelection();
356  var range = this._createRange(sel);
357  var range_comp = "";
358  if (HTMLArea.is_gecko) {
359  range_comp = range;
360  } else if (HTMLArea.is_ie) {
361  range_comp = range.text;
362  }
363  if (range_comp == "" || range_comp == "Type the term" || range_comp == "Type the definition") {
364  this._definitionList("DD");
365  } else {
366  this._definitionListWithSelection("DD");
367  }
368  break;
369  <?php
370 
371  }//end print_plugin_button_click()
372 
373 
383  {
384  ?>
385  case 'd':
386  this._handleDefinitionShortcut();
387  HTMLArea._stopEvent(ev);
388  break;
389  <?php
390 
391  }//end print_plugin_shortcuts()
392 
393 
394 }//end class
395 
396 ?>