[ Index ]

PHP Cross Reference of Nucleus CMS 3.32

title

Body

[close]

/nucleus/javascript/ -> edit.js (source)

   1  /**
   2    * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
   3    * Copyright (C) 2002-2007 The Nucleus Group
   4    *
   5    * This program is free software; you can redistribute it and/or
   6    * modify it under the terms of the GNU General Public License
   7    * as published by the Free Software Foundation; either version 2
   8    * of the License, or (at your option) any later version.
   9    * (see nucleus/documentation/index.html#license for more info)
  10    *
  11    * This file contains functions to allow adding items from inside the weblog.
  12    * Also contains code to avoid submitting form data twice.
  13    *
  14    * $Id: edit.js 1116 2007-02-03 08:24:29Z kimitake $
  15    */
  16  
  17  var nucleusConvertBreaks = true;
  18  var nucleusMediaPopupURL = '';
  19  var nucleusMediaURL = 'media/';
  20  var nucleusAuthorId = 0;
  21  var scrollTop = -1;
  22  
  23  function setConvertBreaks(newval) {    nucleusConvertBreaks = newval; }
  24  function setMediaUrl(url) { nucleusMediaURL = url; }
  25  function setAuthorId(id) { nucleusAuthorId = id; }
  26  
  27  function preview(id, value) {
  28      elem = document.getElementById(id);
  29      if (!elem) return;
  30  
  31      var preview = nucleusConvertBreaks ? str_replace("\n","<br />",value)+"&#160;" : value+"&#160;";
  32  
  33      // expand the media commands (without explicit collection)
  34      preview = preview.replace(/\<\%image\(([^\/\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<img src='"+nucleusMediaURL+nucleusAuthorId+"/$1' width='$2' height='$3' alt=\"$4\" />");
  35  
  36      // expand the media commands (with collection)
  37      preview = preview.replace(/\<\%image\(([^\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<img src='"+nucleusMediaURL+"$1' width='$2' height='$3' alt=\"$4\" />");
  38      preview = preview.replace(/\<\%popup\(([^\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<a href='' onclick='if (event &amp;&amp; event.preventDefault) event.preventDefault(); alert(\"popup image\"); return false;' title='popup'>$4</a>");
  39      preview = preview.replace(/\<\%media\(([^\|]*)\|([^)]*)\)\%\>/g,"<a href='' title='media link'>$2</a>");
  40  
  41      elem.innerHTML = preview;
  42  }
  43  
  44  function showedit() {
  45      prevval = document.getElementById('edit').style.display;
  46      if (prevval == "block")
  47          newval = "none";
  48      else
  49          newval = "block";
  50      document.getElementById('edit').style.display = newval;
  51  
  52      if (newval == "block")
  53          updAllPreviews();
  54  }
  55  
  56  function updAllPreviews() {
  57      updPreview('title');
  58      updPreview('body');
  59      updPreview('more');
  60  }
  61  
  62  function isEditVisible() {
  63      var editform = document.getElementById('edit');
  64      if (!editform) return true;
  65      var prevval = editform.style.display;
  66      return (prevval == "none") ? false : true;
  67  }
  68  
  69  function updPreview(id) {
  70      // don't update when preview is hidden
  71      if (!isEditVisible()) return;
  72  
  73      var inputField = document.getElementById('input' + id);
  74      if (!inputField) return;
  75      preview('prev' + id, inputField.value);
  76  }
  77  
  78  // replace a in s by b (taken from milov.nl)
  79  function str_replace(a, b, s)
  80  {
  81      if (a == b || !s.length || !a.length) return s;
  82      if ((p=s.indexOf(a)) == -1) { return s; }
  83      else { ns = s.substring(0,p) + b + s.substring(p+a.length,s.length); }
  84      return (s.indexOf(a) != -1) ? str_replace(a, b, ns) : ns;
  85  }
  86  
  87  function shortCuts() {
  88      if (!event || (event.ctrlKey != true)) return;
  89  
  90      switch (event.keyCode) {
  91          case 1:
  92              ahrefThis(); break; // ctrl-shift-a
  93          case 2:
  94              boldThis(); break; // ctrl-shift-b
  95          case 9:
  96              italicThis(); break; // ctrl-shift-i
  97          case 13:
  98              addMedia(); break; // ctrl-shift-m
  99          default:
 100              return;
 101      }
 102      return;
 103  }
 104  
 105  function cutThis() { execAndUpdate('cut'); }
 106  function copyThis() { execAndUpdate('copy'); }
 107  function pasteThis() { execAndUpdate('paste'); }
 108  function boldThis() { insertAroundCaret('<b>','</b>'); }
 109  function italicThis() { insertAroundCaret('<i>','</i>'); }
 110  function leftThis() { insertAroundCaret('<div class="leftbox">','</div>'); }
 111  function rightThis() { insertAroundCaret('<div class="rightbox">','</div>'); }
 112  function alignleftThis() { insertAroundCaret('<div style="text-align: left">','</div>'); }
 113  function alignrightThis() { insertAroundCaret('<div style="text-align: right">','</div>'); }
 114  function aligncenterThis() { insertAroundCaret('<div style="text-align: center">','</div>'); }
 115  
 116  
 117  function ahrefThis() {
 118      if (document.selection)
 119          strSelection = document.selection.createRange().text;
 120      else
 121          strSelection = '';
 122  
 123      strHref = prompt("Create a link to:","http://");
 124      if (strHref == null) return;
 125  
 126      var textpre = "<a href=\"" + strHref.replace(/&/g,'&amp;') + "\">";
 127      insertAroundCaret(textpre, "</a>");
 128  }
 129  
 130  function execAndUpdate(action) {
 131      lastSelected.caretPos.execCommand(action);
 132      updAllPreviews();
 133  }
 134  
 135  
 136  var nonie_FormType = 'body';
 137  
 138  // Add media to new item
 139  function addMedia() {
 140  
 141      var mediapopup = window.open(nucleusMediaPopupURL + 'media.php','name',
 142          'status=yes,toolbar=no,scrollbars=yes,resizable=yes,width=500,height=450,top=0,left=0');
 143  
 144      return;
 145  }
 146  
 147  
 148  function setMediaPopupURL(url) {
 149      nucleusMediaPopupURL = url;
 150  }
 151  
 152  function includeImage(collection, filename, type, width, height) {
 153      if (isCaretEmpty()) {
 154          text = prompt("Text to display ?","");
 155      } else {
 156          text = getCaretText();
 157      }
 158  
 159      // add collection name when not private collection (or editing a message that's not your)
 160      var fullName;
 161      if (isNaN(collection) || (nucleusAuthorId != collection)) {
 162          fullName = collection + '/' + filename;
 163      } else {
 164          fullName = filename;
 165      }
 166  
 167  
 168      var replaceBy;
 169      switch(type) {
 170          case 'popup':
 171              replaceBy = '<%popup(' +  fullName + '|'+width+'|'+height+'|' + text +')%>';
 172              break;
 173          case 'inline':
 174          default:
 175              replaceBy = '<%image(' +  fullName + '|'+width+'|'+height+'|' + text +')%>';
 176      }
 177  
 178      insertAtCaret(replaceBy);
 179      updAllPreviews();
 180  
 181  }
 182  
 183  
 184  function includeOtherMedia(collection, filename) {
 185      if (isCaretEmpty()) {
 186          text = prompt("Text to display ?","");
 187      } else {
 188          text = getCaretText();
 189      }
 190  
 191      // add collection name when not private collection (or editing a message that's not your)
 192      var fullName;
 193      if (isNaN(collection) || (nucleusAuthorId != collection)) {
 194          fullName = collection + '/' + filename;
 195      } else {
 196          fullName = filename;
 197      }
 198  
 199      var replaceBy = '<%media(' +  fullName + '|' + text +')%>';
 200  
 201      insertAtCaret(replaceBy);
 202      updAllPreviews();
 203  }
 204  
 205  
 206  
 207  // function to prevent submitting form data twice
 208  var submitcount=0;
 209  function checkSubmit() {
 210      if (submitcount == 0) {
 211          submitcount++;
 212          return true;
 213      } else {
 214          return false;
 215      }
 216  }
 217  
 218  
 219  // code to store the caret (cursor) position of a text field/text area
 220  // taken from javascript.faqts and modified
 221  // http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
 222  
 223  // stores the caret
 224  function storeCaret (textEl) {
 225  
 226      // store caret
 227      if (textEl.createTextRange)
 228          textEl.caretPos = document.selection.createRange().duplicate();
 229  
 230      // also store lastselectedelement
 231      lastSelected = textEl;
 232  
 233      nonie_FormType = textEl.name;
 234  
 235      scrollTop = textEl.scrollTop;
 236  }
 237  
 238  var lastSelected;
 239  
 240   // inserts text at caret (overwriting selection)
 241  function insertAtCaret (text) {
 242      var textEl = lastSelected;
 243      if (textEl && textEl.createTextRange && textEl.caretPos) {
 244          var caretPos = textEl.caretPos;
 245          caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
 246      } else if (!document.all && document.getElementById) {
 247          mozReplace(document.getElementById('input' + nonie_FormType), text);
 248          if(scrollTop>-1) {
 249              document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
 250          }
 251      } else if (textEl) {
 252          textEl.value  += text;
 253      } else {
 254          document.getElementById('input' + nonie_FormType).value += text;
 255          if(scrollTop>-1) {
 256              document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
 257          }
 258      }
 259      updAllPreviews();
 260  }
 261  
 262   // inserts a tag around the selected text
 263  function insertAroundCaret (textpre, textpost) {
 264      var textEl = lastSelected;
 265  
 266      if (textEl && textEl.createTextRange && textEl.caretPos) {
 267          var caretPos = textEl.caretPos;
 268          caretPos.text = textpre + caretPos.text + textpost;
 269      } else if (!document.all && document.getElementById) {
 270          mozWrap(document.getElementById('input' + nonie_FormType), textpre, textpost);
 271          if(scrollTop>-1) {
 272              document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
 273          }
 274      } else {
 275          document.getElementById('input' + nonie_FormType).value += textpre + textpost;
 276          if(scrollTop>-1) {
 277              document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
 278          }
 279      }
 280  
 281      updAllPreviews();
 282  }
 283  
 284  /* some methods to get things working in Mozilla as well */
 285  function mozWrap(txtarea, lft, rgt) {
 286      var selLength = txtarea.textLength;
 287      var selStart = txtarea.selectionStart;
 288      var selEnd = txtarea.selectionEnd;
 289      if (selEnd==1 || selEnd==2) selEnd=selLength;
 290      var s1 = (txtarea.value).substring(0,selStart);
 291      var s2 = (txtarea.value).substring(selStart, selEnd)
 292      var s3 = (txtarea.value).substring(selEnd, selLength);
 293      txtarea.value = s1 + lft + s2 + rgt + s3;
 294  }
 295  function mozReplace(txtarea, newText) {
 296      var selLength = txtarea.textLength;
 297      var selStart = txtarea.selectionStart;
 298      var selEnd = txtarea.selectionEnd;
 299      if (selEnd==1 || selEnd==2) selEnd=selLength;
 300      var s1 = (txtarea.value).substring(0,selStart);
 301      var s2 = (txtarea.value).substring(selStart, selEnd)
 302      var s3 = (txtarea.value).substring(selEnd, selLength);
 303      txtarea.value = s1 + newText + s3;
 304  }
 305  function mozSelectedText() {
 306      var txtarea = document.getElementById('input' + nonie_FormType);
 307      var selLength = txtarea.textLength;
 308      var selStart = txtarea.selectionStart;
 309      var selEnd = txtarea.selectionEnd;
 310      if (selEnd==1 || selEnd==2) selEnd=selLength;
 311      return (txtarea.value).substring(selStart, selEnd);
 312  }
 313  
 314  function getCaretText() {
 315      if (!document.all && document.getElementById)
 316          return mozSelectedText();
 317      else
 318          return lastSelected.caretPos.text;
 319  }
 320  
 321  function isCaretEmpty() {
 322      if (lastSelected && lastSelected.createTextRange && lastSelected.caretPos)
 323          return (lastSelected.caretPos.text == '');
 324      else if (!document.all && document.getElementById)
 325          return (mozSelectedText() == '');
 326      else
 327          return true;
 328  }
 329  
 330  function BtnHighlight(el) {
 331      with(el.style){
 332          borderLeft="1px solid gray";
 333          borderRight="1px solid #e9e9e9";
 334          borderTop="1px solid gray";
 335          borderBottom="1px solid #e9e9e9";
 336      }
 337  }
 338  
 339  function BtnNormal(el) {
 340      with(el.style){
 341          padding="3px";
 342          border="1px solid #dddddd";
 343      }
 344  }
 345  


Generated: Tue Feb 12 15:34:36 2008 Cross-referenced by PHPXref 0.7