aissel-astro/public/assets/js/editor.js

1 line
41 KiB
JavaScript
Raw Normal View History

2024-11-07 04:53:33 +00:00
(function($){var editorObj;var methods={saveSelection:function(){$(this).data("editor").focus();if(window.getSelection){sel=window.getSelection();if(sel.getRangeAt&&sel.rangeCount){$(this).data("currentRange",sel.getRangeAt(0))}}else if(document.selection&&document.selection.createRange){$(this).data("currentRange",document.selection.createRange())}else $(this).data("currentRange",null)},restoreSelection:function(text,mode){var node;typeof text!=="undefined"?text:false;typeof mode!=="undefined"?mode:"";var range=$(this).data("currentRange");if(range){if(window.getSelection){if(text){range.deleteContents();if(mode=="html"){var el=document.createElement("div");el.innerHTML=text;var frag=document.createDocumentFragment(),node,lastNode;while(node=el.firstChild){lastNode=frag.appendChild(node)}range.insertNode(frag)}else range.insertNode(document.createTextNode(text))}sel=window.getSelection();sel.removeAllRanges();sel.addRange(range)}else if(document.selection&&range.select){range.select();if(text){if(mode=="html")range.pasteHTML(text);else range.text=text}}}},restoreIESelection:function(){var range=$(this).data("currentRange");if(range){if(window.getSelection){sel=window.getSelection();sel.removeAllRanges();sel.addRange(range)}else if(document.selection&&range.select){range.select()}}},insertTextAtSelection:function(text,mode){var sel,range,node;typeof mode!=="undefined"?mode:"";if(window.getSelection){sel=window.getSelection();if(sel.getRangeAt&&sel.rangeCount){range=sel.getRangeAt(0);range.deleteContents();var textNode=document.createTextNode(text);if(mode=="html"){var el=document.createElement("div");el.innerHTML=text;var frag=document.createDocumentFragment(),node,lastNode;while(node=el.firstChild){lastNode=frag.appendChild(node)}range.insertNode(frag)}else{range.insertNode(textNode);range.selectNode(textNode)}sel.removeAllRanges();range=range.cloneRange();range.collapse(false);sel.addRange(range)}}else if(document.selection&&document.selection.createRange){range=document.selection.createRange();range.pasteHTML(text);range.select()}},imageWidget:function(){var row=$("<div/>",{"class":"row"}).append($("<div/>",{id:"imgErrMsg"}));var container=$("<div/>",{"class":"tabbable tabs-left"});var navTabs=$("<ul/>",{"class":"nav nav-tabs"}).append($("<li/>",{"class":"active"}).append($("<a/>",{href:"#uploadImageBar","data-toggle":"tab"}).html("From Computer"))).append($("<li/>").append($("<a/>",{href:"#imageFromLinkBar","data-toggle":"tab"}).html("From URL")));var tabContent=$("<div/>",{"class":"tab-content"});var uploadImageBar=$("<div/>",{id:"uploadImageBar","class":"tab-pane active"});handleFileSelect=function(evt){var files=evt.target.files;var output=[];for(var i=0,f;f=files[i];i++){if(!f.type.match("image.*")||!f.name.match(/(?:gif|jpg|png|jpeg)$/)){methods.showMessage.apply(this,["imgErrMsg","Invalid file type"]);continue}var reader=new FileReader;reader.onload=function(imageFile){return function(e){var li=$("<li/>",{"class":"col-xs-12 col-sm-6 col-md-3 col-lg-3"});var a=$("<a/>",{href:"javascript:void(0)","class":"thumbnail"});var image=$("<img/>",{src:e.target.result,title:escape(imageFile.name)}).appendTo(a).click(function(){$("#imageList").data("current",$(this).attr("src"))});li.append(a).appendTo($("#imageList"))}}(f);reader.readAsDataURL(f)}};var chooseFromLocal=$("<input/>",{type:"file","class":"inline-form-control",multiple:"multiple"});chooseFromLocal.on("change",handleFileSelect);uploadImageBar.append(chooseFromLocal);var imageFromLinkBar=$("<div/>",{id:"imageFromLinkBar","class":"tab-pane"});var getImageURL=$("<div/>",{"class":"input-group"});var imageURL=$("<input/>",{type:"url","class":"form-control",id:"imageURL",placeholder:"Enter URL"}).appendTo(getImageURL);var getURL=$("<button/>",{"class":"btn btn-success",type:"button"}).html("Go!").click(function(){var url=$("#imageURL").val();if(url==""){methods.showMessage.apply(this,["imgErrMsg","Please enter image url"]);return false}var li=$("<li/>",{"class":"span6 col-xs-12 col-sm-6 col-md-3 col-lg-3"});var a=$("<a/>",{href:"javascript:void(0)","class":"thumbna