mirror of
https://github.com/discourse/discourse.git
synced 2025-09-05 08:59:27 +08:00
Add ES6 support to more files
This commit is contained in:
parent
766903c430
commit
e2e3e7c0e0
78 changed files with 419 additions and 387 deletions
136
vendor/assets/javascripts/caret_position.js
vendored
Normal file
136
vendor/assets/javascripts/caret_position.js
vendored
Normal file
|
@ -0,0 +1,136 @@
|
|||
// http://stackoverflow.com/questions/263743/how-to-get-caret-position-in-textarea
|
||||
var clone, getCaret;
|
||||
getCaret = function(el) {
|
||||
var r, rc, re;
|
||||
if (el.selectionStart) {
|
||||
return el.selectionStart;
|
||||
} else if (document.selection) {
|
||||
el.focus();
|
||||
r = document.selection.createRange();
|
||||
if (!r) return 0;
|
||||
re = el.createTextRange();
|
||||
rc = re.duplicate();
|
||||
re.moveToBookmark(r.getBookmark());
|
||||
rc.setEndPoint("EndToStart", re);
|
||||
return rc.text.length;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
clone = null;
|
||||
|
||||
$.fn.caret = function(){
|
||||
return getCaret(this[0]);
|
||||
};
|
||||
|
||||
/**
|
||||
This is a jQuery plugin to retrieve the caret position in a textarea
|
||||
|
||||
@module $.fn.caretPosition
|
||||
**/
|
||||
$.fn.caretPosition = function(options) {
|
||||
var after, before, getStyles, guard, html, important, insertSpaceAfterBefore, letter, makeCursor, p, pPos, pos, span, styles, textarea, val;
|
||||
if (clone) {
|
||||
clone.remove();
|
||||
}
|
||||
span = $("#pos span");
|
||||
textarea = $(this);
|
||||
|
||||
getStyles = function(el) {
|
||||
if (el.currentStyle) {
|
||||
return el.currentStyle;
|
||||
} else {
|
||||
return document.defaultView.getComputedStyle(el, "");
|
||||
}
|
||||
};
|
||||
|
||||
styles = getStyles(textarea[0]);
|
||||
clone = $("<div><p></p></div>").appendTo("body");
|
||||
p = clone.find("p");
|
||||
clone.width(textarea.width());
|
||||
clone.height(textarea.height());
|
||||
|
||||
important = function(prop) {
|
||||
return styles.getPropertyValue(prop);
|
||||
};
|
||||
|
||||
clone.css({
|
||||
border: "1px solid black",
|
||||
padding: important("padding"),
|
||||
resize: important("resize"),
|
||||
"max-height": textarea.height() + "px",
|
||||
"overflow-y": "auto",
|
||||
"word-wrap": "break-word",
|
||||
position: "absolute",
|
||||
left: "-7000px"
|
||||
});
|
||||
|
||||
p.css({
|
||||
margin: 0,
|
||||
padding: 0,
|
||||
"word-wrap": "break-word",
|
||||
"letter-spacing": important("letter-spacing"),
|
||||
"font-family": important("font-family"),
|
||||
"font-size": important("font-size"),
|
||||
"line-height": important("line-height")
|
||||
});
|
||||
|
||||
pos = options && (options.pos || options.pos === 0) ? options.pos : getCaret(textarea[0]);
|
||||
val = textarea.val().replace("\r", "");
|
||||
if (options && options.key) {
|
||||
val = val.substring(0, pos) + options.key + val.substring(pos);
|
||||
}
|
||||
before = pos - 1;
|
||||
after = pos;
|
||||
insertSpaceAfterBefore = false;
|
||||
|
||||
// if before and after are \n insert a space
|
||||
if (val[before] === "\n" && val[after] === "\n") {
|
||||
insertSpaceAfterBefore = true;
|
||||
}
|
||||
|
||||
guard = function(v) {
|
||||
var buf;
|
||||
buf = v.replace(/</g, "<");
|
||||
buf = buf.replace(/>/g, ">");
|
||||
buf = buf.replace(/[ ]/g, "​ ​");
|
||||
return buf.replace(/\n/g, "<br />");
|
||||
};
|
||||
|
||||
makeCursor = function(pos, klass, color) {
|
||||
var l;
|
||||
l = val.substring(pos, pos + 1);
|
||||
if (l === "\n") return "<br>";
|
||||
return "<span class='" + klass + "' style='background-color:" + color + "; margin:0; padding: 0'>" + guard(l) + "</span>";
|
||||
};
|
||||
|
||||
html = "";
|
||||
if (before >= 0) {
|
||||
html += guard(val.substring(0, pos - 1)) + makeCursor(before, "before", "#d0ffff");
|
||||
if (insertSpaceAfterBefore) {
|
||||
html += makeCursor(0, "post-before", "#d0ffff");
|
||||
}
|
||||
}
|
||||
|
||||
if (after >= 0) {
|
||||
html += makeCursor(after, "after", "#ffd0ff");
|
||||
if (after - 1 < val.length) {
|
||||
html += guard(val.substring(after + 1));
|
||||
}
|
||||
}
|
||||
|
||||
p.html(html);
|
||||
clone.scrollTop(textarea.scrollTop());
|
||||
letter = p.find("span:first");
|
||||
pos = letter.offset();
|
||||
if (letter.hasClass("before")) {
|
||||
pos.left = pos.left + letter.width();
|
||||
}
|
||||
|
||||
pPos = p.offset();
|
||||
return {
|
||||
left: pos.left - pPos.left,
|
||||
top: (pos.top - pPos.top) - clone.scrollTop()
|
||||
};
|
||||
|
||||
};
|
91
vendor/assets/javascripts/div_resizer.js
vendored
Normal file
91
vendor/assets/javascripts/div_resizer.js
vendored
Normal file
|
@ -0,0 +1,91 @@
|
|||
/**
|
||||
This is a jQuery plugin to support resizing text areas.
|
||||
|
||||
Originally based off text area resizer by Ryan O'Dell : http://plugins.jquery.com/misc/textarea.js
|
||||
|
||||
@module $.fn.DivResizer
|
||||
**/
|
||||
|
||||
var div, endDrag, grip, lastMousePos, min, mousePosition, originalDivHeight, originalPos, performDrag, startDrag, wrappedEndDrag, wrappedPerformDrag;
|
||||
div = void 0;
|
||||
originalPos = void 0;
|
||||
originalDivHeight = void 0;
|
||||
lastMousePos = 0;
|
||||
min = 230;
|
||||
grip = void 0;
|
||||
wrappedEndDrag = void 0;
|
||||
wrappedPerformDrag = void 0;
|
||||
|
||||
startDrag = function(e, opts) {
|
||||
div = $(e.data.el);
|
||||
div.addClass('clear-transitions');
|
||||
div.blur();
|
||||
lastMousePos = mousePosition(e).y;
|
||||
originalPos = lastMousePos;
|
||||
originalDivHeight = div.height();
|
||||
wrappedPerformDrag = (function() {
|
||||
return function(e) {
|
||||
return performDrag(e, opts);
|
||||
};
|
||||
})();
|
||||
wrappedEndDrag = (function() {
|
||||
return function(e) {
|
||||
return endDrag(e, opts);
|
||||
};
|
||||
})();
|
||||
$(document).mousemove(wrappedPerformDrag).mouseup(wrappedEndDrag);
|
||||
return false;
|
||||
};
|
||||
|
||||
performDrag = function(e, opts) {
|
||||
var size, sizePx, thisMousePos;
|
||||
thisMousePos = mousePosition(e).y;
|
||||
size = originalDivHeight + (originalPos - thisMousePos);
|
||||
lastMousePos = thisMousePos;
|
||||
size = Math.min(size, $(window).height());
|
||||
size = Math.max(min, size);
|
||||
sizePx = size + "px";
|
||||
if (typeof opts.onDrag === "function") {
|
||||
opts.onDrag(sizePx);
|
||||
}
|
||||
div.height(sizePx);
|
||||
if (size < min) {
|
||||
endDrag(e, opts);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
endDrag = function(e, opts) {
|
||||
$(document).unbind("mousemove", wrappedPerformDrag).unbind("mouseup", wrappedEndDrag);
|
||||
div.removeClass('clear-transitions');
|
||||
div.focus();
|
||||
if (typeof opts.resize === "function") {
|
||||
opts.resize();
|
||||
}
|
||||
div = null;
|
||||
};
|
||||
|
||||
mousePosition = function(e) {
|
||||
return {
|
||||
x: e.clientX + document.documentElement.scrollLeft,
|
||||
y: e.clientY + document.documentElement.scrollTop
|
||||
};
|
||||
};
|
||||
|
||||
$.fn.DivResizer = function(opts) {
|
||||
return this.each(function() {
|
||||
var grippie, start, staticOffset;
|
||||
div = $(this);
|
||||
if (div.hasClass("processed")) return;
|
||||
div.addClass("processed");
|
||||
staticOffset = null;
|
||||
start = function() {
|
||||
return function(e) {
|
||||
return startDrag(e, opts);
|
||||
};
|
||||
};
|
||||
grippie = div.prepend("<div class='grippie'></div>").find('.grippie').bind("mousedown", {
|
||||
el: this
|
||||
}, start());
|
||||
});
|
||||
};
|
176
vendor/assets/javascripts/probes.js
vendored
Normal file
176
vendor/assets/javascripts/probes.js
vendored
Normal file
|
@ -0,0 +1,176 @@
|
|||
/*
|
||||
* JavaScript probing framework by Sam Saffron
|
||||
* MIT license
|
||||
*
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* someFunction = window.probes.measure(someFunction, {
|
||||
* name: "somename" // or function(args) { return "name"; },
|
||||
* before: function(data, owner, args) {
|
||||
* // if owner is true, we are not in a recursive function call.
|
||||
* //
|
||||
* // data contains the bucker of data already measuer
|
||||
* // data.count >= 0
|
||||
* // data.time is the total time measured till now
|
||||
* //
|
||||
* // arguments contains the original arguments sent to the function
|
||||
* },
|
||||
* after: function(data, owner, args) {
|
||||
* // same format as before
|
||||
* }
|
||||
* });
|
||||
*
|
||||
*
|
||||
* // minimal
|
||||
* someFunction = window.probes.measure(someFunction, "someFunction");
|
||||
*
|
||||
* */
|
||||
(function(){
|
||||
var measure, clear;
|
||||
|
||||
clear = function() {
|
||||
window.probes = {
|
||||
clear: clear,
|
||||
measure: measure,
|
||||
displayProbes: displayProbes
|
||||
};
|
||||
};
|
||||
|
||||
measure = function(fn,options) {
|
||||
// start is outside so we measure time around recursive calls properly
|
||||
var start = null, nameParam, before, after;
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
if (typeof options === "string") {
|
||||
nameParam = options;
|
||||
}
|
||||
else
|
||||
{
|
||||
nameParam = options.name;
|
||||
|
||||
if (nameParam === "measure" || nameParam === "clear") {
|
||||
throw new Error("can not be called measure or clear");
|
||||
}
|
||||
|
||||
if (!nameParam)
|
||||
{
|
||||
throw new Error("you must specify the name option measure(fn, {name: 'some name'})");
|
||||
}
|
||||
|
||||
before = options.before;
|
||||
after = options.after;
|
||||
}
|
||||
|
||||
var now = (function(){
|
||||
var perf = window.performance || {};
|
||||
var time = perf.now || perf.mozNow || perf.webkitNow || perf.msNow || perf.oNow;
|
||||
return time ? time.bind(perf) : function() { return new Date().getTime(); };
|
||||
})();
|
||||
|
||||
return function() {
|
||||
var name = nameParam;
|
||||
if (typeof name === "function"){
|
||||
name = nameParam(arguments);
|
||||
}
|
||||
var p = window.probes[name];
|
||||
var owner = (!start);
|
||||
|
||||
if (before) {
|
||||
// would like to avoid try catch so its optimised properly by chrome
|
||||
before(p, owner, arguments);
|
||||
}
|
||||
|
||||
if (p === undefined) {
|
||||
window.probes[name] = {count: 0, time: 0, currentTime: 0};
|
||||
p = window.probes[name];
|
||||
}
|
||||
|
||||
var callStart;
|
||||
if (owner) {
|
||||
start = now();
|
||||
callStart = start;
|
||||
}
|
||||
else if(after) {
|
||||
callStart = now();
|
||||
}
|
||||
|
||||
var r = fn.apply(this, arguments);
|
||||
if (owner && start) {
|
||||
p.time += now() - start;
|
||||
start = null;
|
||||
}
|
||||
p.count += 1;
|
||||
|
||||
if (after) {
|
||||
p.currentTime = now() - callStart;
|
||||
// would like to avoid try catch so its optimised properly by chrome
|
||||
after(p, owner, arguments);
|
||||
}
|
||||
|
||||
return r;
|
||||
};
|
||||
};
|
||||
|
||||
var displayProbes = function(){
|
||||
var pre;
|
||||
var text = "";
|
||||
var body = document.getElementsByTagName("body")[0];
|
||||
|
||||
for(var prop in window.probes){
|
||||
var probe = window.probes[prop];
|
||||
if(probe && probe.count){
|
||||
text += prop + ": " + probe.time + " ( " + probe.count + " )\n";
|
||||
}
|
||||
}
|
||||
|
||||
pre = document.getElementById("__probes");
|
||||
|
||||
if(!body){
|
||||
return;
|
||||
}
|
||||
|
||||
if(pre){
|
||||
pre.textContent = text;
|
||||
pre.innerText = text;
|
||||
return;
|
||||
}
|
||||
|
||||
var div = document.createElement("div");
|
||||
div.id = "__probes_wrapper";
|
||||
div.setAttribute("style", "position: fixed; bottom: 25px; left: 50px; z-index: 99999; border: 1px solid #777; padding: 10px; background-color: rgba(255,255,255, 0.8);");
|
||||
|
||||
pre = document.createElement("pre");
|
||||
pre.setAttribute("style", "margin:0 0 5px;");
|
||||
pre.textContent = text;
|
||||
pre.innerText = text;
|
||||
pre.id = "__probes";
|
||||
|
||||
div.appendChild(pre);
|
||||
|
||||
var a = document.createElement('a');
|
||||
a.href = "";
|
||||
a.innerText = "clear";
|
||||
a.addEventListener("click", function(e){
|
||||
for(var prop in window.probes){
|
||||
var probe = window.probes[prop];
|
||||
if(probe && probe.count){
|
||||
delete window.probes[prop];
|
||||
}
|
||||
}
|
||||
displayProbes();
|
||||
e.preventDefault();
|
||||
});
|
||||
|
||||
div.appendChild(a);
|
||||
|
||||
body.appendChild(div);
|
||||
};
|
||||
|
||||
|
||||
// setInterval(displayProbes, 1000);
|
||||
clear();
|
||||
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue