Вход Регистрация
Файл: upload/static/jscript/wysiwyg/tiny_mce/plugins/lists/editor_plugin_src.js
Строк: 963
<?php
/**
 * editor_plugin_src.js
 *
 * Copyright 2011, Moxiecode Systems AB
 * Released under LGPL License.
 *
 * License: http://tinymce.moxiecode.com/license
 * Contributing: http://tinymce.moxiecode.com/contributing
 */

(function() {
    var 
each tinymce.eachEvent tinymce.dom.Eventbookmark;

    
// Skips text nodes that only contain whitespace since they aren't semantically important.
    
function skipWhitespaceNodes(enext) {
        while (
&& (e.nodeType === || (e.nodeType === && /^[ tnr]*$/.test(e.nodeValue)))) {
            
next(e);
        }
        return 
e;
    }
    
    function 
skipWhitespaceNodesBackwards(e) {
        return 
skipWhitespaceNodes(e, function(e) { return e.previousSibling; });
    }
    
    function 
skipWhitespaceNodesForwards(e) {
        return 
skipWhitespaceNodes(e, function(e) { return e.nextSibling; });
    }
    
    function 
hasParentInList(ede, list) {
        return 
ed.dom.getParent(e, function(p) {
            return 
tinymce.inArray(list, p) !== -1;
        });
    }
    
    function 
isList(e) {
        return 
&& (e.tagName === 'OL' || e.tagName === 'UL');
    }
    
    function 
splitNestedLists(elementdom) {
        var 
tmpnestedwrapItem;
        
tmp skipWhitespaceNodesBackwards(element.lastChild);
        while (
isList(tmp)) {
            
nested tmp;
            
tmp skipWhitespaceNodesBackwards(nested.previousSibling);
        }
        if (
nested) {
            
wrapItem dom.create('li', { style'list-style-type: none;'});
            
dom.split(elementnested);
            
dom.insertAfter(wrapItemnested);
            
wrapItem.appendChild(nested);
            
wrapItem.appendChild(nested);
            
element wrapItem.previousSibling;
        }
        return 
element;
    }
    
    function 
attemptMergeWithAdjacent(eallowDifferentListStylesmergeParagraphs) {
        
attemptMergeWithPrevious(eallowDifferentListStylesmergeParagraphs);
        return 
attemptMergeWithNext(eallowDifferentListStylesmergeParagraphs);
    }
    
    function 
attemptMergeWithPrevious(eallowDifferentListStylesmergeParagraphs) {
        var 
prev skipWhitespaceNodesBackwards(e.previousSibling);
        if (
prev) {
            return 
attemptMerge(preveallowDifferentListStyles prev falsemergeParagraphs);
        } else {
            return 
e;
        }
    }
    
    function 
attemptMergeWithNext(eallowDifferentListStylesmergeParagraphs) {
        var 
next skipWhitespaceNodesForwards(e.nextSibling);
        if (
next) {
            return 
attemptMerge(enextallowDifferentListStyles next falsemergeParagraphs);
        } else {
            return 
e;
        }
    }
    
    function 
attemptMerge(e1e2differentStylesMasterElementmergeParagraphs) {
        if (
canMerge(e1e2, !!differentStylesMasterElementmergeParagraphs)) {
            return 
merge(e1e2differentStylesMasterElement);
        } else if (
e1 && e1.tagName === 'LI' && isList(e2)) {
            
// Fix invalidly nested lists.
            
e1.appendChild(e2);
        }
        return 
e2;
    }
    
    function 
canMerge(e1e2allowDifferentListStylesmergeParagraphs) {
        if (!
e1 || !e2) {
            return 
false;
        } else if (
e1.tagName === 'LI' && e2.tagName === 'LI') {
            return 
e2.style.listStyleType === 'none' || containsOnlyAList(e2);
        } else if (
isList(e1)) {
            return (
e1.tagName === e2.tagName && (allowDifferentListStyles || e1.style.listStyleType === e2.style.listStyleType)) || isListForIndent(e2);
        } else if (
mergeParagraphs && e1.tagName === 'P' && e2.tagName === 'P') {
            return 
true;
        } else {
            return 
false;
        }
    }
    
    function 
isListForIndent(e) {
        var 
firstLI skipWhitespaceNodesForwards(e.firstChild), lastLI skipWhitespaceNodesBackwards(e.lastChild);
        return 
firstLI && lastLI && isList(e) && firstLI === lastLI && (isList(firstLI) || firstLI.style.listStyleType === 'none'  || containsOnlyAList(firstLI));
    }
    
    function 
containsOnlyAList(e) {
        var 
firstChild skipWhitespaceNodesForwards(e.firstChild), lastChild skipWhitespaceNodesBackwards(e.lastChild);
        return 
firstChild && lastChild && firstChild === lastChild && isList(firstChild);
    }
    
    function 
merge(e1e2masterElement) {
        var 
lastOriginal skipWhitespaceNodesBackwards(e1.lastChild), firstNew skipWhitespaceNodesForwards(e2.firstChild);
        if (
e1.tagName === 'P') {
            
e1.appendChild(e1.ownerDocument.createElement('br'));
        }
        while (
e2.firstChild) {
            
e1.appendChild(e2.firstChild);
        }
        if (
masterElement) {
            
e1.style.listStyleType masterElement.style.listStyleType;
        }
        
e2.parentNode.removeChild(e2);
        
attemptMerge(lastOriginalfirstNewfalse);
        return 
e1;
    }
    
    function 
findItemToOperateOn(edom) {
        var 
item;
        if (!
dom.is(e'li,ol,ul')) {
            
item dom.getParent(e'li');
            if (
item) {
                
item;
            }
        }
        return 
e;
    }
    
    
tinymce.create('tinymce.plugins.Lists', {
        
init: function(edurl) {
            var 
enterDownInEmptyList false;
            function 
isTriggerKey(e) {
                return 
e.keyCode === && (ed.queryCommandState('InsertUnorderedList') || ed.queryCommandState('InsertOrderedList'));
            }
            function 
isEnterInEmptyListItem(ede) {
                var 
sel ed.selectionn;
                if (
e.keyCode === 13) {
                    
sel.getStart();
                    
enterDownInEmptyList sel.isCollapsed() && && n.tagName === 'LI' && n.childNodes.length === 0;
                    return 
enterDownInEmptyList;
                }
            }
            function 
cancelKeys(ede) {
                if (
isTriggerKey(e) || isEnterInEmptyListItem(ede)) {
                    return 
Event.cancel(e);
                }
            }
            
            
this.ed ed;
            
ed.addCommand('Indent'this.indentthis);
            
ed.addCommand('Outdent'this.outdentthis);
            
ed.addCommand('InsertUnorderedList', function() {
                
this.applyList('UL''OL');
            }, 
this);
            
ed.addCommand('InsertOrderedList', function() {
                
this.applyList('OL''UL');
            }, 
this);
            
            
ed.onInit.add(function() {
                
ed.editorCommands.addCommands({
                    
'outdent': function() {
                        var 
sel ed.selectiondom ed.dom;
                        function 
hasStyleIndent(n) {
                            
dom.getParent(ndom.isBlock);
                            return 
&& (parseInt(ed.dom.getStyle(n'margin-left') || 010) + parseInt(ed.dom.getStyle(n'padding-left') || 010)) > 0;
                        }
                        return 
hasStyleIndent(sel.getStart()) || hasStyleIndent(sel.getEnd()) || ed.queryCommandState('InsertOrderedList') || ed.queryCommandState('InsertUnorderedList');
                    }
                }, 
'state');
            });
            
            
ed.onKeyUp.add(function(ede) {
                var 
nrng;
                if (
isTriggerKey(e)) {
                    
ed.execCommand(e.shiftKey 'Outdent' 'Indent'truenull);
                    return 
Event.cancel(e);
                } else if (
enterDownInEmptyList && isEnterInEmptyListItem(ede)) {
                    if (
ed.queryCommandState('InsertOrderedList')) {
                        
ed.execCommand('InsertOrderedList');
                    } else {
                        
ed.execCommand('InsertUnorderedList');
                    }
                    
ed.selection.getStart();
                    if (
&& n.tagName === 'LI') {
                        
// Fix the caret position on IE since it jumps back up to the previous list item.
                        
ed.dom.getParent(n'ol,ul').nextSibling;
                        if (
&& n.tagName === 'P') {
                            if (!
n.firstChild) {
                                
n.appendChild(ed.getDoc().createTextNode(''));
                            }
                            
rng ed.dom.createRng();
                            
rng.setStart(n.firstChild1);
                            
rng.setEnd(n.firstChild1);
                            
ed.selection.setRng(rng);
                        }
                    }
                    return 
Event.cancel(e);
                }
            });
            
ed.onKeyPress.add(cancelKeys);
            
ed.onKeyDown.add(cancelKeys);
        },
        
        
applyList: function(targetListTypeoppositeListType) {
            var 
thised t.eddom ed.domapplied = [], hasSameType falsehasOppositeType falsehasNonList falseactions,
                
selectedBlocks ed.selection.getSelectedBlocks();
            
            function 
cleanupBr(e) {
                if (
&& e.tagName === 'BR') {
                    
dom.remove(e);
                }
            }
            
            function 
makeList(element) {
                var list = 
dom.create(targetListType), li;
                function 
adjustIndentForNewList(element) {
                    
// If there's a margin-left, outdent one level to account for the extra list margin.
                    
if (element.style.marginLeft || element.style.paddingLeft) {
                        
t.adjustPaddingFunction(false)(element);
                    }
                }
                
                if (
element.tagName === 'LI') {
                    
// No change required.
                
} else if (element.tagName === 'P' || element.tagName === 'DIV' || element.tagName === 'BODY') {
                    
processBrs(element, function(startSectionbrpreviousBR) {
                        
doWrapList(startSectionbrelement.tagName === 'BODY' null startSection.parentNode);
                        
li startSection.parentNode;
                        
adjustIndentForNewList(li);
                        
cleanupBr(br);
                    });
                    if (
element.tagName === 'P' || selectedBlocks.length 1) {
                        
dom.split(li.parentNode.parentNodeli.parentNode);
                    }
                    
attemptMergeWithAdjacent(li.parentNodetrue);
                    return;
                } else {
                    
// Put the list around the element.
                    
li dom.create('li');
                    
dom.insertAfter(lielement);
                    
li.appendChild(element);
                    
adjustIndentForNewList(element);
                    
element li;
                }
                
dom.insertAfter(list, element);
                list.
appendChild(element);
                
attemptMergeWithAdjacent(list, true);
                
applied.push(element);
            }
            
            function 
doWrapList(startendtemplate) {
                var 
listarttmpi;
                while (!
dom.isBlock(start.parentNode) && start.parentNode !== dom.getRoot()) {
                    
start dom.split(start.parentNodestart.previousSibling);
                    
start start.nextSibling;
                    
start;
                }
                if (
template) {
                    
li template.cloneNode(true);
                    
start.parentNode.insertBefore(listart);
                    while (
li.firstChilddom.remove(li.firstChild);
                    
li dom.rename(li'li');
                } else {
                    
li dom.create('li');
                    
start.parentNode.insertBefore(listart);
                }
                while (
&& != end) {
                    
tmp n.nextSibling;
                    
li.appendChild(n);
                    
tmp;
                }
                if (
li.childNodes.length === 0) {
                    
li.innerHTML '<br _mce_bogus="1" />';
                }
                
makeList(li);
            }
            
            function 
processBrs(elementcallback) {
                var 
startSectionpreviousBREND_TO_START 3START_TO_END 1,
                    
breakElements 'br,ul,ol,p,div,h1,h2,h3,h4,h5,h6,table,blockquote,address,pre,form,center,dl';
                function 
isAnyPartSelected(startend) {
                    var 
dom.createRng(), sel;
                    
bookmark.keep true;
                    
ed.selection.moveToBookmark(bookmark);
                    
bookmark.keep false;
                    
sel ed.selection.getRng(true);
                    if (!
end) {
                        
end start.parentNode.lastChild;
                    }
                    
r.setStartBefore(start);
                    
r.setEndAfter(end);
                    return !(
r.compareBoundaryPoints(END_TO_STARTsel) > || r.compareBoundaryPoints(START_TO_ENDsel) <= 0);
                }
                function 
nextLeaf(br) {
                    if (
br.nextSibling)
                        return 
br.nextSibling;
                    if (!
dom.isBlock(br.parentNode) && br.parentNode !== dom.getRoot())
                        return 
nextLeaf(br.parentNode);
                }
                
// Split on BRs within the range and process those.
                
startSection element.firstChild;
                
// First mark the BRs that have any part of the previous section selected.
                
var trailingContentSelected false;
                
each(dom.select(breakElementselement), function(br) {
                    var 
b;
                    if (
br.hasAttribute && br.hasAttribute('_mce_bogus')) {
                        return 
true// Skip the bogus Brs that are put in to appease Firefox and Safari.
                    
}
                    if (
isAnyPartSelected(startSectionbr)) {
                        
dom.addClass(br'_mce_tagged_br');
                        
startSection nextLeaf(br);
                    }
                });
                
trailingContentSelected = (startSection && isAnyPartSelected(startSectionundefined));
                
startSection element.firstChild;
                
each(dom.select(breakElementselement), function(br) {
                    
// Got a section from start to br.
                    
var tmp nextLeaf(br);
                    if (
br.hasAttribute && br.hasAttribute('_mce_bogus')) {
                        return 
true// Skip the bogus Brs that are put in to appease Firefox and Safari.
                    
}
                    if (
dom.hasClass(br'_mce_tagged_br')) {
                        
callback(startSectionbrpreviousBR);
                        
previousBR null;
                    } else {
                        
previousBR br;
                    }
                    
startSection tmp;
                });
                if (
trailingContentSelected) {
                    
callback(startSectionundefinedpreviousBR);
                }
            }
            
            function 
wrapList(element) {
                
processBrs(element, function(startSectionbrpreviousBR) {
                    
// Need to indent this part
                    
doWrapList(startSectionbr);
                    
cleanupBr(br);
                    
cleanupBr(previousBR);
                });
            }
            
            function 
changeList(element) {
                if (
tinymce.inArray(appliedelement) !== -1) {
                    return;
                }
                if (
element.parentNode.tagName === oppositeListType) {
                    
dom.split(element.parentNodeelement);
                    
makeList(element);
                    
attemptMergeWithNext(element.parentNodefalse);
                }
                
applied.push(element);
            }
            
            function 
convertListItemToParagraph(element) {
                var 
childnextChildmergedElementsplitLast;
                if (
tinymce.inArray(appliedelement) !== -1) {
                    return;
                }
                
element splitNestedLists(elementdom);
                while (
dom.is(element.parentNode'ol,ul,li')) {
                    
dom.split(element.parentNodeelement);
                }
                
// Push the original element we have from the selection, not the renamed one.
                
applied.push(element);
                
element dom.rename(element'p');
                
mergedElement attemptMergeWithAdjacent(elementfalseed.settings.force_br_newlines);
                if (
mergedElement === element) {
                    
// Now split out any block elements that can't be contained within a P.
                    // Manually iterate to ensure we handle modifications correctly (doesn't work with tinymce.each)
                    
child element.firstChild;
                    while (
child) {
                        if (
dom.isBlock(child)) {
                            
child dom.split(child.parentNodechild);
                            
splitLast true;
                            
nextChild child.nextSibling && child.nextSibling.firstChild
                        } else {
                            
nextChild child.nextSibling;
                            if (
splitLast && child.tagName === 'BR') {
                                
dom.remove(child);
                            }
                            
splitLast false;
                        }
                        
child nextChild;
                    }
                }
            }
            
            
each(selectedBlocks, function(e) {
                
findItemToOperateOn(edom);
                if (
e.tagName === oppositeListType || (e.tagName === 'LI' && e.parentNode.tagName === oppositeListType)) {
                    
hasOppositeType true;
                } else if (
e.tagName === targetListType || (e.tagName === 'LI' && e.parentNode.tagName === targetListType)) {
                    
hasSameType true;
                } else {
                    
hasNonList true;
                }
            });

            if (
hasNonList || hasOppositeType || selectedBlocks.length === 0) {
                
actions = {
                    
'LI'changeList,
                    
'H1'makeList,
                    
'H2'makeList,
                    
'H3'makeList,
                    
'H4'makeList,
                    
'H5'makeList,
                    
'H6'makeList,
                    
'P'makeList,
                    
'BODY'makeList,
                    
'DIV'selectedBlocks.length makeList wrapList,
                    
defaultActionwrapList
                
};
            } else {
                
actions = {
                    
defaultActionconvertListItemToParagraph
                
};
            }
            
this.process(actions);
        },
        
        
indent: function() {
            var 
ed this.eddom ed.domindented = [];
            
            function 
createWrapItem(element) {
                var 
wrapItem dom.create('li', { style'list-style-type: none;'});
                
dom.insertAfter(wrapItemelement);
                return 
wrapItem;
            }
            
            function 
createWrapList(element) {
                var 
wrapItem createWrapItem(element),
                    list = 
dom.getParent(element'ol,ul'),
                    
listType = list.tagName,
                    
listStyle dom.getStyle(list, 'list-style-type'),
                    
attrs = {},
                    
wrapList;
                if (
listStyle !== '') {
                    
attrs.style 'list-style-type: ' listStyle ';';
                }
                
wrapList dom.create(listTypeattrs);
                
wrapItem.appendChild(wrapList);
                return 
wrapList;
            }
            
            function 
indentLI(element) {
                if (!
hasParentInList(edelementindented)) {
                    
element splitNestedLists(elementdom);
                    var 
wrapList createWrapList(element);
                    
wrapList.appendChild(element);
                    
attemptMergeWithAdjacent(wrapList.parentNodefalse);
                    
attemptMergeWithAdjacent(wrapListfalse);
                    
indented.push(element);
                }
            }
            
            
this.process({
                
'LI'indentLI,
                
defaultActionthis.adjustPaddingFunction(true)
            });
            
        },
        
        
outdent: function() {
            var 
thised t.eddom ed.domoutdented = [];
            
            function 
outdentLI(element) {
                var 
listElementtargetParentalign;
                if (!
hasParentInList(edelementoutdented)) {
                    if (
dom.getStyle(element'margin-left') !== '' || dom.getStyle(element'padding-left') !== '') {
                        return 
t.adjustPaddingFunction(false)(element);
                    }
                    
align dom.getStyle(element'text-align'true);
                    if (
align === 'center' || align === 'right') {
                        
dom.setStyle(element'text-align''left');
                        return;
                    }
                    
element splitNestedLists(elementdom);
                    
listElement element.parentNode;
                    
targetParent element.parentNode.parentNode;
                    if (
targetParent.tagName === 'P') {
                        
dom.split(targetParentelement.parentNode);
                    } else {
                        
dom.split(listElementelement);
                        if (
targetParent.tagName === 'LI') {
                            
// Nested list, need to split the LI and go back out to the OL/UL element.
                            
dom.split(targetParentelement);
                        } else if (!
dom.is(targetParent'ol,ul')) {
                            
dom.rename(element'p');
                        }
                    }
                    
outdented.push(element);
                }
            }
            
            
this.process({
                
'LI'outdentLI,
                
defaultActionthis.adjustPaddingFunction(false)
            });
            
            
each(outdentedattemptMergeWithAdjacent);
        },
        
        
process: function(actions) {
            var 
thissel t.ed.selectiondom t.ed.domselectedBlocksr;
            function 
processElement(element) {
                
dom.removeClass(element'_mce_act_on');
                if (!
element || element.nodeType !== 1) {
                    return;
                }
                
element findItemToOperateOn(elementdom);
                var 
action actions[element.tagName];
                if (!
action) {
                    
action actions.defaultAction;
                }
                
action(element);
            }
            function 
recurse(element) {
                
t.splitSafeEach(element.childNodesprocessElement);
            }
            function 
brAtEdgeOfSelection(containeroffset) {
                return 
offset >= && container.hasChildNodes() && offset container.childNodes.length &&
                        
container.childNodes[offset].tagName === 'BR';
            }
            
selectedBlocks sel.getSelectedBlocks();
            if (
selectedBlocks.length === 0) {
                
selectedBlocks = [ dom.getRoot() ];
            }

            
sel.getRng(true);
            if (!
r.collapsed) {
                if (
brAtEdgeOfSelection(r.endContainerr.endOffset 1)) {
                    
r.setEnd(r.endContainerr.endOffset 1);
                    
sel.setRng(r);
                }
                if (
brAtEdgeOfSelection(r.startContainerr.startOffset)) {
                    
r.setStart(r.startContainerr.startOffset 1);
                    
sel.setRng(r);
                }
            }
            
bookmark sel.getBookmark();
            
actions.OL actions.UL recurse;
            
t.splitSafeEach(selectedBlocksprocessElement);
            
sel.moveToBookmark(bookmark);
            
bookmark null;
            
// Avoids table or image handles being left behind in Firefox.
            
t.ed.execCommand('mceRepaint');
        },
        
        
splitSafeEach: function(elementsf) {
            if (
tinymce.isGecko && (/Firefox/[12].[0-9]/.test(navigator.userAgent) ||
                    /
Firefox/3.[0-4]/.test(navigator.userAgent))) {
                
this.classBasedEach(elementsf);
            } else {
                
each(elementsf);
            }
        },
        
        
classBasedEach: function(elementsf) {
            var 
dom this.ed.domnodeselement;
            
// Mark nodes
            
each(elements, function(element) {
                
dom.addClass(element'_mce_act_on');
            });
            
nodes dom.select('._mce_act_on');
            while (
nodes.length 0) {
                
element nodes.shift();
                
dom.removeClass(element'_mce_act_on');
                
f(element);
                
nodes dom.select('._mce_act_on');
            }
        },
        
        
adjustPaddingFunction: function(isIndent) {
            var 
indentAmountindentUnitsed this.ed;
            
indentAmount ed.settings.indentation;
            
indentUnits = /[a-z%]+/i.exec(indentAmount);
            
indentAmount parseInt(indentAmount10);
            return function(
element) {
                var 
currentIndentnewIndentAmount;
                
currentIndent parseInt(ed.dom.getStyle(element'margin-left') || 010) + parseInt(ed.dom.getStyle(element'padding-left') || 010);
                if (
isIndent) {
                    
newIndentAmount currentIndent indentAmount;
                } else {
                    
newIndentAmount currentIndent indentAmount;
                }
                
ed.dom.setStyle(element'padding-left''');
                
ed.dom.setStyle(element'margin-left'newIndentAmount newIndentAmount indentUnits '');
            };
        },
        
        
getInfo: function() {
            return {
                
longname 'Lists',
                
author 'Moxiecode Systems AB',
                
authorurl 'http://tinymce.moxiecode.com',
                
infourl 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/lists',
                
version tinymce.majorVersion "." tinymce.minorVersion
            
};
        }
    });
    
tinymce.PluginManager.add("lists"tinymce.plugins.Lists);
}());
?>
Онлайн: 0
Реклама