38f959890d
create new build tools.
1 line
17 KiB
JavaScript
1 line
17 KiB
JavaScript
function scrollableEditor(h,g){var e=h.css(),t=h.width(),l=null,m=[],u=[],p=[],n={display:"block",position:"absolute",left:"-99999px",top:"-99999px"},r=$("<div></div>").appendTo(document.body),c=!1;for(var o in e)o.match(/^(direction|font-family|font-size|font-style|font-weight|letter-spacing|line-height|text-align|vertical-align|white-space|word-wrap|word-break|word-spacing)$/i)&&(n[o]=e[o]);function a(e){var t=h.val().split("\n"),n=0;r.width(h.width()),u=[];for(var o=0;o<t.length;o++)r.text(t[o]),n+=r.height(),u.push(n);r.html(""),function(e){var a=0;p=[],m=[],$(".line",g).each(function(){var e=$(this),t=e.data("start"),n=e.data("end"),o=e.data("start-original"),r=e.position().top+g.scrollTop();p.push([t,n,r,this]),void 0!==o?(m.push([t,o-1,u[o-1],a,0]),m.push([o,n,u[n],u[o-1],1])):m.push([t,n,u[n],a,1]),a=u[n]});var t=f();e?null!==t&&i(t):i()}(e)}function i(e){var t=h.height(),n=(h.innerHeight()-t)/2,o=h.scrollTop()-n,r=g.scrollTop(),a=0,i=0,l=null;if(void 0===e){for(var c=0;c<m.length;c++)if(o<=(l=m[c])[2]){a=(o-l[3])*l[4]/(l[2]-l[3]);break}if(l)for(var s=0;s<p.length;s++){f=p[s];if(l[0]>=f[0]&&l[1]<=f[1]){var u=p[s+1]?p[s+1][2]:g.get(0).scrollHeight,i=(0==c?0:f[2])+(u-f[2])*a;g.scrollTop(i);break}}}else for(var f,d=0;d<p.length;d++){if(e>=(f=p[d])[0]&&e<=f[1]){(d==p.length-1||f[2]<r||p[d+1][2]>r+g.height())&&g.scrollTop(f[2]);break}}}function s(e){var t=$(e),n=t.parent();return 0<t.length&&t.prop("tagName").match(/^(hr|tr)$/i)?t:0<n.length&&"div"==n.prop("tagName").toLowerCase()?t.next():n}function f(){var e=h.val(),t=h.getSelection().start,n=0,o=0,r=null;if(!c)return r;for(;0<=(n=e.indexOf("\n",n))&&n<t;)o++,n+=1;for(var a=0;a<p.length;a++){var i=p[a];if(o>=i[0]&&o<=i[1]){s(l).removeClass("focus"),s(i[3]).addClass("focus"),l=i[3],r=o;break}}return r}return r.css(n),r.css("min-height",n["line-height"]),setInterval(function(){h.width()!=t&&(t=h.width(),h.trigger("resize"))},150),h.on("touch keypress click",f),h.on("focus",function(){c=!0}).on("blur",function(){c=!1,s(l).removeClass("focus")}),h.on("resize",a),h.on("DOMMouseScroll mousewheel touchmove",function(){i()}),g.on("DOMMouseScroll mousewheel touchmove",function(){!function(){var e,t=h.height(),n=(h.innerHeight()-t)/2,o=g.scrollTop(),r=!1;if(!(p.length<=0)){for(var a=0;a<p.length;a++){if(o<p[a][2]){r=!0;break}}var i=0<(e=r?p[0<a?a-1:0]:p[p.length-1])[0]?u[e[0]-1]:0,l=u[e[1]],c=r?p[0<a?a:1][2]:g.get(0).scrollHeight,s=(o-e[2])/(c-e[2]);h.scrollTop(i+(l-i)*s+n)}}()}),a}window.Typecho={insertFileToEditor:function(e,t,n){},uploadFile:function(e){},editorResize:function(e,t){$("#"+e).resizeable({minHeight:100,afterResize:function(e){$.post(t,{size:e})}})},uploadComplete:function(e){}},function(u){u.fn.dropdownMenu=function(n){this.each(function(){var e=this,t=u.extend({menuEl:null,btnEl:null},n);u(t.btnEl,e).click(function(){return u(this).toggleClass("active"),u(t.menuEl,e).toggle(),!1})})},u.fn.resizeable=function(e){var s=u.extend({minHeight:100,afterResize:null},e);return this.each(function(){var o,e=u('<span class="resize"><i></i></span>').insertAfter(this),r=0,a=s.minHeight,n=this;function i(e){var t=c(e).y,n=o+t;return t<=r&&(n-=5),r=t,n=Math.max(a,n),textarea.height(n+"px"),n<a&&l(),!1}function l(e){var t=textarea.outerHeight();u(document).unbind("mousemove",i).unbind("mouseup",l),textarea.css("opacity",1),textarea.focus(),textarea=null,o=null,r=0,s.afterResize&&s.afterResize.call(n,t)}function c(e){return{x:e.clientX+document.documentElement.scrollLeft,y:e.clientY+document.documentElement.scrollTop}}e.bind("mousedown",{el:this},function(e){return textarea=u(e.data.el),textarea.blur(),r=c(e).y,o=textarea.height()-r,textarea.css("opacity",.25),u(document).mousemove(i).mouseup(l),!1})})},u.fn.tableSelectable=function(e){var n=this,o=u.extend({checkEl:null,rowEl:null,selectAllEl:null,actionEl:null},e);function r(e){var e=u(e),t=u(o.checkEl,e),n=t.prop("checked");t.length&&(t.prop("checked",!n),n?e.removeClass("checked"):e.addClass("checked"))}u(o.rowEl,this).each(function(){u(o.checkEl,this).click(function(e){r(u(this).parents(o.rowEl))})}).click(function(e){var t=u(e.toElement?e.toElement:e.target),n=t.prop("tagName").toLowerCase();0<=u.inArray(n,["input","textarea","a","button","i"])&&"checkbox"!=t.attr("type")?e.stopPropagation():r(this)}),u(o.selectAllEl).click(function(){u(this).prop("checked")?u(o.rowEl,n).each(function(){var e=u(this);0<u(o.checkEl,this).prop("checked",!0).length&&e.addClass("checked")}):u(o.rowEl,n).each(function(){var e=u(this);0<u(o.checkEl,this).prop("checked",!1).length&&e.removeClass("checked")})}),u(o.actionEl).click(function(){var e=u(this),t=e.attr("lang");return t&&!confirm(t)||n.parents("form").attr("action",e.attr("href")).submit(),!1})}}($),function(c){c.tableDnD={currentTable:null,dragObject:null,mouseOffset:null,oldY:0,build:function(i){return i=i||{},this.each(function(){var e,t,n,o,r,a;this.tableDnDConfig={onDragStyle:i.onDragStyle,onDropStyle:i.onDropStyle,onDragClass:i.onDragClass?i.onDragClass:"tDnD_whileDrag",onDrop:i.onDrop,onDragStart:i.onDragStart,scrollAmount:i.scrollAmount?i.scrollAmount:5},c.tableDnD.makeDraggable(this),0==$("tfoot",this).length&&0<$("thead",this).length&&(e=$("thead",this),t=$("th",e).length,n=$('<tfoot><tr><td style="padding:0;height:0;line-height:0;border:none" colspan="'+t+'"></td></tr></tfoot>').insertAfter(e),"tfoot"!=(o=$("tr:last",this)).parent().prop("tagName").toLowerCase()&&(a=(r=$("td",o)).height(),r.height(a-n.outerHeight())))}),c(document).bind("mousemove",c.tableDnD.mousemove).bind("mouseup",c.tableDnD.mouseup),this},makeDraggable:function(t){for(var e=t.rows,n=t.tableDnDConfig,o=0;o<e.length;o++){$(e[o]).hasClass("nodrag")||c(e[o]).mousedown(function(e){if("TD"==e.target.tagName)return c.tableDnD.dragObject=this,c.tableDnD.currentTable=t,c.tableDnD.mouseOffset=c.tableDnD.getMouseOffset(this,e),n.onDragStart&&n.onDragStart(t,this),!1}).css("cursor","move")}},mouseCoords:function(e){return e.pageX||e.pageY?{x:e.pageX,y:e.pageY}:{x:e.clientX+document.body.scrollLeft-document.body.clientLeft,y:e.clientY+document.body.scrollTop-document.body.clientTop}},getMouseOffset:function(e,t){t=t||window.event;var n=this.getPosition(e),o=this.mouseCoords(t);return{x:o.x-n.x,y:o.y-n.y}},getPosition:function(e){var t=0,n=0;for(0==e.offsetHeight&&(e=e.firstChild);e.offsetParent;)t+=e.offsetLeft,n+=e.offsetTop,e=e.offsetParent;return{x:t+=e.offsetLeft,y:n+=e.offsetTop}},mousemove:function(e){if(null!=c.tableDnD.dragObject){var t,n,o=c(c.tableDnD.dragObject),r=c.tableDnD.currentTable.tableDnDConfig,a=c.tableDnD.mouseCoords(e),i=a.y-c.tableDnD.mouseOffset.y,l=window.pageYOffset;return document.all&&(void 0!==document.compatMode&&"BackCompat"!=document.compatMode?l=document.documentElement.scrollTop:void 0!==document.body&&(l=document.body.scrollTop)),a.y-l<r.scrollAmount?window.scrollBy(0,-r.scrollAmount):(window.innerHeight?window.innerHeight:document.documentElement.clientHeight?document.documentElement.clientHeight:document.body.clientHeight)-(a.y-l)<r.scrollAmount&&window.scrollBy(0,r.scrollAmount),i!=c.tableDnD.oldY&&(t=i>c.tableDnD.oldY,c.tableDnD.oldY=i,r.onDragClass?o.addClass(r.onDragClass):o.css(r.onDragStyle),(n=c.tableDnD.findDropTargetRow(o,i))&&(t&&c.tableDnD.dragObject!=n?c.tableDnD.dragObject.parentNode.insertBefore(c.tableDnD.dragObject,n.nextSibling):t||c.tableDnD.dragObject==n||c.tableDnD.dragObject.parentNode.insertBefore(c.tableDnD.dragObject,n))),!1}},findDropTargetRow:function(e,t){for(var n=c.tableDnD.currentTable.rows,o=0;o<n.length;o++){var r=n[o],a=this.getPosition(r).y,i=parseInt(r.offsetHeight)/2;if(0==r.offsetHeight&&(a=this.getPosition(r.firstChild).y,i=parseInt(r.firstChild.offsetHeight)/2),a-i<t&&t<a+i){if(r==e)return null;var l=c.tableDnD.currentTable.tableDnDConfig;return l.onAllowDrop?l.onAllowDrop(e,r)?r:null:$(r).hasClass("nodrop")?null:r}}return null},mouseup:function(e){var t,n;c.tableDnD.currentTable&&c.tableDnD.dragObject&&(t=c.tableDnD.dragObject,(n=c.tableDnD.currentTable.tableDnDConfig).onDragClass?c(t).removeClass(n.onDragClass):c(t).css(n.onDropStyle),c.tableDnD.dragObject=null,n.onDrop&&n.onDrop(c.tableDnD.currentTable,t),c.tableDnD.currentTable=null)},serialize:function(){if(c.tableDnD.currentTable){for(var e="",t=c.tableDnD.currentTable.id,n=c.tableDnD.currentTable.rows,o=0;o<n.length;o++)0<e.length&&(e+="&"),e+=t+"[]="+n[o].id;return e}return"Error: No Table id set, you need to set an id on your table and every row"}},c.fn.extend({tableDnD:c.tableDnD.build})}($),function(v){var o,e,t,r=(e=document.createElement("input"),t="onpaste",e.setAttribute(t,""),("function"==typeof e[t]?"paste":"input")+".mask"),n=navigator.userAgent,D=/iphone/i.test(n),y=/android/i.test(n);v.mask={definitions:{9:"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},dataName:"rawMaskFn",placeholder:"_"},v.fn.extend({caret:function(e,t){var n;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof e?(t="number"==typeof t?t:e,this.each(function(){this.setSelectionRange?this.setSelectionRange(e,t):this.createTextRange&&((n=this.createTextRange()).collapse(!0),n.moveEnd("character",t),n.moveStart("character",e),n.select())})):(this[0].setSelectionRange?(e=this[0].selectionStart,t=this[0].selectionEnd):document.selection&&document.selection.createRange&&(n=document.selection.createRange(),e=0-n.duplicate().moveStart("character",-1e5),t=e+n.text.length),{begin:e,end:t})},unmask:function(){return this.trigger("unmask")},mask:function(t,h){var n,g,m,p,b;return!t&&0<this.length?v(this[0]).data(v.mask.dataName)():(h=v.extend({placeholder:v.mask.placeholder,completed:null},h),n=v.mask.definitions,g=[],m=b=t.length,p=null,v.each(t.split(""),function(e,t){"?"==t?(b--,m=e):n[t]?(g.push(new RegExp(n[t])),null===p&&(p=g.length-1)):g.push(null)}),this.trigger("unmask").each(function(){var i=v(this),l=v.map(t.split(""),function(e,t){if("?"!=e)return n[e]?h.placeholder:e}),a=i.val();function c(e){for(;++e<b&&!g[e];);return e}function s(e,t){var n,o;if(!(e<0)){for(n=e,o=c(t);n<b;n++)if(g[n]){if(!(o<b&&g[n].test(l[o])))break;l[n]=l[o],l[o]=h.placeholder,o=c(o)}f(),i.caret(Math.max(p,e))}}function u(e,t){for(var n=e;n<t&&n<b;n++)g[n]&&(l[n]=h.placeholder)}function f(){i.val(l.join(""))}function d(e){var t,n=i.val(),o=-1,r=0;for(pos=0;r<b;r++)if(g[r]){for(l[r]=h.placeholder;pos++<n.length;)if(t=n.charAt(pos-1),g[r].test(t)){l[r]=t,o=r;break}if(pos>n.length)break}else l[r]===n.charAt(pos)&&r!==m&&(pos++,o=r);return e?f():o+1<m?(i.val(""),u(0,b)):(f(),i.val(i.val().substring(0,o+1))),m?r:p}i.data(v.mask.dataName,function(){return v.map(l,function(e,t){return g[t]&&e!=h.placeholder?e:null}).join("")}),i.attr("readonly")||i.one("unmask",function(){i.unbind(".mask").removeData(v.mask.dataName)}).bind("focus.mask",function(){var e;clearTimeout(o),a=i.val(),e=d(),o=setTimeout(function(){f(),e==t.length?i.caret(0,e):i.caret(e)},10)}).bind("blur.mask",function(){d(),i.val()!=a&&i.change()}).bind("keydown.mask",function(e){var t,n,o,r=e.which;8===r||46===r||D&&127===r?(n=(t=i.caret()).begin,(o=t.end)-n==0&&(n=46!==r?function(e){for(;0<=--e&&!g[e];);return e}(n):o=c(n-1),o=46===r?c(o):o),u(n,o),s(n,o-1),e.preventDefault()):27==r&&(i.val(a),i.caret(0,d()),e.preventDefault())}).bind("keypress.mask",function(e){var t,n,o,r=e.which,a=i.caret();e.ctrlKey||e.altKey||e.metaKey||r<32||r&&(a.end-a.begin!=0&&(u(a.begin,a.end),s(a.begin,a.end-1)),(t=c(a.begin-1))<b&&(n=String.fromCharCode(r),g[t].test(n)&&(function(e){for(var t,n,o=e,r=h.placeholder;o<b;o++)if(g[o]){if(t=c(o),n=l[o],l[o]=r,!(t<b&&g[t].test(n)))break;r=n}}(t),l[t]=n,f(),o=c(t),y?setTimeout(v.proxy(v.fn.caret,i,o),0):i.caret(o),h.completed&&b<=o&&h.completed.call(i))),e.preventDefault())}).bind(r,function(){setTimeout(function(){var e=d(!0);i.caret(e),h.completed&&e==i.val().length&&h.completed.call(i)},0)}),d()}))}})}(jQuery),jQuery.fn.extend({getSelection:function(){var o=this.get(0);return o?(("selectionStart"in o?function(){var e=o.selectionEnd-o.selectionStart;return{start:o.selectionStart,end:o.selectionEnd,length:e,text:o.value.substr(o.selectionStart,e)}}:window.getSelection()&&function(){var e=window.getSelection().getRangeAt(0);return{start:e.startOffset,end:e.endOffset,length:e.endOffset-e.startOffset,text:e.toString()}})||document.selection&&function(){o.focus();var e=document.selection.createRange();if(null===e)return{start:0,end:o.value.length,length:0};var t=o.createTextRange(),n=t.duplicate();return t.moveToBookmark(e.getBookmark()),n.setEndPoint("EndToStart",t),{start:n.text.length,end:n.text.length+e.text.length,length:e.text.length,text:e.text}}||function(){return null})():null},setSelection:function(e,t){var n,o=this.get(0);o&&(o.setSelectionRange?(o.focus(),o.setSelectionRange(e,t)):o.createTextRange&&((n=o.createTextRange()).collapse(!0),n.moveEnd("character",t),n.moveStart("character",e),n.select()))},replaceSelection:function(){var e=this.get(0);if(!e)return null;var t=arguments[0]||"";return(("selectionStart"in e?function(){return e.value=e.value.substr(0,e.selectionStart)+t+e.value.substr(e.selectionEnd,e.value.length),this}:document.selection&&function(){return e.focus(),document.selection.createRange().text=t,this})||function(){return e.value+=t,jQuery(e)})()}}),jQuery.cookie=function(e,t,n){var o,r;if(1<arguments.length&&"[object Object]"!==String(t))return n=jQuery.extend({},n),null==t&&(n.expires=-1),"number"==typeof n.expires&&(o=n.expires,(r=n.expires=new Date).setDate(r.getDate()+o)),t=String(t),document.cookie=[encodeURIComponent(e),"=",n.raw?t:encodeURIComponent(t),n.expires?"; expires="+n.expires.toUTCString():"",n.path?"; path="+n.path:"",n.domain?"; domain="+n.domain:"",n.secure?"; secure":""].join("");var a,i=(n=t||{}).raw?function(e){return e}:decodeURIComponent;return(a=new RegExp("(?:^|; )"+encodeURIComponent(e)+"=([^;]*)").exec(document.cookie))?i(a[1]):null},function(l){var p=l.scrollTo=function(e,t,n){l(window).scrollTo(e,t,n)};function e(e){return"object"==typeof e?e:{top:e,left:e}}p.defaults={axis:"xy",duration:1.3<=parseFloat(l.fn.jquery)?0:1,limit:!0},p.window=function(e){return l(window)._scrollable()},l.fn._scrollable=function(){return this.map(function(){var e=this;if(!(!e.nodeName||-1!=l.inArray(e.nodeName.toLowerCase(),["iframe","#document","html","body"])))return e;var t=(e.contentWindow||e).document||e.ownerDocument||e;return/webkit/i.test(navigator.userAgent)||"BackCompat"==t.compatMode?t.body:t.documentElement})},l.fn.scrollTo=function(t,n,m){return"object"==typeof n&&(m=n,n=0),"function"==typeof m&&(m={onAfter:m}),"max"==t&&(t=9e9),m=l.extend({},p.defaults,m),n=n||m.duration,m.queue=m.queue&&1<m.axis.length,m.queue&&(n/=2),m.offset=e(m.offset),m.over=e(m.over),this._scrollable().each(function(){if(null!=t){var c,s=this,u=l(s),f=t,d={},h=u.is("html,body");switch(typeof f){case"number":case"string":if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=e(f);break}if(!(f=l(f,this)).length)return;case"object":(f.is||f.style)&&(c=(f=l(f)).offset())}l.each(m.axis.split(""),function(e,t){var n,o="x"==t?"Left":"Top",r=o.toLowerCase(),a="scroll"+o,i=s[a],l=p.max(s,t);c?(d[a]=c[r]+(h?0:i-u.offset()[r]),m.margin&&(d[a]-=parseInt(f.css("margin"+o))||0,d[a]-=parseInt(f.css("border"+o+"Width"))||0),d[a]+=m.offset[r]||0,m.over[r]&&(d[a]+=f["x"==t?"width":"height"]()*m.over[r])):(n=f[r],d[a]=n.slice&&"%"==n.slice(-1)?parseFloat(n)/100*l:n),m.limit&&/^\d+$/.test(d[a])&&(d[a]=d[a]<=0?0:Math.min(d[a],l)),!e&&m.queue&&(i!=d[a]&&g(m.onAfterFirst),delete d[a])}),g(m.onAfter)}function g(e){u.animate(d,n,m.easing,e&&function(){e.call(this,t,m)})}}).end()},p.max=function(e,t){var n="x"==t?"Width":"Height",o="scroll"+n;if(!l(e).is("html,body"))return e[o]-l(e)[n.toLowerCase()]();var r="client"+n,a=e.ownerDocument.documentElement,i=e.ownerDocument.body;return Math.max(a[o],i[o])-Math.min(a[r],i[r])}}(jQuery),jQuery.fn.css2=jQuery.fn.css,jQuery.fn.css=function(){if(arguments.length)return jQuery.fn.css2.apply(this,arguments);for(var e=["font-family","font-size","font-weight","font-style","color","box-sizing","text-transform","text-decoration","letter-spacing","box-shadow","line-height","text-align","vertical-align","direction","background-color","background-image","background-repeat","background-position","background-attachment","opacity","width","height","top","right","bottom","left","margin-top","margin-right","margin-bottom","margin-left","padding-top","padding-right","padding-bottom","padding-left","border-top-width","border-right-width","border-bottom-width","border-left-width","border-top-color","border-right-color","border-bottom-color","border-left-color","border-top-style","border-right-style","border-bottom-style","border-left-style","position","display","visibility","z-index","overflow-x","overflow-y","white-space","clip","float","clear","cursor","list-style-image","list-style-position","list-style-type","marker-offset","word-wrap","word-break","word-spacing"],t=e.length,n={},o=0;o<t;o++)n[e[o]]=jQuery.fn.css2.call(this,e[o]);return n}; |