Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
matrix_insert_anchor.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 
36 {
37 
45  function matrix_insert_anchor(&$wysiwyg)
46  {
47  wysiwyg_plugin::wysiwyg_plugin($wysiwyg);
48  $this->_add_button('matrixcreateanchor','MatrixCreateAnchor',translate('insert_anchor'),'false','30');
49 
50  }//end constructor
51 
52 
61  function paint_generic()
62  {
63  ?>
64  <script type="text/javascript" src="<?php echo sq_web_path('lib').'/js/general.js' ?>"></script>
65  <script type="text/javascript" src="<?php echo sq_web_path('fudge').'/var_serialise/var_serialise.js' ?>"></script>
66 
67  <script type="text/javascript" language="Javascript">
68  var wysiwyg_editing = true; // just so that onclick links don't execute in wysiwyg
69 
70  // Called when the user clicks on "Insert Anchor" button
71  HTMLArea.prototype._matrixInsertAnchor = function() {
72  var editor = this; // for nested functions
73 
74  // OK, let's create the current args array that we'll pass to the dialog
75  var currArgs = new Object();
76  currArgs["name"] = "";
77 
78  var sel = this._getSelection();
79  var range = this._createRange(sel);
80  var text_range = false;
81  // OK, so they have a control object selected, let's try and make it a text selection
82  if (sel.type && sel.type.toLowerCase() == "control") {
83  var control_range = this._createRange(sel);
84  var text_range = this._createTextRange(sel);
85  if (text_range != null) {
86  text_range = true;
87  editor.selectNodeContents(control_range(0));
88  } else {
89  alert(js_translate('object_requires_selection'));
90  sel.empty();
91  return;
92  }
93  sel = this._getSelection();
94  range = this._createRange(sel);
95  }//end if a control
96 
97  var inside_html = '';
98  var current_a = this.getClosest("a");
99  if (current_a) {
100  if (HTMLArea.is_ie) {
101  inside_html = current_a.innerHTML;
102  } else {
103  this.selectNodeContents(current_a);
104  inside_html = editor.getSelectedHTML();
105  }
106  } else {
107  inside_html = editor.getSelectedHTML();
108  }
109  sel = this._getSelection();
110  range = this._createRange(sel);
111  // Is this aready a link?
112  if (current_a) {
113 
114  if (typeof(visual_aid_mode) != 'undefined' && visual_aid_mode) {
115  // If visual aid is being used now, remove it before processing
116  // we keep the copy here in the case of canceling
117  var copied_html = current_a.innerHTML;
118  if (HTMLArea.is_gecko) {
119  current_a.innerHTML = current_a.innerHTML.replace(/<span id="wysiwyg-visual-aid-plugin-anchor"[^>]*>A[^>]*<\/span>/, "");
120  } else if (HTMLArea.is_ie) {
121  var e = '<span id=wysiwyg-visual-aid-plugin-anchor[^>]*>A[^>]*<\/span>';
122  var re = new RegExp(e, "ig");
123  current_a.innerHTML = current_a.innerHTML.replace(re, '');
124  }
125  }
126 
127  var e = '^(<a [^>]+>)(.*)</a>$';
128  var re = new RegExp(e, "i");
129  var tag_html = editor.getSelectedHTML().replace(re, "$1");
130 
131  var re = new RegExp("\n", "gi");
132  var tag_html = tag_html.replace(re, "");
133 
134  currArgs["name"] = current_a.name;
135 
136  re = new RegExp('if \\(!window\\.wysiwyg_editing\\)[\\s]+\\{[\\s]+window\\.open\\([\'"]([^\'"]+)[\'"], [\'"][0-9]+[\'"], [\'"]([^\'"]+)[\'"]\\);', "gi");
137  results = re.exec(tag_html);
138 
139  if (results != null) {
140  currArgs["name"] = results[1];
141  }// end if
142 
143  // else not a link so check for paragraphs
144  } else {
145 
146  // convert all para tags to lowercase
147  var para_html = inside_html.replace(/(<\/?)P/gi, "$1p");
148 
149  // OK so we have some paragraph tags caught up in the mix, let's see what we can do with them
150  // because we don't want our href's going around paragraph tags
151  if (para_html.indexOf("<p") > -1) {
152 
153  // if the position of the first tag is the same as the last then we have only one paragraph
154  // we don't want more than one paragraph because these can start or end from half-way through
155  // a para, which would break the formatting when we go to re-insert the text with the href
156  if (para_html.indexOf("<p") != para_html.lastIndexOf("<p")) {
157  alert("Please select only one paragraph to link at a time.");
158  return;
159  }
160 
161  }// end if paragraphs exist
162 
163  }// end if existing link
164 
165  var in_popup = '0';
166  if (this._inPopup == true) { in_popup = '1'; }
167 
168  editor_<?php echo $this->wysiwyg->name?>._object = this;
169  var strPage = "<?php echo $this->get_popup_href('insert_anchor.php')?>?&name=" + escape(currArgs["name"]) + "&editor_name=" + escape("<?php echo $this->wysiwyg->name; ?>") + "&in_popup=" + escape(in_popup);
170 
171  this._popupDialog("matrixInsertAnchor", strPage, 480, 300, true, function(param) {
172 
173  if (!param) {
174  // user must have pressed Cancel
175  if (typeof(visual_aid_mode) != 'undefined' && visual_aid_mode) {
176  if (current_a != null) {
177  current_a.innerHTML = copied_html;
178  }
179  }
180  return false;
181  }
182 
183  if (param["remove"] == "1") {
184  if (current_a != null) {
185  // remove this anchor tag
186  var current_html = current_a.innerHTML;
187  var anchor_inner_html = current_html;
188 
189  // Brackets within the HTML must be quoted when in Visual Aid mode, so the anchor can be located
190  if (typeof(visual_aid_mode) != 'undefined' && visual_aid_mode) {
191  if (HTMLArea.is_gecko) {
192  current_html = current_html.replace(/\(/, "\\\(");
193  current_html = current_html.replace(/\)/, "\\\)");
194 
195  // Remove the Visual Aid code including the anchor
196  anchor_inner_html = anchor_inner_html.replace(/<span id="wysiwyg-visual-aid-plugin-anchor".*<\/span>/, '');
197  } else if (HTMLArea.is_ie) {
198  var re = new RegExp("\\(", "ig");
199  current_html = current_html.replace(re, "\\\(");
200 
201  re = new RegExp("\\)", "ig");
202  current_html = current_html.replace(re, "\\\)");
203 
204  re = new RegExp("<span id=wysiwyg-visual-aid-plugin-anchor.*<\/span>", "ig");
205  anchor_inner_html = anchor_inner_html.replace(re, '');
206  }
207  }
208 
209  var parent = current_a.parentNode;
210  if (HTMLArea.is_gecko) {
211  var e = '(<a.*name="' + param["name"] + '"[^>]*>)' + current_html + '</a>';
212  } else if (HTMLArea.is_ie) {
213  var e = '(<a.*name="?' + param["name"] + '"?[^>]*>)' + current_html + '</a>';
214  }
215  var re = new RegExp(e, "ig");
216  var new_html = parent.innerHTML.replace(re, anchor_inner_html);
217  parent.innerHTML = new_html;
218  }
219  } else {
220  var visual_aid_tag = "";
221  var visual_aid_style = 'padding-left: 1px; padding-right: 1px; padding-top: 1px; padding-bottom: 1px; background-color : #37DF5E; font: 10px Verdana,Tahoma,sans-serif; font-weight: bold; border-left: 1px solid; border-right: 1px solid; border-top: 1px solid; border-bottom: 1px solid;';
222 
223  if (param["name"] != "") {
224  if (typeof(visual_aid_mode) != 'undefined' && visual_aid_mode) {
225  visual_aid_tag = '<span id="wysiwyg-visual-aid-plugin-anchor" style="' + visual_aid_style + '">A "' + param["name"] + '"</span>';
226  }
227  if (current_a) {
228  // editing
229  if (param["name"] != current_a.getAttribute("name")) {
230  if (HTMLArea.is_gecko) {
231  current_a.setAttribute("name", param["name"]);
232  current_a.setAttribute("id", param["name"]);
233  current_a.innerHTML = visual_aid_tag + current_a.innerHTML;
234  } else if (HTMLArea.is_ie) {
235  // somehow IE can not update the name attribute value in the wysiwyg
236  // this is just mysterious...
237 
238  // Also some trickery to spot lack of quotes in IE for one-word attribute values
239  var e = '<A( id="?' + current_a.getAttribute("name") + '"?){0,1} name="?' + current_a.getAttribute("name") + '"?>';
240  var re = new RegExp(e, "gi");
241  var replacement = '<A id="' + param["name"] + '" name="' + param["name"] + '">' + visual_aid_tag;
242  current_a.parentNode.innerHTML = current_a.parentNode.innerHTML.replace(re, replacement);
243  }
244  }
245  } else {
246  // adding
247  var para = new RegExp("^[\n\t\s]*<P[^>]*>(.*)<\/P>", "gim")
248  var insideP = inside_html.replace(para, "$1");
249  if (insideP != inside_html) {
250  var html = '<p><a name="' + param["name"] + '" id="' + param["name"];
251  html += '">' + visual_aid_tag + insideP + '</a></p>';
252  } else {
253  var html = '<a name="' + param["name"] + '" id="' + param["name"];
254  html += '">' + visual_aid_tag + inside_html + '</a>';
255  }
256 
257  // replace with new URL
258  html = editor.make_absolute_urls(html);
259  editor.insertHTML(html, range);
260  }
261 
262  }// end if param exists
263  }
264  // unselect the selection
265  if (HTMLArea.is_ie) {
266  range.execCommand("Unselect");
267  } else {
268  sel.removeAllRanges();
269  }
270 
271  }, null);
272 
273  };
274  </script>
275  <?php
276 
277  }//end paint_generic()
278 
279 
286  function print_plugin_button_click()
287  {
288  ?>
289  case "matrixcreateanchor":
290  this._matrixInsertAnchor();
291  break;
292  <?php
293 
294  }//end print_plugin_button_click()
295 
296 
303  function print_plugin_update_toolbar()
304  {
305  ?>
306  case "matrixcreateanchor":
307  if (allEnabled == null && this._tmp['disable_toolbar'] == true) {
308  this.updateToolbar(false);
309  return;
310  }
311  break;
312  <?php
313 
314  }//end print_plugin_update_toolbar()
315 
316 }//end class
317 
318 ?>