htmlarea-functions.js

来自「Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是」· JavaScript 代码 · 共 362 行

JS
362
字号
/*
* Copyright 2001-2007 Hippo (www.hippo.nl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

myMap=function(){
  var val=[];
  var keys = [];

  this.count=val.length;
  
  this.clear=function(){
	val=[];
	keys = [];
	this.count=val.length;
  };
  
  this.contains=function(strId){
	for(var i=0; i<val.length; i++){
	  if (keys[i]==strId){
		return true;
	  }
	}
	return false;
  };
  
  this.add=function(key,o) {
    if(!this.contains(key)) {
      this.count 		= val.length;
      val[this.count] 	= o;
      keys[this.count]	= key;
    }  
  };
  
  this.get=function(key){
  	for(var i=0; i<val.length; i++){
	  if (keys[i]==key){
        return val[i];
      }  
    }
    return null;
  };
}

var myGeneratableAreas = {
  queueClear: function(){
    this.queue = new Array();
  },
  queueFull: function(){
    return (this.queueSize == this.queue.length);
  },
  initHtmlareasOnFullQueue: function(size){
    this.queueClear();
    this.queueSize=size;
    
    var thisObj = this;

    var _myAreas = this;
    polledConditionalAction(
      function(){ 
        return _myAreas.queueFull();
      },
      function(){
        _myAreas.initializeQueuedHtmlareas();
      },
      50
    );
  },
  loadTextareaValues: function(){
    if (typeof __htmlareas != 'undefined'){
      
      for (var i=0; i<__htmlareas.length; i++){
        _editor=__htmlareas[i];
        _editor._textArea.value = _editor.outwardHtml(_editor.getHTML());
      }
    }
    return true;
  },
  queuePut: function(id,func){
    if (typeof this.queue == 'undefined') this.queue = new Array();
    
    var qEntry = new Object();
    qEntry.f = func;
    qEntry.id = id;
    this.queue[this.queue.length]= qEntry;  
  },
  queueGet: function(){
    if (this.queue.length > 0)  
      return this.queue.shift();
    else
      return null;  
  },
  registered: function(id){
    return (typeof this.registeredAreas != 'undefined' && typeof this.registeredAreas[id] != 'undefined');  
  },
  getHtmlArea: function(id){
    
    if (typeof __htmlareas == 'undefined') return -1;
    
    var i;     
    for (i=0; i<__htmlareas.length; i++)
      if (__htmlareas[i]._textArea.name == id) return __htmlareas[i];
    return null;
  },
  getHtmlAreaIndex: function(id){
    var i;
    for (i=0; i<__htmlareas.length; i++)
      if (__htmlareas[i]._textArea.name == id) return i;
    return -1;
  },
  initializeFirstHtmlAreaAndWait: function(){
    var oldHtmlArea;
    var newHtmlArea;
    var _myAreas = this;
    
    // pop htmlarea from FIFO queue
    var qEntry = this.queueGet();

    // clean up exisiting HTMLArea with same id
    if (this.registered(qEntry.id)){
      var index = this.getHtmlAreaIndex(qEntry.id);
      __htmlareas.splice(index,1);
    }

    // configure htmlarea
    newHtmlArea = qEntry.f(qEntry.id);

    // check whether more htmlareas need to be initialized
    
    if (this.queue.length > 0){
      newHtmlArea._onGenerate = 
        function(){
          _myAreas.initializeFirstHtmlAreaAndWait();
        }; 
    } else
      jumptoBookmark();
    
    // initialize htmlarea
    newHtmlArea.generate();
    
    this.registeredAreas[qEntry.id] = qEntry;
  },
  initializeQueuedHtmlareas:function(){
    if (typeof this.registeredAreas == 'undefined') this.registeredAreas = new Object();
    
    if (typeof this.queue != 'undefined' && this.queue.length > 0)
      this.initializeFirstHtmlAreaAndWait();
    else
      jumptoBookmark();    
  }  
};
 
var PREVIEW_CONTAINER = '-preview-container';
var PREVIEW_BUTTON = '-expand-preview';
var EDIT_BUTTON = '-edit-action';

var myAreas = new Array();
var myAreaInitFuncs = new Array();
var myGeneratedAreas = new myMap();

var currentBookmark ="";
var currentHtmlArea = null;
var maxPreviewAreaHeight = null;

function registerArea(id,func)
{
  myAreas.push(id);
  myAreaInitFuncs.push(func);
}

/*
  Function that handles the generating/wakeup/unhide functionality of htmlareas that are in
  preview mode. It first tries to hide the currently active htmlArea (only if it's of type preview-htmlarea)
  HtmlArea's which have been generated wil be saved inside a hashMap for fast re-editing.  
*/

