-
Notifications
You must be signed in to change notification settings - Fork 0
/
lorum.js
118 lines (105 loc) · 3.5 KB
/
lorum.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var lorum = (function() {
var my = {};
function formatLorum(loremData) {
var text = "";
var i;
var j;
var nextSpace = false;
for (i = 0; i < loremData.length; ++i) {
var sentence = loremData[i];
for (j = 0; j < sentence.length; ++j) {
var token = sentence[j];
var noSpace = token[3];
var spaceLeft = nextSpace && noSpace !== "left";
nextSpace = noSpace !== "right";
if (spaceLeft) {
text += " ";
}
text += token[0];
}
}
return text;
}
function generate(withInitSentence) {
var deferred = $.Deferred();
var url = "http://www.lorumipse.hu/generate/";
if (withInitSentence) {
url += "init/";
}
$.ajax(url).then(function(data) {
var text = formatLorum(data);
deferred.resolve(text);
});
return deferred.promise();
}
my.addGeneratedTextToTarget = function(trg_spec, withInitSentence) {
var trg = $(trg_spec);
var deferred = $.Deferred();
generate(withInitSentence).then(function(text) {
trg.append("<p>" + text + "</p>");
deferred.resolve();
});
return deferred.promise();
};
my.highlightLastPara = function(parent_spec) {
var para = parent_spec + " p:last-child";
setTimeout(function() {
$(para).addClass("highlighted");
}, 0);
window.scrollTo(0, document.body.scrollHeight);
setTimeout(function() {
$(para).removeClass("highlighted");
}, 500);
}
return my;
})();
var lorumNav = (function() {
var my = {};
var openSectionId = null;
function collapseSection(section) {
openSectionId = null;
$(".collapsible-header li[data-section='" + section + "']")
.removeClass("collapsible-header-open")
.addClass("collapsible-header-closed");
$(".collapsible-section[data-section='" + section + "']").hide("fast");
$(".collapsible-close-button").hide("fast");
}
function openSection(section) {
openSectionId = section;
$(".collapsible-header li[data-section='" + section + "']")
.removeClass("collapsible-header-closed")
.addClass("collapsible-header-open");
$(".collapsible-section[data-section='" + section + "']").show("fast");
$(".collapsible-close-button").show("fast");
}
function bindCollapsible(index, item) {
var toggleHandler = function(event) {
collapseOpenSection();
openSection($(item).attr("data-section"));
};
$(item).click(toggleHandler);
$(item).addClass("collapsible-header-closed");
}
function collapseOpenSection() {
if (openSectionId) {
collapseSection(openSectionId);
}
$(".collapsible-close-button").hide();
}
my.init = function() {
$(".collapsible-section").hide();
$(".collapsible-header li").each(bindCollapsible);
$(".collapsible-close-button").click(collapseOpenSection)
};
return my;
})();
$(function() {
lorumNav.init();
var target = "#lorumtext";
$("#more").click(function() {
lorum.addGeneratedTextToTarget(target, false).then(function() {
lorum.highlightLastPara(target);
});
});
lorum.addGeneratedTextToTarget(target, true);
});