| author | ymh <ymh.work@gmail.com> |
| Thu, 05 Mar 2015 16:58:28 +0100 | |
| changeset 479 | 51cd6fa9dfe3 |
| parent 477 | 8f70ba2106a9 |
| child 620 | f45d7494332e |
| permissions | -rw-r--r-- |
| 477 | 1 |
// from http://stackoverflow.com/a/7031800 |
2 |
||
3 |
$.fn.reveal = function() { |
|
4 |
var args = Array.prototype.slice.call(arguments); |
|
5 |
return this.each(function(){ |
|
6 |
var img = $(this), |
|
7 |
src = img.data("src"); |
|
|
479
51cd6fa9dfe3
small optimization on image loading
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
8 |
src && img.attr("src") !== src && img.attr("src", src).load(function(){ |
| 477 | 9 |
img[args[0]||"show"].apply(img, args.splice(1)); |
10 |
}); |
|
11 |
}); |
|
12 |
}; |
|
13 |
||
| 252 | 14 |
|
15 |
function countContentsAndThemes(_data) { |
|
16 |
_data.counts = { |
|
17 |
contents: (typeof _data.contents == "object" && typeof _data.contents.slice == "function" ? _data.contents.length : 0), |
|
18 |
themes: (typeof _data.themes == "object" && typeof _data.themes.slice == "function" ? _data.themes.length : 0) |
|
19 |
} |
|
20 |
if (typeof _data.themes == "object" && typeof _data.themes.slice == "function") { |
|
21 |
for (var _i = 0; _i < _data.themes.length; _i++) { |
|
22 |
countContentsAndThemes(_data.themes[_i]); |
|
23 |
var _subres = _data.themes[_i].counts; |
|
24 |
_data.counts.contents += _subres.contents; |
|
25 |
_data.counts.themes += _subres.themes; |
|
26 |
} |
|
27 |
} |
|
28 |
} |
|
29 |
||
| 261 | 30 |
function renderTemplate(_data, _level, _scale, _maxlevel) { |
31 |
_data.isfolded = _level >= _maxlevel; |
|
| 252 | 32 |
_(_data.tags).each(function(_tag) { |
33 |
_(_tag.contents).each(function(_content, _key) { |
|
34 |
_content.trimmed_description = _content.description.trim().replace(/(\n|\r|\r\n)/mg,' ').replace(/(^.{150,200})[\s].+$/m,'$1…'); |
|
35 |
_content.hidden = (_key >= 5); |
|
36 |
}); |
|
37 |
_tag.show_more = _tag.contents && _tag.contents.length > 5 ? _tag.contents.length - 5 : 0; |
|
38 |
}); |
|
39 |
_(_data.contents).each(function(_content, _key) { |
|
40 |
_content.trimmed_description = _content.description.trim().replace(/(\n|\r|\r\n)/mg,' ').replace(/(^.{150,200})[\s].+$/m,'$1…'); |
|
41 |
_content.hidden = (_key >= 5); |
|
42 |
}); |
|
43 |
_data.show_more = _data.contents && _data.contents.length > 5 ? _data.contents.length - 5 : 0; |
|
44 |
_data.font_size = (10+Math.log(_data.counts.contents)*_scale); |
|
| 253 | 45 |
var _dsurl = endpoints.datasheet.replace(/ID$/,''); |
| 261 | 46 |
var _prefix = '<li class="themeli{{#isfolded}} folded{{/isfolded}}"><p class="theme"><span style="font-size: {{font_size}}px">{{label}}</span> — {{#counts.themes}}{{counts.themes}} thème(s){{#counts.contents}}, {{/counts.contents}}{{/counts.themes}}{{#counts.contents}}{{counts.contents}} notice(s){{/counts.contents}}</p><ul>', |
| 253 | 47 |
_suffix = '{{#contents}}<li class="content{{#hidden}} hidden{{/hidden}}" id="content_{{id}}"><p class="title score_{{score}}"><a href="' |
48 |
+ _dsurl |
|
| 477 | 49 |
+ '{{hda_id}}">{{title}}</a></p><div class="foldedcontent"><div class="img-container"><img data-src="http://www.histoiredesarts.culture.fr/images/pf/{{hda_id}}.jpg" />' |
| 253 | 50 |
+ '</div><p class="description">Source: <a href="{{organization_url}}" target="_blank">{{organization}}</a><br /><a href="' |
51 |
+ _dsurl |
|
52 |
+ '{{hda_id}}" class="content">{{trimmed_description}}</a></p></div></li>{{/contents}}{{#show_more}}<li class="show_more">Plus de contenus (<span class="show_more_count">{{show_more}}</span>)…</li>{{/show_more}}</ul></li>', |
|
| 252 | 53 |
_html = Mustache.to_html(_prefix, _data); |
54 |
if (typeof _data.themes == "object" && typeof _data.themes.slice == "function") { |
|
55 |
for (var _i = 0; _i < _data.themes.length; _i++) { |
|
| 261 | 56 |
_html += renderTemplate(_data.themes[_i], _level + 1, _scale, _maxlevel); |
| 252 | 57 |
} |
58 |
} |
|
59 |
_html += Mustache.to_html(_suffix, _data); |
|
60 |
return _html; |
|
61 |
} |
|
62 |
||
| 261 | 63 |
function renderData(_selector, _data, _maxlevel) { |
| 252 | 64 |
countContentsAndThemes(_data); |
65 |
var _scale = 8 / Math.max(1,Math.log(_data.counts.contents)); |
|
| 261 | 66 |
_selector.html(renderTemplate(_data, 0, _scale, _maxlevel)); |
| 252 | 67 |
_selector.find("p.theme").click(function() { |
68 |
$(this).parent().toggleClass("folded") |
|
69 |
return false; |
|
70 |
}); |
|
71 |
_selector.find("li.content").mouseenter(function() { |
|
72 |
$(this).find(".foldedcontent") |
|
73 |
.dequeue() |
|
74 |
.animate({ |
|
75 |
height: 5 + $(this).find("p.description").outerHeight() |
|
76 |
}, |
|
77 |
500); |
|
| 477 | 78 |
$(this).find(".img-container img").reveal("fadeIn", 500); |
| 252 | 79 |
}).mouseleave(function() { |
80 |
$(this).find(".foldedcontent") |
|
81 |
.dequeue() |
|
82 |
.animate({ |
|
83 |
height: "0" |
|
84 |
}, |
|
85 |
500); |
|
86 |
}); |
|
87 |
_selector.find(".show_more").click(function() { |
|
88 |
$(this).siblings(".hidden:lt(5)").removeClass("hidden"); |
|
89 |
var _l = $(this).siblings(".hidden").length; |
|
90 |
$(this).find(".show_more_count").html(_l); |
|
91 |
if (!_l) { |
|
92 |
$(this).detach(); |
|
93 |
} |
|
94 |
}); |
|
95 |
} |