1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
| /* Switch between WYSIWYG and HTML source */
widgEditor.prototype.switchMode = function()
{
if (!this.locked)
{
this.locked = true;
/* Switch to HTML source */
if (this.wysiwyg)
{
this.updateWidgInput();
this.theTextarea.value = this.theInput.value;
this.theContainer.replaceChild(this.theTextarea, this.theIframe);
this.theToolbar.disable();
this.wysiwyg = false;
this.locked = false;
}
/* Switch to WYSIWYG */
else
{
this.updateWidgInput();
this.theContainer.replaceChild(this.theIframe, this.theTextarea);
this.writeDocument(this.theInput.value);
this.theToolbar.enable();
this.initEdit();
this.wysiwyg = true;
}
}
return true;
}
/* Update hidden input to reflect editor contents, for submission */
widgEditor.prototype.updateWidgInput = function()
{
if (this.wysiwyg)
{
/* Convert spans to semantics in Mozilla */
if (!this.IE)
{
this.convertSPANs(true);
}
this.paragraphise();
this.cleanSource();
}
else
{
this.theInput.value = this.theTextarea.value;
}
return true;
}
/* Write initial content to editor */
widgEditor.prototype.writeDocument = function(documentContent)
{
/* HTML template into which the HTML Editor content is inserted */
var documentTemplate = '\
<html>\
<head>\
INSERT:STYLESHEET:END\
</head>\
<body id="iframeBody">\
INSERT:CONTENT:END\
</body>\
</html>\
';
/* Insert dynamic variables/content into document */
/* IE needs stylesheet to be written inline */
if (typeof document.all != "undefined")
{
documentTemplate = documentTemplate.replace(/INSERT:STYLESHEET:END/, '<link rel="stylesheet" type="text/css" href="' + widgStylesheet + '"></link>');
}
/* Firefox can't have stylesheet written inline */
else
{
documentTemplate = documentTemplate.replace(/INSERT:STYLESHEET:END/, "");
}
documentTemplate = documentTemplate.replace(/INSERT:CONTENT:END/, documentContent);
this.theIframe.contentWindow.document.open();
this.theIframe.contentWindow.document.write(documentTemplate);
this.theIframe.contentWindow.document.close();
/* In Firefox stylesheet needs to be loaded separate to other HTML, because if it's loaded inline it causes Firefox to have problems with an empty document */
if (typeof document.all == "undefined")
{
var stylesheet = this.theIframe.contentWindow.document.createElement("link");
stylesheet.setAttribute("rel", "stylesheet");
stylesheet.setAttribute("type", "text/css");
stylesheet.setAttribute("href", widgStylesheet);
this.theIframe.contentWindow.document.getElementsByTagName("head")[0].appendChild(stylesheet);
}
return true;
}
/* Toolbar items */
function widgToolbar(theEditor)
{
var self = this;
this.widgEditorObject = theEditor;
/* Create toolbar ul element */
this.theList = document.createElement("ul");
this.theList.id = this.widgEditorObject.theInput.id + "WidgToolbar";
this.theList.className = "widgToolbar";
this.theList.widgToolbarObject = this;
/* Create toolbar items */
for (var i = 0; i < widgToolbarItems.length; i++)
{
switch (widgToolbarItems[i])
{
case "bold":
this.addButton(this.theList.id + "ButtonBold", "widgButtonBold", "Gras", "bold");
break;
case "italic":
this.addButton(this.theList.id + "ButtonItalic", "widgButtonItalic", "Italique", "italic");
break;
case "hyperlink":
this.addButton(this.theList.id + "ButtonLink", "widgButtonLink", "Lien", "link");
break;
case "unorderedlist":
this.addButton(this.theList.id + "ButtonUnordered", "widgButtonUnordered", "Liste à Puces", "insertunorderedlist");
break;
case "orderedlist":
this.addButton(this.theList.id + "ButtonOrdered", "widgButtonOrdered", "Liste Numérotée", "insertorderedlist");
break;
case "image":
this.addButton(this.theList.id + "ButtonImage", "widgButtonImage", "Image", "image");
break;
case "htmlsource":
this.addButton(this.theList.id + "ButtonHTML", "widgButtonHTML", "Code HTML", "html");
break;
case "blockformat":
this.addSelect(this.theList.id + "SelectBlock", "widgSelectBlock", widgSelectBlockOptions, "formatblock");
break;
}
}
return true;
}
/* Add button to toolbar */
widgToolbar.prototype.addButton = function(theID, theClass, theLabel, theAction)
{
var menuItem = document.createElement("li");
var theLink = document.createElement("a");
var theText = document.createTextNode(theLabel);
menuItem.id = theID;
menuItem.className = "widgEditButton";
theLink.href = "#";
theLink.title = theLabel;
theLink.className = theClass;
theLink.action = theAction;
theLink.onclick = widgToolbarAction;
theLink.onmouseover = widgToolbarMouseover;
theLink.appendChild(theText);
menuItem.appendChild(theLink);
this.theList.appendChild(menuItem);
return true;
}
/* Add select box to toolbar. theContentArray is an array of string pairs (option value, option label) */
widgToolbar.prototype.addSelect = function(theID, theClass, theContentArray, theAction)
{
var menuItem = document.createElement("li");
var theSelect = document.createElement("select");
menuItem.className = "widgEditSelect";
theSelect.id = theID;
theSelect.name = theID;
theSelect.className = theClass;
theSelect.action = theAction;
theSelect.onchange = widgToolbarAction;
for (var i = 0; i < theContentArray.length; i += 2)
{
var theOption = document.createElement("option");
var theText = document.createTextNode(theContentArray[i + 1]);
theOption.value = theContentArray[i];
theOption.appendChild(theText);
theSelect.appendChild(theOption);
}
menuItem.appendChild(theSelect);
this.theList.appendChild(menuItem);
return true;
}
/* Turn off toolbar items */
widgToolbar.prototype.disable = function()
{
/* Change class to disable buttons using CSS */
this.theList.className += " widgSource";
/* Loop through lis */
for (var i = 0; i < this.theList.childNodes.length; i++)
{
var theChild = this.theList.childNodes[i];
if (theChild.nodeName.toLowerCase() == "li" && theChild.className == "widgEditSelect")
{
/* Loop through li children to find select */
for (j = 0; j < theChild.childNodes.length; j++)
{
if (theChild.childNodes[j].nodeName.toLowerCase() == "select")
{
theChild.childNodes[j].disabled = "disabled";
break;
}
}
}
}
return true;
}
/* Turn on toolbar items */
widgToolbar.prototype.enable = function()
{
/* Change class to enable buttons using CSS */
this.theList.className = this.theList.className.replace(/ widgSource/, "");
/* Loop through lis */
for (var i = 0; i < this.theList.childNodes.length; i++)
{
var theChild = this.theList.childNodes[i];
if (theChild.nodeName.toLowerCase() == "li" && theChild.className == "widgEditSelect")
{
/* Loop through li children to find select */
for (j = 0; j < theChild.childNodes.length; j++)
{
if (theChild.childNodes[j].nodeName.toLowerCase() == "select")
{
theChild.childNodes[j].disabled = "";
break;
}
}
}
}
return true;
}
/* Change the status of the selected toolbar item */
widgToolbar.prototype.setState = function(theState, theStatus)
{
if (theState != "SelectBlock")
{
var theButton = document.getElementById(this.theList.id + "Button" + theState);
if (theButton != null)
{
if (theStatus == "on")
{
theButton.className = theButton.className.addClass("on");
}
else
{
theButton.className = theButton.className.removeClass("on");
}
}
}
else
{
var theSelect = document.getElementById(this.theList.id + "SelectBlock");
if (theSelect != null)
{
theSelect.value = "";
theSelect.value = theStatus;
}
}
return true;
}
/* Action taken when toolbar item activated */
function widgToolbarAction()
{
var theToolbar = this.parentNode.parentNode.widgToolbarObject;
var theWidgEditor = theToolbar.widgEditorObject;
var theIframe = theWidgEditor.theIframe;
var theSelection = "";
/* If somehow a button other than "HTML source" is clicked while viewing HTML source, ignore click */
if (!theWidgEditor.wysiwyg && this.action != "html")
{
return false;
}
switch (this.action)
{
case "formatblock":
theIframe.contentWindow.document.execCommand(this.action, false, this.value);
theWidgEditor.theToolbar.setState("SelectBlock", this.value);
break;
case "html":
theWidgEditor.switchMode();
break;
case "link":
if (this.parentNode.className.classExists("on"))
{
theIframe.contentWindow.document.execCommand("Unlink", false, null);
theWidgEditor.theToolbar.setState("Link", "off");
}
else
{
if (theIframe.contentWindow.document.selection)
{
theSelection = theIframe.contentWindow.document.selection.createRange().text;
if (theSelection == "")
{
alert("Veuillez choisir le texte à lier.");
break;
}
}
else
{
theSelection = theIframe.contentWindow.getSelection();
if (theSelection == "")
{
alert("Veuillez choisir le texte à lier.");
break;
}
}
var theURL = prompt("Entrer la cible du lien:", "http://");
if (theURL != null)
{
theIframe.contentWindow.document.execCommand("CreateLink", false, theURL);
theWidgEditor.theToolbar.setState("Link", "on");
}
}
break;
case "image":
var theImage = prompt("Indiquez l'emplacement de l'image:", "");
if (theImage != null && theImage != "")
{
var theAlt = prompt("Entrez un texte alternatif:", "");
var theSelection = null;
var theRange = null;
/* IE selections */
if (theIframe.contentWindow.document.selection)
{
/* Escape quotes in alt text */
theAlt = theAlt.replace(/"/g, "'");
theSelection = theIframe.contentWindow.document.selection;
theRange = theSelection.createRange();
theRange.collapse(false);
theRange.pasteHTML("<img alt=\"" + theAlt + "\" src=\"" + theImage + "\" />");
break;
}
/* Mozilla selections */
else
{
try
{
theSelection = theIframe.contentWindow.getSelection();
}
catch (e)
{
return false;
}
theRange = theSelection.getRangeAt(0);
theRange.collapse(false);
var theImageNode = theIframe.contentWindow.document.createElement("img");
theImageNode.src = theImage;
theImageNode.alt = theAlt;
theRange.insertNode(theImageNode);
break;
}
}
else
{
return false;
}
default:
theIframe.contentWindow.document.execCommand(this.action, false, null);
var theAction = this.action.replace(/^./, function(match){return match.toUpperCase();});
/* Turn off unordered toolbar item if ordered toolbar item was activated */
if (this.action == "insertorderedlist")
{
theAction = "Ordered";
theWidgEditor.theToolbar.setState("Unordered", "off");
}
/* Turn off ordered toolbar item if unordered toolbar item was activated */
if (this.action == "insertunorderedlist")
{
theAction = "Unordered";
theWidgEditor.theToolbar.setState("Ordered", "off");
}
/* If toolbar item was turned on */
if (theIframe.contentWindow.document.queryCommandState(this.action, false, null))
{
theWidgEditor.theToolbar.setState(theAction, "on");
}
else
{
theWidgEditor.theToolbar.setState(theAction, "off");
}
}
if (theWidgEditor.wysiwyg == true)
{
theIframe.contentWindow.focus();
}
else
{
theWidgEditor.theTextarea.focus();
}
return false;
}
/* Check the nesting of the current cursor position/selection */
function widgToolbarCheckState(theWidgEditor, resubmit)
{
if (!resubmit)
{
/* Allow browser to update selection before using the selection */
setTimeout(function(){widgToolbarCheckState(theWidgEditor, true); return true;}, 500);
}
var theSelection = null;
var theRange = null;
var theParentNode = null;
var theLevel = 0;
/* Turn off all the buttons */
var menuListItems = theWidgEditor.theToolbar.theList.childNodes;
for (var i = 0; i < menuListItems.length; i++)
{
menuListItems[i].className = menuListItems[i].className.removeClass("on");
}
/* IE selections */
if (theWidgEditor.theIframe.contentWindow.document.selection)
{
theSelection = theWidgEditor.theIframe.contentWindow.document.selection;
theRange = theSelection.createRange();
try
{
theParentNode = theRange.parentElement();
}
catch (e)
{
return false;
}
}
/* Mozilla selections */
else
{
try
{
theSelection = theWidgEditor.theIframe.contentWindow.getSelection();
}
catch (e)
{
return false;
}
theRange = theSelection.getRangeAt(0);
theParentNode = theRange.commonAncestorContainer;
}
while (theParentNode.nodeType == 3)
{
theParentNode = theParentNode.parentNode;
}
while (theParentNode.nodeName.toLowerCase() != "body")
{
switch (theParentNode.nodeName.toLowerCase())
{
case "a":
theWidgEditor.theToolbar.setState("Link", "on");
break;
case "em":
theWidgEditor.theToolbar.setState("Italic", "on");
break;
case "li":
break;
case "ol":
theWidgEditor.theToolbar.setState("Ordered", "on");
theWidgEditor.theToolbar.setState("Unordered", "off");
break;
case "span":
if (theParentNode.getAttribute("style") == "font-weight: bold;")
{
theWidgEditor.theToolbar.setState("Bold", "on");
}
else if (theParentNode.getAttribute("style") == "font-style: italic;")
{
theWidgEditor.theToolbar.setState("Italic", "on");
}
else if (theParentNode.getAttribute("style") == "font-weight: bold; font-style: italic;")
{
theWidgEditor.theToolbar.setState("Bold", "on");
theWidgEditor.theToolbar.setState("Italic", "on");
}
else if (theParentNode.getAttribute("style") == "font-style: italic; font-weight: bold;")
{
theWidgEditor.theToolbar.setState("Bold", "on");
theWidgEditor.theToolbar.setState("Italic", "on");
}
break;
case "strong":
theWidgEditor.theToolbar.setState("Bold", "on");
break;
case "ul":
theWidgEditor.theToolbar.setState("Unordered", "on");
theWidgEditor.theToolbar.setState("Ordered", "off");
break;
default:
theWidgEditor.theToolbar.setState("SelectBlock", "<" + theParentNode.nodeName.toLowerCase() + ">");
break;
}
theParentNode = theParentNode.parentNode;
theLevel++;
}
return true;
}
/* Turn off browser status display for toolbar items */
function widgToolbarMouseover()
{
window.status = "";
return true;
}
function acceptableChildren(theNode)
{
var theChildren = theNode.childNodes;
for (var i = 0; i < theChildren.length; i++)
{
if (!theChildren[i].nodeName.isAcceptedElementName())
{
if (!theChildren[i].nodeName.isInlineName())
{
if (theNode.nodeName.toLowerCase() == "p")
{
acceptableChildren(replaceNodeWithChildren(theNode));
return true;
}
changeNodeType(theChildren[i], "p");
}
else
{
replaceNodeWithChildren(theChildren[i]);
}
i = -1;
}
}
for (var i = 0; i < theChildren.length; i++)
{
acceptableChildren(theChildren[i]);
}
return true;
}
/* Change the type of a node, e.g. h3 to p */
function changeNodeType(theNode, nodeType)
{
var theChildren = new Array();
var theNewNode = document.createElement(nodeType);
var theParent = theNode.parentNode;
if (theParent != null)
{
for (var i = 0; i < theNode.childNodes.length; i++)
{
theChildren.push(theNode.childNodes[i].cloneNode(true));
}
for (var i = 0; i < theChildren.length; i++)
{
theNewNode.appendChild(theChildren[i]);
}
theParent.replaceChild(theNewNode, theNode);
}
return true;
}
/* Replace a node with its children -- delete the item and move its children up one level in the hierarchy */
function replaceNodeWithChildren(theNode)
{
var theChildren = new Array();
var theParent = theNode.parentNode;
if (theParent != null)
{
for (var i = 0; i < theNode.childNodes.length; i++)
{
theChildren.push(theNode.childNodes[i].cloneNode(true));
}
for (var i = 0; i < theChildren.length; i++)
{
theParent.insertBefore(theChildren[i], theNode);
}
theParent.removeChild(theNode);
return theParent;
}
return true;
}
/* Add a class to a string */
String.prototype.addClass = function(theClass)
{
if (this != "")
{
if (!this.classExists(theClass))
{
return this + " " + theClass;
}
}
else
{
return theClass;
}
return this;
}
/* Check if a class exists in a string */
String.prototype.classExists = function(theClass)
{
var regString = "(^| )" + theClass + "\W*";
var regExpression = new RegExp(regString);
if (regExpression.test(this))
{
return true;
}
return false;
}
/* Check if a string is the nodeName of an accepted element */
String.prototype.isAcceptedElementName = function()
{
var elementList = new Array("#text", "a", "em", "h1", "h2", "h3", "h4", "h5", "h6", "img", "li", "ol", "p", "strong", "ul");
var theName = this.toLowerCase();
for (var i = 0; i < elementList.length; i++)
{
if (theName == elementList[i])
{
return true;
}
}
return false;
}
/* Check if a string is the nodeName of an inline element */
String.prototype.isInlineName = function()
{
var inlineList = new Array("#text", "a", "em", "font", "span", "strong", "u");
var theName = this.toLowerCase();
for (var i = 0; i < inlineList.length; i++)
{
if (theName == inlineList[i])
{
return true;
}
}
return false;
}
/* Remove a class from a string */
String.prototype.removeClass = function(theClass)
{
var regString = "(^| )" + theClass + "\W*";
var regExpression = new RegExp(regString);
return this.replace(regExpression, "");
}
/* Reverse a string */
String.prototype.reverse = function()
{
var theString = "";
for (var i = this.length - 1; i >= 0; i--)
{
theString += this.charAt(i);
}
return theString;
}
/* Make tags valid by converting uppercase element and attribute names to lowercase and quoting attributes */
String.prototype.validTags = function()
{
var theString = this;
/* Replace uppercase element names with lowercase */
theString = theString.replace(/<[^> ]*/g, function(match){return match.toLowerCase();});
/* Replace uppercase attribute names with lowercase */
theString = theString.replace(/<[^>]*>/g, function(match)
{
match = match.replace(/ [^=]+=/g, function(match2){return match2.toLowerCase();});
return match;
});
/* Put quotes around unquoted attributes */
theString = theString.replace(/<[^>]*>/g, function(match)
{
match = match.replace(/( [^=]+=)([^"][^ >]*)/g, "$1\"$2\"");
return match;
});
return theString;
} |
Partager