Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
matrix_expand_keywords.inc
1 <?php
18 global $ROOT_PATH;
19 include_once($ROOT_PATH.'wysiwyg_plugin.inc');
20 
35 {
36 
47  {
48  ?>
49  if ((ev.type == "keypress")) {
50  var key = (HTMLArea.is_ie ? ev.keyCode : ev.charCode);
51  if ((key == 32) || (key == 13) || (key == 9) || (key == 0)) {
52  expandKeywords(editor);
53  }
54  } else if (ev.type == "keydown") {
55  var key = (HTMLArea.is_ie ? ev.keyCode : ev.charCode);
56  if ((key == 0)) {
57  expandKeywords(editor);
58  }
59  }
60 
61  <?php
62 
63  }//end print_plugin_event_handler()
64 
73  function paint_generic()
74  {
75  if (!$GLOBALS['SQ_SYSTEM']->am->installed('thesaurus')) {
76  ?>
77  <script type="text/javascript"> function expandKeywords() {} </script>
78  <?php
79  return;
80  }
81  $thesaurusid = $GLOBALS['SQ_SYSTEM']->getUserPrefs('content_type_wysiwyg','SQ_THESAURUS');
82  if (!$thesaurusid) {
83  ?>
84  <script type="text/javascript"> function expandKeywords() {} </script>
85  <?php
86  return;
87  }
88 
89  $thesaurus = &$GLOBALS['SQ_SYSTEM']->am->getAsset($thesaurusid, '', true);
90  if (!$thesaurus->id) {
91  ?>
92  <script type="text/javascript"> function expandKeywords() {} </script>
93  <?php
94  return;
95  }
96 
97  $abbrevs = $thesaurus->getAbbreviations();
98  if (!is_array($abbrevs) || empty($abbrevs)) {
99  ?>
100  <script type="text/javascript"> function expandKeywords() {} </script>
101  <?php
102  return;
103  }
104 
105  ?>
106  <script type="text/javascript">
107  var abbrevs = Array();
108 
109  <?php
110  foreach ($abbrevs as $abbrev => $word) {
111  echo " abbrevs['$abbrev'] = '$word';\n";
112  }
113  ?>
114 
115  var stripTagsRE = /(<\S[^>]*>)|(\W)/g; // yes, for some reason this cannot have quotes around it
116 
117  HTMLArea.prototype.replaceLastWord = function(oldWord, newWord) {
118  switch (this._editMode) {
119  case "wysiwyg":
120  if (HTMLArea.is_gecko) {
121  var range = this._createRange(this._getSelection());
122  if (range.startContainer.data.replace(new RegExp(/\s+$/), '') == '') {
123  var contentNode = range.startContainer.parentNode.previousSibling.firstChild;
124  range.selectNodeContents(contentNode);
125  range.setEnd(range.endContainer, range.endContainer.data.length);
126  range.setStart(range.startContainer, range.startContainer.data.length-oldWord.length);
127  } else {
128  range.setStart(range.startContainer, range.startOffset-oldWord.length-1);
129  }
130  // remove the current selection
131  range.deleteContents();
132  toBeInserted = document.createTextNode(newWord+' ');
133  var node = range.startContainer;
134  var pos = range.startOffset;
135  node.insertData(pos, toBeInserted.data);
136  range.setEnd(node, pos + newWord.length + 1);
137  range.setStart(node, pos + newWord.length + 1);
138  } else if (HTMLArea.is_ie) {
139  var doc = this._docContent;
140  while (doc.nodeName != 'BODY') {
141  doc = doc.parentNode;
142  }
143  doc = doc.parentNode.parentNode;
144  var rng = doc.selection.createRange();
145  rng.moveStart('character', -1 * oldWord.length);
146  rng.text = newWord;
147  }
148  break;
149  case "textmode":
150  // code me!
151  break;
152  }
153  }
154 
155  function expandKeywords(editor)
156  {
157  var lastWord = getLastWord(editor);
158  if (((newWord = abbrevs[lastWord]) != null) && (typeof newWord == 'string')) {
159  if (confirm(newWord + '?')) {
160  editor.replaceLastWord(lastWord, newWord);
161  } else {
162  delete abbrevs[lastWord];
163  }
164  }
165  }
166 
167  function getLastWord(editor)
168  {
169  if (HTMLArea.is_gecko) {
170  var range = editor._createRange(editor._getSelection());
171  var contentNode = range.startContainer;
172  var html = contentNode.nodeValue.substr(0, range.endOffset);
173  html = html.replace(new RegExp(/\s+$/), ''); // strip trailing spaces
174  if (html == '') {
175  html = contentNode.parentNode.previousSibling.innerHTML;
176  html = html.replace(new RegExp(/\s+$/), ''); // strip trailing spaces
177  }
178  return html.substring(html.lastIndexOf(" ")+1);
179  } else {
180  var doc = editor._docContent;
181  while (doc.nodeName != 'BODY') {
182  doc = doc.parentNode;
183  }
184  doc = doc.parentNode.parentNode;
185  var rng = doc.selection.createRange();
186  while ((rng.text.charAt(0) != ' ') && (rng.htmlText.charAt(0) != '>') && (rng.text.charCodeAt(0) != 13)) {
187  if (rng.moveStart('character', -1) == 0) break;
188  }
189  while ((rng.text.charAt(0) == ' ') || (rng.htmlText.charAt(0) == '>')) {
190  rng.moveStart('character', 1);
191  }
192  return rng.text.replace(new RegExp(/^\s+/), '');
193  }
194  }
195 
196 
197  </script>
198  <?php
199 
200  }//end paint_generic()
201 
202 
203 }//end class
204 
205 ?>