⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kupueditor.js

📁 一个使用struts+hibernate+spring开发的完的网站源代码。
💻 JS
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************** * * Copyright (c) 2003-2004 Kupu Contributors. All rights reserved. * * This software is distributed under the terms of the Kupu * License. See LICENSE.txt for license text. For a list of Kupu * Contributors see CREDITS.txt. * *****************************************************************************/// $Id: kupueditor.js,v 1.1 2005/03/26 20:31:36 svieujot Exp $//----------------------------------------------------------------------------// Main classes//----------------------------------------------------------------------------/* KupuDocument        This essentially wraps the iframe.    XXX Is this overkill?    */function KupuDocument(iframe) {    /* Model */        // attrs    this.editable = iframe; // the iframe    this.window = this.editable.contentWindow;    this.document = this.window.document;    this._browser = _SARISSA_IS_IE ? 'IE' : 'Mozilla';        // methods    this.execCommand = function(command, arg) {        /* delegate execCommand */        // XXX Is the command always a string? Can't it be '' or 0 or so?        if (!arg) arg = null;        this.document.execCommand(command, false, arg);    };        this.reloadSource = function() {        /* reload the source */                // XXX To temporarily work around problems with resetting the        // state after a reload, currently the whole page is reloaded.        // XXX Nasty workaround!! to solve refresh problems...        document.location = document.location;    };    this.getDocument = function() {        /* returns a reference to the window.document object of the iframe */        return this.document;    };    this.getWindow = function() {        /* returns a reference to the window object of the iframe */        return this.window;    };    this.getSelection = function() {        if (this._browser == 'Mozilla') {            return new MozillaSelection(this);        } else {            return new IESelection(this);        };    };    this.getEditable = function() {        return this.editable;    };};/* KupuEditor    This controls the document, should be used from the UI.    */function KupuEditor(document, config, logger) {    /* Controller */        // attrs    this.document = document; // the model    this.config = config; // an object that holds the config values    this.log = logger; // simple logger object    this.tools = {}; // mapping id->tool    this.filters = new Array(); // contentfilters        this._designModeSetAttempts = 0;    this._initialized = false;    // some properties to save the selection, required for IE to remember where     // in the iframe the selection was    this._previous_range = null;    // this property is true if the content is changed, false if no changes are made yet    this.content_changed = false;    // methods    this.initialize = function() {        /* Should be called on iframe.onload, will initialize the editor */        //DOM2Event.initRegistration();        this._initializeEventHandlers();        this.focusDocument();        if (this.getBrowserName() == "IE") {            var body = this.getInnerDocument().getElementsByTagName('body')[0];            body.setAttribute('contentEditable', 'true');            // provide an 'afterInit' method on KupuEditor.prototype            // for additional bootstrapping (after editor init)            this._initialized = true;            if (this.afterInit) {                this.afterInit();            };            this._saveSelection();        } else {            this._setDesignModeWhenReady();        };        this.logMessage('Editor initialized');    };    this.setContextMenu = function(menu) {        /* initialize the contextmenu */        menu.initialize(this);    };    this.registerTool = function(id, tool) {        /* register a tool */        this.tools[id] = tool;        tool.initialize(this);    };    this.getTool = function(id) {        /* get a tool by id */        return this.tools[id];    };    this.registerFilter = function(filter) {        /* register a content filter method            the method will be called together with any other registered            filters before the content is saved to the server, the methods            can be used to filter any trash out of the content. they are            called with 1 argument, which is a reference to the rootnode            of the content tree (the html node)        */        this.filters.push(filter);        filter.initialize(this);    };    this.updateStateHandler = function(event) {        /* check whether the event is interesting enough to trigger the         updateState machinery and act accordingly */        var interesting_codes = new Array(8, 13, 37, 38, 39, 40, 46);        // unfortunately it's not possible to do this on blur, since that's        // too late. also (some versions of?) IE 5.5 doesn't support the        // onbeforedeactivate event, which would be ideal here...        if (this.getBrowserName() == 'IE') {            this._saveSelection();        };        if (event.type == 'click' || event.type=='mouseup' ||                (event.type == 'keyup' &&                     interesting_codes.contains(event.keyCode))) {            // Filthy trick to make the updateState method get called *after*            // the event has been resolved. This way the updateState methods can            // react to the situation *after* any actions have been performed (so            // can actually stay up to date).            this.updateState(event);        }    };        this.updateState = function(event) {        /* let each tool change state if required */        // first see if the event is interesting enough to trigger        // the whole updateState machinery        var selNode = this.getSelectedNode();        for (var id in this.tools) {            try {                this.tools[id].updateState(selNode, event);            } catch (e) {                if (e == UpdateStateCancelBubble) {                    this.updateState(event);                    break;                } else {                    this.logMessage('Exception while processing updateState on ' + id + ': ' + e, 2);                };            };        };    };        this.saveDocument = function(redirect, synchronous) {        /* save the document, redirect if te arg is provided and the save is successful                     the (optional) redirect argument can be used to make the client jump to            another URL when the save action was successful.        */                // if no dst is available, bail out        if (!this.config.dst) {            this.logMessage('No destination URL available!', 2);            return;        }        var sourcetool = this.getTool('sourceedittool');        if (sourcetool) {sourcetool.cancelSourceMode();};        // make sure people can't edit or save during saving        if (!this._initialized) {            return;        }        this._initialized = false;                // set the window status so people can see we're actually saving        window.status= "Please wait while saving document...";        // call (optional) beforeSave() method on all tools        for (var id in this.tools) {            var tool = this.tools[id];            if (tool.beforeSave) {                try {                    tool.beforeSave();                } catch(e) {                    alert(e);                    this._initialized = true;                    return;                };            };        };                // pass the content through the filters        this.logMessage("Starting HTML cleanup");        var transform = this._filterContent(this.getInnerDocument().documentElement);        // serialize to a string        var contents = this._serializeOutputToString(transform);                this.logMessage("Cleanup done, sending document to server");        var request = Sarissa.getXmlHttpRequest();            if (!synchronous) {            request.onreadystatechange = (new ContextFixer(this._saveCallback,                                                this, request, redirect)).execute;            request.open("PUT", this.config.dst, true);            request.setRequestHeader("Content-type", this.config.content_type);            request.send(contents);            this.logMessage("Request sent to server");        } else {            this.logMessage('Sending request to server');            request.open("PUT", this.config.dst, false);            request.setRequestHeader("Content-type", this.config.content_type);            request.send(contents);            this.handleSaveResponse(request,redirect)        };    };        this.prepareForm = function(form, id) {        /* add a field to the form and place the contents in it            can be used for simple POST support where Kupu is part of a            form        */        var sourcetool = this.getTool('sourceedittool');        if (sourcetool) {sourcetool.cancelSourceMode();};        // make sure people can't edit or save during saving        if (!this._initialized) {            return;        }        this._initialized = false;                // set the window status so people can see we're actually saving        window.status= "Please wait while saving document...";/* Removed for MyFaces   TODO : Evaluate how to handle this.        // call (optional) beforeSave() method on all tools        for (var id in this.tools) {            var tool = this.tools[id];            if (tool.beforeSave) {                try {                    tool.beforeSave();                } catch(e) {                    alert(e);                    this._initialized = true;                    return;                };            };        };*/        // set a default id        if (!id) {            id = 'kupu';        };                // pass the content through the filters        this.logMessage("Starting HTML cleanup");        var transform = this._filterContent(this.getInnerDocument().documentElement);                // XXX need to fix this.  Sometimes a spurious "\n\n" text         // node appears in the transform, which breaks the Moz         // serializer on .xml        var contents =  this._serializeOutputToString(transform);                this.logMessage("Cleanup done, sending document to server");                // now create the form input, since IE 5.5 doesn't support the         // ownerDocument property we use window.document as a fallback (which        // will almost by definition be correct).        var document = form.ownerDocument ? form.ownerDocument : window.document;        var ta = document.createElement('textarea');        ta.style.visibility = 'hidden';        var text = document.createTextNode(contents);        ta.appendChild(text);        ta.setAttribute('name', id);                // and add it to the form        form.appendChild(ta);        // let the calling code know we have added the textarea        return true;    };    this.execCommand = function(command, param) {        /* general stuff like making current selection bold, italics etc.             and adding basic elements such as lists            */        if (!this._initialized) {            this.logMessage('Editor not initialized yet!');            return;        };        if (this.getBrowserName() == "IE") {            this._restoreSelection();        } else {            this.focusDocument();            if (command != 'useCSS') {                this.content_changed = true;                // note the negation: the argument doesn't work as                // expected...                // Done here otherwise it doesn't always work or gets lost                // after some commands                this.getDocument().execCommand('useCSS', !this.config.use_css);            };        };        this.getDocument().execCommand(command, param);        var message = 'Command ' + command + ' executed';        if (param) {            message += ' with parameter ' + param;        }        this.updateState();        this.logMessage(message);    };        this.getSelection = function() {        /* returns a Selection object wrapping the current selection */        if (this.getBrowserName() == "IE") {            this._restoreSelection();        };        return this.getDocument().getSelection();    };        this.getSelectedNode = function() {        /* returns the selected node (read: parent) or none */        return this.getSelection().getSelectedNode();    };    this.getNearestParentOfType = function(node, type) {        /* well the title says it all ;) */        var type = type.toLowerCase();        while (node) {            if (node.nodeName.toLowerCase() == type) {                return node            }               var node = node.parentNode;        }        return false;    };    this.removeNearestParentOfType = function(node, type) {        var nearest = this.getNearestParentOfType(node, type);        if (!nearest) {

⌨️ 快捷键说明

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