$.editor = { defaultOptions: { controls: "bold italic underline strikethrough | style removeformat | bullets numbering | image link | alignleft center alignright justify", styles: [["Обычный текст", "
"], ["Заголовок 2", "
', null); } } } }, this)); // property для image $doc.bind("mouseover", $.proxy(function(event) { switch (event.target.tagName.toLowerCase()) { case 'img': $(event.target).css({'opacity':'0.9', 'filter':'alpha(opacity = 90);', 'cursor':'default'}); break; } }, this)); $doc.bind("mouseout", $.proxy(function(event) { switch (event.target.tagName.toLowerCase()) { case 'img': $(event.target).css({'opacity':'', 'filter':'', 'cursor':''}); break; } }, this)); $doc.bind("mousedown", $.proxy(function(event) { if ($(event.target).parents('.builder-prop').length > 0) return; switch (event.target.tagName.toLowerCase()) { case 'img': this.showImageProp($(event.target)); break; default: this.hideImageProp(); break; } }, this)); $doc.bind("keypress keydown keyup mouseup", $.proxy(function() { var d = this.$doc[0]; var h = Math.max(Math.max(d.body.scrollHeight, d.documentElement.scrollHeight), Math.max(d.body.offsetHeight, d.documentElement.offsetHeight), Math.max(d.body.clientHeight, d.documentElement.clientHeight)); //var h = Math.ceil($doc.find('body').height()+16); //if (h < 200) h = 200; this.frame.height(h); // Проверяем чтобы всегда вконце было
last = (d.body.childNodes.length == 0)?null:d.body.childNodes[d.body.childNodes.length-1]; if (last == null || typeof(last.tagName) == 'undefined' || last.tagName.toLowerCase() != 'p') { $('').appendTo(d.body); } }, this)); // Work around for bug in IE which causes the editor to lose // focus when clicking below the end of the document. if ($.browser.msie) $doc.click(function() {_this.focus();}); // Load the content this.updateFrame(); // Bind the ie specific iframe event handlers if ($.browser.msie) { // Save the current user selection. This code is needed since IE will // reset the selection just after the beforedeactivate event and just // before the beforeactivate event. $doc.bind("beforedeactivate beforeactivate selectionchange keypress", function(e) { // Flag the editor as inactive if (e.type == "beforedeactivate") _this.inactive = true; // Get rid of the bogus selection and flag the editor as active else if (e._this == "beforeactivate") { if (!_this.inactive && _this.range && _this.range.length > 1) _this.range.shift(); delete _this.inactive; } // Save the selection when the editor is active else if (!_this.inactive) { if (!_this.range) _this.range = []; _this.range.unshift(_this.getRange()); // We only need the last 2 selections while (_this.range.length > 2) _this.range.pop(); } }); // Restore the text range when the iframe gains focus this.frame.focus(function() { _this.restoreRange(); }); } // Обновляем textarea когда теряется фокус ($.browser.mozilla ? $doc : $(contentWindow)).blur(function() { _this.updateTextArea(); }); // Enable the toolbar buttons as the user types or clicks //$doc.click(this.hidePopups) $doc.bind("keyup mouseup", $.proxy( function() { this.refreshButtons(); }, this)); this.frame.show(); $(function() { var group = _this.toolbar.children("div[class='builder-btn-group']:last"), wid = _this.main.width(); // var hgt = group.offset().top + group.outerHeight() - _this.toolbar.offset().top + 1; // _this.toolbar.height(hgt); // Switch the iframe into design mode if enabled _this.disable(); }); idB = $('#wisiwigBody'); if (idB.length) { var h = Math.ceil(idB.height()/100)*100; _this.frame.height(h); idB.remove(); } }, refreshButtons : function () { // Webkit requires focus before queryCommandEnabled will return anything but false if ($.browser.webkit && !this.focused) { this.frame[0].contentWindow.focus(); ///window.focus(); this.focused = true; } // Get the object used for checking queryCommandEnabled var queryObj = ($.browser.msie)?this.getRange():this.doc; //_this = this; $.each(this.toolbar.find("button"), $.proxy(function(idx, elem) { var $elem = $(elem), buttonName = $.data(elem, 'name'), button = this.buttons[buttonName], command = $.nvl(button.command, buttonName), enabled = true; if (this.disabled) { enabled = false; } else if ($.isFunction(button.enabled)) { enabled = $.proxy(button.enabled, button)(); } else if ($.isset(button.enabled)) { enabled = button.enabled; } /* else if (($.browser.msie && (command == "undo" || command == "redo"))) { enabled = false; } else if (command && command != "print") { if ($.browser.msie && command == "hilitecolor") command = "backcolor"; // IE does not support inserthtml, so it's always enabled if (!$.browser.msie || command != "inserthtml") { try { enabled = queryObj.queryCommandEnabled(command); } catch (err) { enabled = false; } } } */ try { var state = queryObj.queryCommandState(command); } catch (err) { state = false; } if (command == 'formatblock') { var value = '<'+queryObj.queryCommandValue(command).toLowerCase()+'>'; var styles = this.options['styles']; $elem.find('em').html(styles[0][0]); for (var o in styles) { var o = styles[o]; if (o[1] == value) $elem.find('em').html(o[0]); } } if (state) { $elem.addClass($.editor.classPrefix+'active'); } else { $elem.removeClass($.editor.classPrefix+'active'); } // Enable or disable the button if (enabled) { // $elem.parent().removeClass('disabled').removeAttr("disabled"); $elem.removeClass($.editor.classPrefix+'disabled').removeAttr("disabled"); } else { // $elem.parent().addClass('disabled').attr("disabled", "on"); $elem.addClass($.editor.classPrefix+'disabled').attr("disabled", "on"); } }, this)); }, // restoreRange - restores the current ie selection restoreRange : function() { if ($.browser.msie && this.range) this.range[0].select(); }, showMessage : function (message, button) { var popup = this.createPopup("msg", this.options, MSG_CLASS); popup.innerHTML = message; this.showPopup(popup, button); }, // showPopup - shows a popup showPopup : function (popup, button) { var offset, left, top, $popup = $(popup); // Determine the popup location if (button) { var $button = $(button); offset = $button.offset(); left = --offset.left - 3; top = offset.top + $button.height() + 2; } else { offset = this.toolbar.offset(); left = Math.floor((this.toolbar.width() - $popup.width()) / 2) + offset.left; top = offset.top + this.toolbar.height() - 2; } // Position and show the popup this.hidePopups(); $popup.css({left: left, top: top}).show(); // Assign the popup button and click event handler if (button) { $.data(popup, "button", button); $popup.bind('click', {popup: popup}, $.proxy(this.popupClick, this)); } // Focus the first input element if any setTimeout(function() { $popup.find(":text,textarea").eq(0).focus().select(); }, 100); }, // updateFrame - updates the iframe with the textarea contents updateFrame : function() { var code = this.area.val(), $body = $(this.doc.body); // Convert the textarea source code into iframe html var html = code; // Prevent script injection attacks by html encoding script tags html = html.replace(/<(?=\/?script)/ig, "<"); // if (html == '' && !$.browser.mozilla) html = ''; // Update the iframe and trigger the change event if (html != $body.html()) { $body.html(html); $(this).triggerHandler("change"); } setTimeout($.proxy(function() { var d = this.doc; var h = Math.max(Math.max(d.body.scrollHeight, d.documentElement.scrollHeight), Math.max(d.body.offsetHeight, d.documentElement.offsetHeight), Math.max(d.body.clientHeight, d.documentElement.clientHeight)); //if (h < 200) h = 200; $(this.frame).height(h); }, this) , 1000); }, showImageProp: function(e) { this.hideImageProp(); if (this.imagePropForm == null) { this.imagePropForm = $(''); if ($.browser.msie) this.imagePropForm[0].contentEditable = false; this.imagePropForm.load(this.area.data('imageprop'), $.proxy(function() { $(this.imagePropForm).find('*[data-dismiss="modal"]').click($.proxy(function() { this.hideImageProp(); }, this)); f = e.css('float'); var align = (f == 'left' || f == 'right')?f:'center'; this.imagePropForm.find('*[data-align="'+align+'"]').addClass('builder-active'); this.imagePropForm.find('*[data-align]').click(function() { var o = $(this); o.parent().find('*[data-align]').removeClass('builder-active'); o.addClass('builder-active'); switch (o.data('align')) { case 'left': e.css({'float':'left'}); break; case 'right': e.css({'float':'right'}); break; case 'center': e.css({'float':''}); break; } }); this.imagePropForm.find("[data-method='change-image']").click($.proxy(function() { this.showImagesForm(e); }, this)); this.imagePropForm.show(); }, this)); } else { this.imagePropForm.show(); } o = e.offset(); this.imagePropForm.appendTo($(this.doc.body)); var left = o.left + e.width() + 10; var top = o.top; offsetLeft = $(this.doc.body).width() - left - this.imagePropForm.width() - 10; if (offsetLeft < 0) { left += offsetLeft; top += 10; } this.imagePropForm.css({left:left, top:top}); }, hideImageProp: function() { if (this.imagePropForm != null) { this.imagePropForm.hide(); //this.imagePropForm = null; } }, showImagesForm: function(e) { var form = $('').appendTo($(document.body)).modal('show').load(this.area.data('imagesform'), $.proxy(function() { $ok = form.find('.builder-modal-footer .builder-btn-primary'); if (e != undefined) form.find('.builder-files .builder-img img[data-src="'+e.attr('src').replace('http://park.onreg.ru', '')+'"]').parent().addClass('builder-active'); $ok[0].disabled = (form.find('.builder-files .builder-active').length == 0)?'on':''; form.find('.builder-fileinput-button').fileuploadImage(); $ok.click($.proxy(function() { var src = form.find('.builder-files .builder-active img').data('src'); if (e != undefined) { e.attr('src', src); } else { this.insertHTML(''); } form.modal('hide'); }, this)); }, this)).on('hidden', function () { $(this).remove(); }); }, applyImageProp: function() { var d = $('#idImagesProp'); o = this.currentElement.parent(); t = d.find('#idImageAltProp').val(); child = o.contents(); if (this.currentElement[0].parentNode.tagName.toLowerCase() == 'div') { if (child.length == 2) { child[1].textContent = t; } else { o.append(document.createTextNode(t)); } this.currentElement[0].parentNode.className = d.find('#idImagePositionProp').val(); } else { $(this.currentElement[0]).attr('align', d.find('#idImagePositionProp').val()); } d.modal('hide'); }, showImagePropModal: function(e) { var e = $(e); this.currentElement = e; function _a(d, e) { d.find('#idImageAltProp').val(e[0].parentNode.innerText); d.find('#idImagePositionProp').val(e[0].parentNode.className); } if (!$('#idImagesProp').length) { var d = $('').appendTo(document.body); d.modal().html(''); $.ajax({ url:"postimagesprop.html", method:'get', success: function(t) { d.html(t); _a(d, e); } }); } else { var d = $('#idImagesProp'); d.modal(); _a(d, e); } }, // updateTextArea - updates the textarea with the iframe contents updateTextArea : function() { var html = $(this.doc.body).html(); // Convert the iframe html into textarea source code var code = html; // Update the textarea and trigger the change event if (code != this.area.val()) { this.area.val(code); $(this).triggerHandler("change"); } }, selectedText : function () { this.restoreRange(); if ($.browser.msie) return this.getRange().text; return this.getSelection().toString(); } } /* $.editor.buttons['spell'] = {stripIndex:0, name:'spell', title:'Spell', enabled: true, click: function(e, d) { $(d.editor.doc.body).spellchecker({ url:'/groups/spellcheck.html', innerDocument: true, addToDictionary: false }).spellchecker("check"); return false; } } */ $(function() { if ($("#idTextarea").length) { $("#idTextarea").val($('#wisiwigBody').html()); editor = $("#idTextarea").removeAttr('id').editor({css:$(document.head || document.getElementsByTagName("head")[0] || document.documentElement).find('link[rel="stylesheet"]').map(function() { return $(this).attr('href'); }).toArray(), bodyStyle:''}); editor = editor[0]; var t = editor.toolbar; var y = t.position().top; var d = $(document); t = t[0]; $(window).scroll(function () { t.className = (d.scrollTop() > y)?($.editor.classPrefix+'fixed '+$.editor.classPrefix+'btn-toolbar '+$.editor.classPrefix+'toolbar-editor'):($.editor.classPrefix+'btn-toolbar '+$.editor.classPrefix+'toolbar-editor'); }); $('form').bind('submit', function() { editor.hideImageProp(); editor.updateTextArea(); }); } });