| author | cavaliet |
| Tue, 25 Feb 2014 18:46:27 +0100 | |
| changeset 260 | ebcc7a401ae9 |
| parent 258 | bfa951bac3e3 |
| permissions | -rwxr-xr-x |
| 121 | 1 |
$(function(){ |
|
191
d7b30914607d
enhance collection and add slideshow with images and fragments.
cavaliet
parents:
184
diff
changeset
|
2 |
|
| 121 | 3 |
//masonry |
| 149 | 4 |
var masonry465 = $('.masonry-465'); |
5 |
masonry465.masonry({ |
|
| 121 | 6 |
columnWidth: 465, |
| 149 | 7 |
itemSelector: '.item-masonry', |
| 121 | 8 |
gutter : 20 |
9 |
}); |
|
| 149 | 10 |
|
11 |
var masonry225 = $('.masonry-225'); |
|
12 |
masonry225.masonry({ |
|
13 |
columnWidth: 225, |
|
14 |
itemSelector: '.item-masonry', |
|
15 |
gutter : 16 |
|
16 |
}); |
|
17 |
||
| 121 | 18 |
//popin |
| 140 | 19 |
$('.open-popin').bind('click', function(e){ |
| 121 | 20 |
e.preventDefault(); |
21 |
var target = $(this).attr('href'); |
|
22 |
$('.popin-wrap').fadeIn(function(){ |
|
23 |
$(target).show(); |
|
24 |
}); |
|
25 |
}); |
|
26 |
||
27 |
$('.close-popin, .popin-wrap').bind('click', function(e){ |
|
28 |
e.preventDefault(); |
|
29 |
e.stopPropagation(); |
|
30 |
$('.popin-wrap').fadeOut(function(){ |
|
31 |
$('.popin').hide(); |
|
32 |
}); |
|
33 |
}); |
|
34 |
||
35 |
$('.popin').bind('click', function(e){ |
|
36 |
e.stopPropagation(); |
|
37 |
}); |
|
| 257 | 38 |
|
39 |
/* DBPEDIA OVERLAY */ |
|
40 |
|
|
41 |
var sparqlTpl = 'select distinct * where { ' |
|
42 |
+ 'OPTIONAL { <<%= uri %>> rdfs:label ?l FILTER( langMatches( lang(?l), "<%- lang %>" ) ) }. ' |
|
43 |
+ 'OPTIONAL { <<%= uri %>> dbpedia-owl:thumbnail ?t }. ' |
|
44 |
+ 'OPTIONAL { <<%= uri %>> dbpedia-owl:abstract ?a FILTER( langMatches( lang(?a), "<%- lang %>" ) ) }. ' |
|
45 |
+ 'OPTIONAL { <<%= uri %>> dbpedia-owl:wikiPageRedirects ?r }. ' |
|
46 |
+ 'OPTIONAL { ?r rdfs:label ?lr FILTER( langMatches( lang(?lr), "<%- lang %>" ) ) }. ' |
|
47 |
+ 'OPTIONAL { ?r dbpedia-owl:thumbnail ?tr }. ' |
|
48 |
+ 'OPTIONAL { ?r dbpedia-owl:abstract ?ar FILTER( langMatches( lang(?ar), "<%- lang %>" ) ) }. ' |
|
49 |
+ '}', |
|
50 |
$overlay = $(".dbpedia-overlay"), |
|
51 |
hovering = null, |
|
52 |
anchor = null, |
|
53 |
$win = $(window), |
|
54 |
dbpediaCache = {}, |
|
55 |
$overlayImg = $overlay.find("img"), |
|
56 |
$h2 = $overlay.find("h2"), |
|
57 |
$abstract = $overlay.find(".dbpedia-abstract"), |
|
58 |
$source = $overlay.find(".dbpedia-source a"); |
|
59 |
|
|
60 |
function setDbpediaBoxAnchor(a) { |
|
61 |
anchor = a || null; |
|
62 |
if (anchor) { |
|
63 |
recentreDbpediaBox(); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
function recentreDbpediaBox() { |
|
68 |
if (!anchor) { return; } |
|
69 |
var ovw = $overlay.outerWidth(), |
|
70 |
ovh = $overlay.outerHeight(), |
|
71 |
refbox; |
|
72 |
switch (anchor.type) { |
|
73 |
case "dom": |
|
74 |
var $refdiv = anchor.selector, |
|
75 |
refoff = $refdiv.offset(), |
|
76 |
refw = $refdiv.outerWidth(), |
|
77 |
refh = $refdiv.outerHeight(), |
|
78 |
refx = refoff.left, |
|
79 |
refy = refoff.top; |
|
80 |
refbox = { left: refx, right: refx + refw, top: refy, bottom: refy + refh }; |
|
81 |
break; |
|
82 |
case "box": |
|
83 |
refbox = anchor.box; |
|
84 |
break; |
|
85 |
case "callback": |
|
86 |
refbox = anchor.callback(); |
|
87 |
break; |
|
88 |
} |
|
89 |
if (!refbox) { return; } |
|
90 |
if (!refbox.right) { refbox.right = refbox.left; } |
|
91 |
if (!refbox.bottom) { refbox.bottom = refbox.top; } |
|
92 |
refbox.hcentre = (refbox.left + refbox.right) / 2; |
|
93 |
switch (anchor.positioning) { |
|
94 |
case "side": |
|
95 |
var showLeft = (refbox.right + ovw) > $win.width(); |
|
96 |
css = { left: showLeft ? (refbox.left - ovw) : (refbox.right), top: refbox.top }; |
|
97 |
break; |
|
98 |
case "bottom": |
|
99 |
css = { left: refbox.hcentre - ovw / 2, top: refbox.bottom }; |
|
100 |
break; |
|
101 |
case "vertical": |
|
102 |
default: |
|
103 |
var showAbove = (refbox.bottom + ovh) > ($win.height() + $win.scrollTop()); |
|
104 |
css = { left: refbox.hcentre - ovw / 2, top: showAbove ? refbox.top - ovh : refbox.bottom }; |
|
105 |
} |
|
106 |
if (css) { |
|
107 |
css.left = Math.max(5, Math.min($win.width() - ovw - 5, css.left)); |
|
108 |
$overlay.css(css); |
|
109 |
} |
|
110 |
} |
|
111 |
|
|
112 |
function showDbpediaBox(dbpediaUri) { |
|
113 |
if (!dbpediaUri) { |
|
114 |
return; |
|
115 |
} |
|
116 |
hovering = dbpediaUri; |
|
117 |
$overlay.hide(); |
|
118 |
$overlayImg.attr("src",""); |
|
119 |
var uriData = dbpediaCache[dbpediaUri]; |
|
120 |
if (!uriData) { |
|
121 |
getUriData(dbpediaUri); |
|
122 |
return; |
|
123 |
} |
|
124 |
$overlay.show().attr("data-dbpedia-uri", dbpediaUri); |
|
125 |
if (uriData.t || uriData.tr) { |
|
126 |
$overlayImg.attr("src",uriData.t || uriData.tr).show(); |
|
127 |
} else { |
|
128 |
$overlayImg.hide(); |
|
129 |
} |
|
130 |
var label = uriData.l || uriData.lr || "", |
|
131 |
wkUrl = "http://fr.wikipedia.org/"; |
|
132 |
if (label) { |
|
133 |
wkUrl += "wiki/" + encodeURI(label.replace(/ /g,'_')); |
|
134 |
} |
|
135 |
$h2.text((uriData.l && uriData.lr) ? (uriData.l + " → " + uriData.lr) : label); |
|
136 |
$abstract.text((uriData.a || uriData.ar || "").replace(/^(.{240,260})\s.+$/,'$1…').substr(0,261)); |
|
137 |
$source.attr("href", wkUrl); |
|
138 |
recentreDbpediaBox(); |
|
139 |
} |
|
140 |
|
|
141 |
function getUriData(dbpediaUri) { |
|
142 |
if (typeof dbpediaCache[dbpediaUri] !== "undefined") { |
|
143 |
return; |
|
144 |
} |
|
145 |
var sparqlEndpoint = dbpediaUri.replace(/\/resource\/.*$/,'/sparql'), |
|
146 |
query = sparqlTpl.replace(new RegExp("<%= uri %>", "g"), decodeURI(dbpediaUri)).replace(new RegExp("<%- lang %>", "g"), "fr"); |
|
147 |
dbpediaCache[dbpediaUri] = false; |
|
148 |
$.getJSON(sparqlEndpoint, { |
|
149 |
query: query, |
|
150 |
format: "application/sparql-results+json" |
|
151 |
}, function(data) { |
|
152 |
if (!data.results || !data.results.bindings || !data.results.bindings.length) { |
|
153 |
return; |
|
154 |
} |
|
155 |
var res = data.results.bindings[0], cacheData = {}; |
|
156 |
for (var k in res) { |
|
157 |
if (res.hasOwnProperty(k)) { |
|
158 |
cacheData[k] = res[k].value; |
|
159 |
} |
|
160 |
} |
|
161 |
dbpediaCache[dbpediaUri] = cacheData; |
|
162 |
if (hovering === dbpediaUri) { |
|
163 |
showDbpediaBox(dbpediaUri); |
|
164 |
} |
|
165 |
}); |
|
166 |
} |
|
167 |
|
|
168 |
function hideDbpediaBox() { |
|
169 |
hovering = null; |
|
170 |
setTimeout(function() { |
|
171 |
if (!hovering) { |
|
172 |
$overlay.hide(); |
|
173 |
setDbpediaBoxAnchor(); |
|
174 |
} |
|
175 |
}, 0); |
|
176 |
} |
|
177 |
|
|
178 |
function bindDbpediaBox(selector, defaultUri) { |
|
179 |
var $sel = $(selector); |
|
180 |
$sel.off("mouseenter mouseleave"); |
|
181 |
$sel.mouseenter(function(e) { |
|
182 |
var $this = $(this); |
|
183 |
setDbpediaBoxAnchor({ selector: $this, type: "dom", positioning: "vertical" }); |
|
184 |
var dbpediaUri = $this.attr("data-dbpedia-uri") || defaultUri; |
|
185 |
if (!dbpediaUri || dbpediaUri === "None") { |
|
186 |
return; |
|
187 |
} |
|
188 |
showDbpediaBox(dbpediaUri); |
|
189 |
}); |
|
190 |
$sel.mouseleave(hideDbpediaBox); |
|
191 |
} |
|
| 260 | 192 |
bindDbpediaBox(".for-overlay"); |
| 257 | 193 |
|
194 |
$overlay.hover(function() { |
|
195 |
var $this = $(this), |
|
196 |
dbpediaUri = $this.attr("data-dbpedia-uri"); |
|
197 |
if (dbpediaUri) { |
|
198 |
hovering = dbpediaUri; |
|
199 |
} |
|
200 |
}, hideDbpediaBox); |
|
201 |
|
|
202 |
$overlay.find(".dbpedia-close").click(function() { |
|
203 |
hideDbpediaBox(); |
|
204 |
return false; |
|
205 |
}); |
|
206 |
|
|
207 |
window.dbpediaBox = { |
|
208 |
bind: bindDbpediaBox, |
|
209 |
hide: hideDbpediaBox, |
|
210 |
show: showDbpediaBox, |
|
211 |
setAnchor: setDbpediaBoxAnchor, |
|
212 |
recentre: recentreDbpediaBox |
|
213 |
}; |
|
214 |
|
|
215 |
/* END DBPEDIA OVERLAY MANAGEMENT */ |
|
216 |
|
|
217 |
// TAG IT |
|
| 149 | 218 |
if($('.tag-it').length){ |
219 |
var keywordsTagIt = $('.tag-it').tagit({ |
|
| 256 | 220 |
allowSpaces : true, |
221 |
tagSource: function(search, showChoices){ |
|
222 |
$.ajax({ |
|
223 |
url : "http://fr.wikipedia.org/w/api.php", |
|
224 |
dataType: "jsonp", |
|
225 |
data : { |
|
226 |
action: "opensearch", |
|
227 |
search: search.term, |
|
228 |
format: "json", |
|
229 |
limit: 10 |
|
230 |
}, |
|
231 |
success:function(data){ |
|
232 |
var datas = data[1]; |
|
233 |
var n = datas.length; |
|
234 |
var choices = []; |
|
235 |
for(var i=0;i<n;i++){ |
|
236 |
var l = data[1][i]; |
|
| 257 | 237 |
var l_uri = l.replace(new RegExp(" ","g"),"_"); |
238 |
choices.push({ label:l, value:( 'http://fr.wikipedia.org/wiki/' + l_uri), dbpedia_uri: ('http://fr.dbpedia.org/resource/' + l_uri) }); |
|
| 256 | 239 |
} |
240 |
showChoices(choices); |
|
241 |
} |
|
242 |
}); |
|
243 |
}, |
|
| 257 | 244 |
autocomplete: { |
245 |
focus: function(e, ui) { |
|
246 |
showDbpediaBox(ui.item.dbpedia_uri); |
|
247 |
setDbpediaBoxAnchor({type: "dom", selector: $(e.target).autocomplete("widget"), positioning: "side"}); |
|
| 258 | 248 |
}, |
249 |
close: function(e, ui){ |
|
250 |
hideDbpediaBox(); |
|
| 257 | 251 |
} |
252 |
}, |
|
| 256 | 253 |
beforeTagAdded: function(event, ui) { |
254 |
if($(ui.tag.children()[0]).hasClass("tagit-label")){ |
|
255 |
var tagit_label = $(ui.tag.children()[0]).html(); |
|
256 |
if(tagit_label.substr(0, 29) == 'http://fr.wikipedia.org/wiki/'){ |
|
| 257 | 257 |
var label = tagit_label.substr(29).replace(new RegExp("_","g")," "); |
| 256 | 258 |
$(ui.tag.children()[0]).html(label + ' <a href="' + tagit_label + '" target="_blank"><span class="logo_wikipedia f-right"> </span></a>'); |
| 260 | 259 |
ui.tag.attr("data-dbpedia-uri", "http://fr.dbpedia.org/resource/" + tagit_label.substr(29)); |
260 |
bindDbpediaBox(ui.tag); |
|
| 256 | 261 |
} |
262 |
} |
|
| 258 | 263 |
hideDbpediaBox(); |
264 |
}, |
|
265 |
beforeTagRemoved: function(event, ui) { |
|
266 |
// test if tag label is wikipedia url |
|
267 |
if($(ui.tag.children()[0]).children().length>0){ |
|
268 |
var $a_tag = $($(ui.tag.children()[0]).children()[0]); |
|
269 |
if($a_tag.attr("href").substr(0, 29) == 'http://fr.wikipedia.org/wiki/'){ |
|
270 |
// Before removing, turn label into url |
|
271 |
$(ui.tag.children()[0]).html($a_tag.attr("href").replace(new RegExp(" ","g"),"_")); |
|
272 |
} |
|
273 |
} |
|
| 256 | 274 |
} |
| 149 | 275 |
}); |
276 |
$('.list-key-add a').bind('click', function(e){ |
|
277 |
e.preventDefault(); |
|
| 226 | 278 |
keywordsTagIt.tagit("createTag", $(this).attr('data-tag')); |
| 149 | 279 |
}); |
280 |
} |
|
| 168 | 281 |
|
|
191
d7b30914607d
enhance collection and add slideshow with images and fragments.
cavaliet
parents:
184
diff
changeset
|
282 |
|
| 140 | 283 |
|
| 168 | 284 |
// add item to collection behaviour |
285 |
$('.additemtocollection').bind('click', function(e){ |
|
286 |
// When an item meant to be added to a collection is clicked, |
|
287 |
// we fill the form in the add-to-collection div |
|
288 |
$('#add-to-collection .item-type').val($(this).attr('data-type')); |
|
289 |
$('#add-to-collection .item-id').val($(this).attr('data-id')); |
|
290 |
}); |
|
| 234 | 291 |
// modify item in collection behaviour |
292 |
$('.modifyitemincollection').bind('click', function(e){ |
|
293 |
$('#modify-item .item-pk').val($(this).attr('data-pk')); |
|
294 |
$('#modify-item .item-description').val($(this).attr('data-description')); |
|
295 |
}); |
|
| 168 | 296 |
|
297 |
|
|
| 210 | 298 |
$(".ajax-form").submit(function(e) { // On submit Ajax Form |
299 |
var formel = $(this); |
|
300 |
$.ajax({ |
|
301 |
url: formel.attr("action"), |
|
302 |
type: formel.attr("method"), |
|
303 |
data: formel.serialize(), |
|
|
211
35aca8206b40
collection parameters with title, delete collection, ajax add item
cavaliet
parents:
210
diff
changeset
|
304 |
success: function(text) { |
| 234 | 305 |
// different behaviour if we add or modify an item |
306 |
if(text=="modifyok"){ |
|
307 |
// modify |
|
308 |
$('.popin-wrap').fadeIn(function(){ |
|
309 |
$(".modifyitem-success").show(); |
|
310 |
}); |
|
311 |
// update text. $("item{{ item.pk }}") is the <div> with the text as <p> and <a data-description> |
|
312 |
var item_pk = $('#modify-item .item-pk').val(); |
|
313 |
var item_desc = $('#modify-item .item-description').val(); |
|
314 |
$("#item" + item_pk).find("a").attr("data-description", item_desc); |
|
315 |
$("#item" + item_pk).find("p").html(item_desc.replace(/\n/g, '<br />')); |
|
316 |
} |
|
317 |
else{ |
|
318 |
// add |
|
319 |
$('.popin-wrap').fadeIn(function(){ |
|
320 |
$(".additem-success").show(); |
|
321 |
}); |
|
322 |
$(".additem-success .collection-url").attr("href", text); |
|
323 |
} |
|
| 210 | 324 |
}, |
325 |
error: function() { |
|
326 |
$('.popin-wrap').fadeIn(function(){ |
|
327 |
$(".additem-error").show(); |
|
328 |
}); |
|
329 |
} |
|
330 |
}); |
|
331 |
formel.parents(".popin").hide(); |
|
332 |
return false; |
|
| 240 | 333 |
}); |
| 168 | 334 |
|
335 |
});//ready |