|
50
|
1 |
function showTags() { |
|
|
2 |
var tagsOuter = $(".taglist_container"), |
|
|
3 |
tagsInner = $(".taglist_container table"), |
|
|
4 |
acInput = $("#form_tag input[type=search]"), |
|
|
5 |
acdata = [], |
|
|
6 |
labels = { |
|
|
7 |
content: "Séquence", |
|
64
|
8 |
tag: "Mot-clé" |
|
|
9 |
}, |
|
|
10 |
replacerx; |
|
50
|
11 |
|
|
|
12 |
acInput.autocomplete({ |
|
|
13 |
source: function( request, response ) { |
|
64
|
14 |
var charsub = [ '[aáàâä]', '[cç]', '[eéèêë]', '[iíìîï]', '[oóòôö]' ], |
|
|
15 |
terms = request.term.split(/[\s,]/), |
|
|
16 |
escterms = terms.map(function(term) { |
|
|
17 |
var t = term.replace(/([\\\*\+\?\|\{\[\}\]\(\)\^\$\.\#\/])/gm, '\\$1'); |
|
|
18 |
_(charsub).each(function(chars) { |
|
|
19 |
var tmprx = new RegExp(chars,"gim"); |
|
|
20 |
t = t.replace(tmprx, chars); |
|
|
21 |
}); |
|
|
22 |
return t; |
|
|
23 |
}).filter(_.identity); |
|
|
24 |
var searchrxs = _(escterms).map(function(term) { |
|
|
25 |
return new RegExp("(^|\\s)" + term, "i") |
|
|
26 |
}); |
|
|
27 |
replacerx = new RegExp("(^|\\s)(" + escterms.join("|") + ")", "gi"); |
|
|
28 |
var filtered = acdata |
|
|
29 |
.filter(function(d) { |
|
|
30 |
return _(searchrxs).reduce(function(memo, rx) { |
|
|
31 |
return memo && rx.test(d.label); |
|
|
32 |
}, true); |
|
|
33 |
}) |
|
|
34 |
.sort(function(a, b) { |
|
|
35 |
if (a.type === b.type) { |
|
|
36 |
return (a.label > b.label ? 1 : (a.label < b.label ? -1 : 0)); |
|
|
37 |
} else { |
|
|
38 |
return (a.type === "content" ? -1 : 1); |
|
|
39 |
} |
|
|
40 |
}); |
|
|
41 |
response(filtered); |
|
50
|
42 |
}, |
|
|
43 |
select: function(event, ui) { |
|
|
44 |
document.location.href = ui.item.href; |
|
|
45 |
} |
|
|
46 |
}).data("autocomplete")._renderItem = function(ul, item) { |
|
|
47 |
return $( "<li>" ) |
|
|
48 |
.data( "item.autocomplete", item ) |
|
64
|
49 |
.append( "<a href='" + item.href + "'>" + labels[item.type] + " : " + item.label.replace(replacerx, '$1<b>$2</b>') + "</a>" ) |
|
50
|
50 |
.appendTo( ul ); |
|
|
51 |
}; |
|
64
|
52 |
acInput.on("keyup input paste", function() { |
|
|
53 |
acInput.val(acInput.val().toUpperCase()); |
|
|
54 |
}); |
|
50
|
55 |
|
|
|
56 |
$("#form_tag").submit(function() { |
|
|
57 |
return false; |
|
|
58 |
}); |
|
|
59 |
|
|
|
60 |
tagsInner.draggable(); |
|
|
61 |
|
|
64
|
62 |
var dragtimeout; |
|
|
63 |
|
|
|
64 |
$(".arrow_left").data({xdelta: 6, ydelta: 0}); |
|
|
65 |
$(".arrow_right").data({xdelta: -6, ydelta: 0}); |
|
|
66 |
$(".arrow_top").data({xdelta: 0, ydelta: 6}); |
|
|
67 |
$(".arrow_bottom").data({xdelta: 0, ydelta: -6}); |
|
|
68 |
|
|
|
69 |
$(".arrow") |
|
|
70 |
.mousedown(function() { |
|
|
71 |
var deltas = $(this).data(), |
|
|
72 |
move = function() { |
|
|
73 |
tagsInner.css({ |
|
|
74 |
"left": deltas.xdelta + parseInt(tagsInner.css("left")), |
|
|
75 |
"top": deltas.ydelta + parseInt(tagsInner.css("top")) |
|
|
76 |
}); |
|
|
77 |
} |
|
|
78 |
window.clearInterval(dragtimeout); |
|
|
79 |
dragtimeout = window.setInterval(move, 20); |
|
|
80 |
move(); |
|
|
81 |
}) |
|
|
82 |
.on( |
|
|
83 |
"mouseout mouseup", |
|
|
84 |
function() { |
|
|
85 |
window.clearInterval(dragtimeout); |
|
|
86 |
} |
|
|
87 |
); |
|
|
88 |
|
|
50
|
89 |
function resizeTags() { |
|
|
90 |
tagsInner.css({ |
|
|
91 |
"margin-left": - Math.floor(tagsInner.width()/2) + "px", |
|
|
92 |
"margin-top": - Math.floor(tagsInner.height()/2) + "px", |
|
|
93 |
"left": Math.floor(tagsOuter.width()/2) + "px", |
|
|
94 |
"top": Math.floor(tagsOuter.height()/2) + "px" |
|
|
95 |
}); |
|
|
96 |
} |
|
|
97 |
$(window).on("resize",resizeTags); |
|
|
98 |
|
|
|
99 |
$.ajax({ |
|
|
100 |
url: endpoints.contents_api, |
|
|
101 |
dataType: "jsonp", //TODO: JSON if on Platform |
|
|
102 |
data: { |
|
|
103 |
limit: 0, |
|
|
104 |
format: "jsonp" |
|
|
105 |
}, |
|
|
106 |
success: function(data) { |
|
|
107 |
_(data.objects).each(function(content) { |
|
64
|
108 |
var fpmatch = (content.front_project || "").match(/projects\/([0-9a-f\-]+)/), |
|
50
|
109 |
fproj = fpmatch ? fpmatch[1] : ""; |
|
|
110 |
acdata.push({ |
|
|
111 |
label: content.title, |
|
|
112 |
type: "content", |
|
|
113 |
href: endpoints.content_page.replace("__CONTENT_ID__", content.iri_id).replace("__FRONT_PROJECT_ID__", fproj) |
|
|
114 |
}); |
|
|
115 |
}); |
|
|
116 |
} |
|
|
117 |
}) |
|
|
118 |
|
|
|
119 |
$.ajax({ |
|
|
120 |
url: endpoints.tag_api, |
|
|
121 |
dataType: "json", |
|
|
122 |
data: { |
|
|
123 |
contents: "all", |
|
|
124 |
format: "json", |
|
|
125 |
limit: 0, |
|
|
126 |
steps: 100 |
|
|
127 |
}, |
|
|
128 |
success: function(data) { |
|
|
129 |
var grouped = {}; |
|
|
130 |
_(data.objects).each(function(t) { |
|
|
131 |
var upt = t.name.toUpperCase(), |
|
|
132 |
w = t.weight + (grouped[upt] || 0); |
|
|
133 |
grouped[upt] = w; |
|
|
134 |
}); |
|
|
135 |
_(grouped).each(function(v, k) { |
|
|
136 |
acdata.push({ |
|
|
137 |
label: k, |
|
|
138 |
type: "tag", |
|
|
139 |
href: endpoints.tag_page.replace("__TAG__", k) |
|
|
140 |
}); |
|
|
141 |
}); |
|
|
142 |
var ordered = _(grouped) |
|
|
143 |
.chain() |
|
|
144 |
.map(function(v, k) { |
|
|
145 |
return { |
|
|
146 |
label: k, |
|
|
147 |
weight: v |
|
|
148 |
} |
|
|
149 |
}) |
|
|
150 |
.sortBy(function(t) { |
|
|
151 |
return -t.weight; |
|
|
152 |
}) |
|
|
153 |
.value(); |
|
|
154 |
var l = ordered.length, |
|
|
155 |
max = ordered[0].weight, |
|
|
156 |
min = Math.min(max - 1, ordered[l - 1].weight), |
|
64
|
157 |
colorscale = 160 / (max - min), |
|
50
|
158 |
ncols = Math.floor(.65*Math.sqrt(l)), |
|
|
159 |
nlines = Math.ceil(l/ncols); |
|
|
160 |
var cells = _(nlines * ncols) |
|
|
161 |
.chain() |
|
|
162 |
.range() |
|
|
163 |
.map(function(i) { |
|
|
164 |
var x = i % ncols, |
|
|
165 |
y = Math.floor(i / ncols), |
|
|
166 |
dx = x - ((ncols - 1) / 2), |
|
|
167 |
dy = y - ((nlines - 1) / 2), |
|
|
168 |
d = Math.sqrt(dx*dx + dy*dy); |
|
|
169 |
return { |
|
|
170 |
n: i, |
|
|
171 |
x: x, |
|
|
172 |
y: y, |
|
|
173 |
d: d |
|
|
174 |
} |
|
|
175 |
}) |
|
|
176 |
.sortBy(function(c) { |
|
|
177 |
return c.d; |
|
|
178 |
}) |
|
|
179 |
.value(); |
|
|
180 |
_(ordered).each(function(v, k) { |
|
|
181 |
cells[k].tag = v; |
|
|
182 |
}); |
|
|
183 |
cells = _(cells).sortBy(function(c) { return c.n }); |
|
|
184 |
var _html = ""; |
|
|
185 |
_(cells).each(function(c) { |
|
|
186 |
if (c.x === 0) { |
|
|
187 |
_html += "<tr>"; |
|
|
188 |
} |
|
|
189 |
_html += "<td>"; |
|
|
190 |
if (c.tag) { |
|
64
|
191 |
var color = Math.floor( 95 + (c.tag.weight - min) * colorscale ); |
|
50
|
192 |
_html += '<a href="' |
|
|
193 |
+ endpoints.tag_page.replace("__TAG__",encodeURIComponent(c.tag.label)) |
|
|
194 |
+ '" style="color: rgb(' |
|
|
195 |
+ [color,color,color].join(",") |
|
|
196 |
+ ')">' |
|
|
197 |
+ c.tag.label |
|
|
198 |
+ '</a>'; |
|
|
199 |
} |
|
|
200 |
_html += "</td>"; |
|
|
201 |
if (c.x === (ncols - 1)) { |
|
|
202 |
_html += "</tr>"; |
|
|
203 |
} |
|
|
204 |
}); |
|
|
205 |
tagsInner.html(_html); |
|
|
206 |
resizeTags(); |
|
|
207 |
} |
|
|
208 |
}); |
|
|
209 |
} |