[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/admin/jscripts/codepress/ -> codepress.js (source)

   1  /*
   2   * CodePress - Real Time Syntax Highlighting Editor written in JavaScript - http://codepress.org/
   3   * 
   4   * Copyright (C) 2006 Fernando M.A.d.S. <fermads@gmail.com>
   5   *
   6   * This program is free software; you can redistribute it and/or modify it under the terms of the 
   7   * GNU Lesser General Public License as published by the Free Software Foundation.
   8   * 
   9   * Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
  10   */
  11  
  12  CodePress = function(obj) {
  13      if(MyBB.browser == "opera" || MyBB.browser == "safari" || MyBB.browser == "chrome")
  14      {
  15          return;
  16      }
  17      
  18      var self = document.createElement('iframe');
  19      self.textarea = obj;
  20      self.textarea.disabled = true;
  21      self.textarea.style.overflow = 'hidden';
  22      self.style.height = self.textarea.clientHeight +'px';
  23      self.style.width = self.textarea.clientWidth +'px';
  24      if(self.textarea.style.width && self.textarea.style.width.indexOf('%') != -1) {
  25          self.style.width = self.textarea.style.width;    
  26      }
  27      self.textarea.style.overflow = 'auto';
  28      self.style.border = '1px solid gray';
  29      self.frameBorder = 0; // remove IE internal iframe border
  30      self.style.visibility = 'hidden';
  31      self.style.position = 'absolute';
  32      self.options = self.textarea.className;
  33      
  34      self.initialize = function() {
  35          self.editor = self.contentWindow.CodePress;
  36          self.editor.body = self.contentWindow.document.getElementsByTagName('body')[0];
  37          self.editor.setCode(self.textarea.value);
  38          self.setOptions();
  39          self.editor.syntaxHighlight('init');
  40          self.textarea.style.display = 'none';
  41          self.style.position = 'static';
  42          self.style.visibility = 'visible';
  43          self.style.display = 'inline';
  44      }
  45      
  46      // obj can by a textarea id or a string (code)
  47      self.edit = function(obj,language) {
  48          if(obj) self.textarea.value = document.getElementById(obj) ? document.getElementById(obj).value : obj;
  49          if(!self.textarea.disabled) return;
  50          self.language = language ? language : self.getLanguage();
  51          self.src = CodePress.path+'codepress.html?language='+self.language+'&ts='+(new Date).getTime();
  52          if(self.attachEvent) self.attachEvent('onload',self.initialize);
  53          else self.addEventListener('load',self.initialize,false);
  54      }
  55  
  56      self.getLanguage = function() {
  57          for (language in CodePress.languages) 
  58              if(self.options.match('\\b'+language+'\\b')) 
  59                  return CodePress.languages[language] ? language : 'generic';
  60      }
  61      
  62      self.setOptions = function() {
  63          if(self.options.match('autocomplete-off')) self.toggleAutoComplete();
  64          if(self.options.match('readonly-on')) self.toggleReadOnly();
  65          if(self.options.match('linenumbers-off')) self.toggleLineNumbers();
  66      }
  67      
  68      self.getCode = function() {
  69          return self.textarea.disabled ? self.editor.getCode() : self.textarea.value;
  70      }
  71  
  72      self.setCode = function(code) {
  73          self.textarea.disabled ? self.editor.setCode(code) : self.textarea.value = code;
  74      }
  75  
  76      self.toggleAutoComplete = function() {
  77          self.editor.autocomplete = (self.editor.autocomplete) ? false : true;
  78      }
  79      
  80      self.toggleReadOnly = function() {
  81          self.textarea.readOnly = (self.textarea.readOnly) ? false : true;
  82          if(self.style.display != 'none') // prevent exception on FF + iframe with display:none
  83              self.editor.readOnly(self.textarea.readOnly ? true : false);
  84      }
  85      
  86      self.toggleLineNumbers = function() {
  87          var cn = self.editor.body.className;
  88          self.editor.body.className = (cn==''||cn=='show-line-numbers') ? 'hide-line-numbers' : 'show-line-numbers';
  89      }
  90      
  91      self.toggleEditor = function() {
  92          if(self.textarea.disabled) {
  93              self.textarea.value = self.getCode();
  94              self.textarea.disabled = false;
  95              self.style.display = 'none';
  96              self.textarea.style.display = 'inline';
  97          }
  98          else {
  99              self.textarea.disabled = true;
 100              self.setCode(self.textarea.value);
 101              self.editor.syntaxHighlight('init');
 102              self.style.display = 'inline';
 103              self.textarea.style.display = 'none';
 104          }
 105      }
 106  
 107      self.edit();
 108      return self;
 109  }
 110  
 111  CodePress.languages = {    
 112      css : 'CSS', 
 113      generic : 'Generic',
 114      html : 'HTML',
 115      javascript : 'JavaScript', 
 116      php : 'PHP', 
 117      text : 'Text', 
 118      sql : 'SQL',
 119      mybb : 'MyBB Templates'
 120  }
 121  
 122  
 123  CodePress.run = function() {
 124      s = document.getElementsByTagName('script');
 125      for(var i=0,n=s.length;i<n;i++) {
 126          if(s[i].src.match('codepress.js')) {
 127              CodePress.path = s[i].src.replace('codepress.js','');
 128          }
 129      }
 130      t = document.getElementsByTagName('textarea');
 131      for(var i=0,n=t.length;i<n;i++) {
 132          if(t[i].className.match('codepress')) {
 133              id = t[i].id;
 134              t[i].id = id+'_cp';
 135              eval(id+' = new CodePress(t[i])');
 136              t[i].parentNode.insertBefore(eval(id), t[i]);
 137          } 
 138      }
 139  }
 140  
 141  if(window.attachEvent) window.attachEvent('onload',CodePress.run);
 142  else window.addEventListener('DOMContentLoaded',CodePress.run,false);


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1