function startEditing(strId) {
  stopEditing();
  
  var button = document.getElementById(strId + EDIT_BUTTON);
  if(button != null)
    button.style.display = 'none';
  
  if(!myGeneratedAreas.contains(strId)) {
    var myEditor = initializeEditor(strId,myAreaInitFuncs[indexOfArea(strId)]);
    myEditor.generate();
    myGeneratedAreas.add(strId, myEditor);
  }else{
    var myEditor = myGeneratedAreas.get(strId);
    myEditor.wakeup();
  }  
  //hide preview div
  var previewContainer = document.getElementById(strId + PREVIEW_CONTAINER);
  if(previewContainer != null)
    previewContainer.style.display = 'none';
  
  currentHtmlArea = strId;
}

/*
  Function that handles the hiding/sleeping of the HtmlArea
  that's was previously started
*/

function stopEditing() {
  
  if(currentHtmlArea != null) {
    var button = document.getElementById(currentHtmlArea + EDIT_BUTTON);
    if(button != null)
      button.style.display = 'block';
  
    var myEditor = myGeneratedAreas.get(currentHtmlArea);
    var content = myEditor.outwardHtml(myEditor.getHTML());
    
    var hiddenTextArea = document.getElementById(currentHtmlArea);
    if(hiddenTextArea != null) 
      hiddenTextArea.value = content;
      
    var previewContainer = document.getElementById(currentHtmlArea + PREVIEW_CONTAINER);
    if(previewContainer != null)
      previewContainer.innerHTML = content;	
    
    myEditor.sleep();
    
    //show preview div
    var previewContainer = document.getElementById(currentHtmlArea + PREVIEW_CONTAINER);
    if(previewContainer != null)
      previewContainer.style.display = 'block';

    var previewButton = document.getElementById(currentHtmlArea + PREVIEW_BUTTON);
    if(previewButton != null && previewButton.className == 'hidden')
      previewButton.className = '';
    
  }
}

/* 
  Helper function that returns the index (zero-based) of htmlArea with param id 
  in the local myAreas array
*/
function indexOfArea(id){
  var result = -1;
  var i = 0;
  while (result == -1 && i < myAreas.length){
    if (myAreas[i] == id){
      result = i;
      
    }
    else{
      i += 1;
    }
  }
  return result;
}

function togglePreviewExpansion(strId) {
  var el = document.getElementById(strId + PREVIEW_CONTAINER);
  if(el.className.indexOf(' no-overflow')>0) {
    el.className = el.className.substring(0, el.className.indexOf(' no-overflow'));
    el.style.height = maxPreviewAreaHeight;  
  }else{
    el.className = el.className + ' no-overflow';
    maxPreviewAreaHeight = el.style.height;
    el.style.height = 'auto';
  }
}

function initializeHTMLAreas()
{
  if (typeof myGeneratableAreas != 'undefined'){
    myGeneratableAreas.initializeQueuedHtmlareas();
    // set_forms_onsubmitHandlers();
  }
}

/*
  Helper function: perform 'action' when 'condition' is satisfied. If condition is not satisfied,
  try again in 'pollPeriod' ms.
*/
function polledConditionalAction(condition,action,pollPeriod){
  var _cond = false;
  var _abort = false;

  try{
    _cond = condition();
  } catch(e){
    _abort = true;
  }
  if (_cond){
    try{
      action();
    } catch(e){}
  } else if (!_abort){  
     setTimeout(
       function(){
         polledConditionalAction(condition,action,pollPeriod);
       },
       pollPeriod
     );
  } else {
    alert('polledConditionalAction(): An error occurred');
  }
}


function jumptoBookmark2()
{
  if (bookmarks!=null){
    var i;
    var el;
    var currentBookmark='';
    for (i=0; i<bookmarks.length;i++){
      el=document.getElementById(bookmarks[i]);
      if (el != null && el.type!='hidden'){
        currentBookmark="#"+bookmarks[i];
        break;
      }
    }
    if(currentBookmark!="")
    {
      location.href=currentBookmark;
    }
  }
}

function jumptoBookmark(){
  // jump to active element
  // need delay to make sure html area generation is finished 
  setTimeout(jumptoBookmark2,500);
}


function setCurrentBookmark(widgetid) {
  currentBookmark = widgetid;
}

function initializeEditor(id,func) {
  return func(id);
}

function getBrowser() {
  var agt = navigator.userAgent.toLowerCase();
  if ((agt.indexOf("msie") != -1)) {
    browser ='ie';
  }
  else if(navigator.product == "Gecko") {
    browser = 'moz';
  }
  return browser;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?