forked from svanlaere/easysnippet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasysnippet.js
82 lines (68 loc) · 2.23 KB
/
easysnippet.js
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
// http://stackoverflow.com/a/2819568
jQuery.fn.extend({
insertAtCaret: function(myValue){
return this.each(function(i) {
if (document.selection) {
//For browsers like Internet Explorer
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
else if (this.selectionStart || this.selectionStart == '0') {
//For browsers like Firefox and Webkit based
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos)
+myValue+this.value.substring(endPos,this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
} else {
this.value += myValue;
this.focus();
}
})
}
});
Easysnippet= function init (uiVariant, customTag){
return function (){
var
view = $("#easysnippet"),
insert = function (e){
try { // this code is fragile, touches the DOM and must return false
var snippet = 'select' == uiVariant
? view.find("option:selected").text()
: e.target.textContent,
php_left = "<",
php_start = "?php",
include_start = "$this->includeSnippet('",
include_end = "');",
php_end = "?>",
space = " ",
pagepart = $('.here')[1].hash;
if ('select' == uiVariant){
view.find("button") .css({display:'inline-block'});
view.find("option:lt(1)").css({display:'none'});
}
$(pagepart).find('textarea')
.insertAtCaret([
customTag ? customTag[0]
: (php_left + php_start + space + include_start),
snippet,
customTag ? customTag[1]
: (include_end + space + php_end),
'\n'
].join('')).change();
} catch (_) {;} // if this fails the page gets saved
return false
};
view.find("button").click(insert);
if ('select' == uiVariant){
view.find("option:lt(1)").attr("disabled", "disabled");
view.find("select").change(insert);
}
}
}