5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
function doFold(cm, pos, options, force) {
15
if (options && options.call) {
19
var finder = getOption(cm, options, "rangeFinder");
21
if (typeof pos == "number") pos = CodeMirror.Pos(pos, 0);
22
var minSize = getOption(cm, options, "minFoldSize");
24
function getRange(allowFolded) {
25
var range = finder(cm, pos);
26
if (!range || range.to.line - range.from.line < minSize) return null;
27
if (force === "fold") return range;
29
var marks = cm.findMarksAt(range.from);
30
for (var i = 0; i < marks.length; ++i) {
31
if (marks[i].__isFold) {
32
if (!allowFolded) return null;
40
var range = getRange(true);
41
if (getOption(cm, options, "scanUp")) while (!range && pos.line > cm.firstLine()) {
42
pos = CodeMirror.Pos(pos.line - 1, 0);
43
range = getRange(false);
45
if (!range || range.cleared || force === "unfold") return;
47
var myWidget = makeWidget(cm, options, range);
48
CodeMirror.on(myWidget, "mousedown", function(e) {
50
CodeMirror.e_preventDefault(e);
52
var myRange = cm.markText(range.from, range.to, {
53
replacedWith: myWidget,
54
clearOnEnter: getOption(cm, options, "clearOnEnter"),
57
myRange.on("clear", function(from, to) {
58
CodeMirror.signal(cm, "unfold", cm, from, to);
60
CodeMirror.signal(cm, "fold", cm, range.from, range.to);
63
function makeWidget(cm, options, range) {
64
var widget = getOption(cm, options, "widget");
66
if (typeof widget == "function") {
67
widget = widget(range.from, range.to);
70
if (typeof widget == "string") {
71
var text = document.createTextNode(widget);
72
widget = document.createElement("span");
73
widget.appendChild(text);
74
widget.className = "CodeMirror-foldmarker";
76
widget = widget.cloneNode(true)
82
CodeMirror.newFoldFunction = function(rangeFinder, widget) {
83
return function(cm, pos) { doFold(cm, pos, {rangeFinder: rangeFinder, widget: widget}); };
87
CodeMirror.defineExtension("foldCode", function(pos, options, force) {
88
doFold(this, pos, options, force);
91
CodeMirror.defineExtension("isFolded", function(pos) {
92
var marks = this.findMarksAt(pos);
93
for (var i = 0; i < marks.length; ++i)
94
if (marks[i].__isFold) return true;
97
CodeMirror.commands.toggleFold = function(cm) {
98
cm.foldCode(cm.getCursor());
100
CodeMirror.commands.fold = function(cm) {
101
cm.foldCode(cm.getCursor(), null, "fold");
103
CodeMirror.commands.unfold = function(cm) {
104
cm.foldCode(cm.getCursor(), { scanUp: false }, "unfold");
106
CodeMirror.commands.foldAll = function(cm) {
107
cm.operation(function() {
108
for (var i = cm.firstLine(), e = cm.lastLine(); i <= e; i++)
109
cm.foldCode(CodeMirror.Pos(i, 0), { scanUp: false }, "fold");
112
CodeMirror.commands.unfoldAll = function(cm) {
113
cm.operation(function() {
114
for (var i = cm.firstLine(), e = cm.lastLine(); i <= e; i++)
115
cm.foldCode(CodeMirror.Pos(i, 0), { scanUp: false }, "unfold");
119
CodeMirror.registerHelper("fold", "combine", function() {
120
var funcs = Array.prototype.slice.call(arguments, 0);
121
return function(cm, start) {
122
for (var i = 0; i < funcs.length; ++i) {
123
var found = funcs[i](cm, start);
124
if (found) return found;
129
CodeMirror.registerHelper("fold", "auto", function(cm, start) {
130
var helpers = cm.getHelpers(start, "fold");
131
for (var i = 0; i < helpers.length; i++) {
132
var cur = helpers[i](cm, start);
137
var defaultOptions = {
138
rangeFinder: CodeMirror.fold.auto,
145
CodeMirror.defineOption("foldOptions", null);
147
function getOption(cm, options, name) {
148
if (options && options[name] !== undefined)
149
return options[name];
150
var editorOptions = cm.options.foldOptions;
151
if (editorOptions && editorOptions[name] !== undefined)
152
return editorOptions[name];
153
return defaultOptions[name];
156
CodeMirror.defineExtension("foldOption", function(options, name) {
157
return getOption(this, options, name);