-
Notifications
You must be signed in to change notification settings - Fork 0
/
fancyBoxPlugin.js
495 lines (422 loc) · 14.2 KB
/
fancyBoxPlugin.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/***
|''Name''|FancyBoxPlugin|
|''Description''|Wraps the jQuery.fancybox() function into a TiddlyWiki friendly macro|
|''Author''|PMario|
|''Version''|0.4.13|
|''Status''|''beta''|
|''Source''|http://fancybox-plugin.tiddlyspace.com/|
|''License''|http://www.opensource.org/licenses/mit-license.php|
|''CoreVersion''|2.5.0|
|''Keywords''|light box fancybox|
!Documentation
*[[FancyBoxPluginInfo|http://fancybox.tiddlyspace.com/#About]]
!!!Description
<<<
Wraps the jQuery.fancybox() function into a TiddlyWiki friendly macro
<<<
!!!!Usage
<<<
Used in a tiddler, the macro assumes, that ''only'' pictures inside this tiddler will open the lightbox.
The default mode is "slides". If there are several pictures in one tiddler and you click one, a slideshow of all of them will be created.
{{{
<<fancyBox>>
}}}
If you add the {{{mode:picture}}} parameter it will open the lightbox in single picture mode.
{{{
<<fancyBox mode:picture>>
}}}
Used with ViewTemplate
{{{
<span macro='fancyBox'></span>
or
<span macro='fancyBox mode:picture'></span>
}}}
<<<
!!!!StyleSheet
<<<
*add this to your StyleSheet
{{{
[[FancyBoxStyleSheet]]
}}}
<<<
!!!!~AttachFilePluginFormatters
<<<
If AttachFilePluginFormatters plugin is available, it will be used.
<<<
!!!!Further readings
<<<
see: http://fancybox.net/api
<<<
***/
//{{{
version.extensions.FancyBox = {
major: 0,
minor: 4,
revision: 13,
date: new Date(2011, 2, 26)
};
(function ($) {
var me;
config.macros.fancyBox = me = {
// should be done for easy localisation
locale: {
},
getPictureInfo: function(title, opts, elem) {
var picURI;
picURI = (opts[elem]) ? opts[elem] + encodeURIComponent(title) : '';
if (!picURI) {
picURI = (config.macros.attach) ? config.macros.attach.getAttachment(title) : encodeURIComponent(title);
}
return picURI;
},
setFbAdvanced: function (conf, params) {
// set the fancyBox advanced options
var p = ['type', 'href', 'title', 'content', 'orig', 'index'];
var tmp;
for (var i=0, im = p.length; i<im; i += 1) {
tmp = getParam( params, p[i], undefined);
if (tmp) {conf[p[i]] = tmp;}
}
// return conf;
},
createElement: function (tag, thumbURI, picURI, label, relId, alt, data) {
return '<span class="twfb-list twfb-' + tag + '">' +
'<a class="imageLink" rel="' + relId + '" href=' + picURI + '>' +
'<img title="' + label + '" alt="' + alt + '" src="' + thumbURI + '" />' +
'</a>' +
'</span>';
},
createStack: function () {
var xx;
// createTiddlyElement( parent, element, id, className, text, attribs)
xx = createTiddlyElement(place, 'div', null, 'stackLeft');
xx = createTiddlyElement(xx, 'div', null, 'stackRight');
xx = createTiddlyElement(xx, 'div', null, 'stackNormal');
return xx;
},
helper : {
'true': true,
'false': false,
'null': null
},
calcTextSlices: function (text) {
var slices = {};
store.slicesRE.lastIndex = 0;
var m = store.slicesRE.exec(text);
while (m) {
if (m[2]) {
if (isNaN(m[3])) {
slices[m[2]] = (m[3] in me.helper) ? me.helper[m[3]] : m[3];
}
else {
slices[m[2]] = parseFloat(m[3]);
}
} else {
if (isNaN(m[6])) {
slices[m[5]] = (m[6] in me.helper) ? me.helper[m[6]] : m[6];
}
else {
slices[m[5]] = parseFloat(m[6]);
}
}
m = store.slicesRE.exec(text);
}
return slices;
},
rdSettings: function (cName, offline) {
var settings = {};
var text;
var offName, title = cName;
var section = null;
var pos = title.indexOf(config.textPrimitives.sectionSeparator);
if (pos != -1) {
section = title.substr(pos + config.textPrimitives.sectionSeparator.length);
title = title.substr(0, pos);
}
offName = (title) ? title + '##Offline' : tiddler.title + '##Offline';
cName = (title) ? cName : tiddler.title + cName;
title = (title) ? title : tiddler.title;
if (store.tiddlerExists(title)) {
// read Offline config if offline
if (offline) {text = store.getTiddlerText(offName);}
// if there is no offline section use the defaults section
text = (text) ? text : store.getTiddlerText(cName);
settings = me.calcTextSlices(text);
}
// some special handling, due to jQuery.fancybox() library structure.
if (settings.swf) {
settings.swf = $.parseJSON(settings.swf);
}
if (settings.href) {
settings.href = settings.href.replace(/youtube.com\/watch\?v=/i, 'youtube.com/v/');
}
var p = ['onComplete', 'onStart', 'onCancel', 'onCleanup', 'onClosed', 'titleFormat'];
var cmfa = config.macros.fancyBox.addOns;
var x;
for (var i = 0, im = p.length; i<im; i += 1) {
x = settings[p[i]];
if (x) {
settings[p[i]] = (cmfa && cmfa[x]) ? cmfa[x] : null;
}
}
return settings;
},
// creates a fancybox friendly DOM structure
thumbList: function (list, data) {
var label, slide, elem, thumbURI, picURI;
var relId = '';
var cma = (config.macros.attach) ? config.macros.attach : null;
data.selector = '#' + data.genIdA + ' a.imageLink';
// createTiddlyElement(parent, element, id, className, text, attribs)
var listElem = createTiddlyElement(place, 'span', data.genIdA, 'imgStack');
if (data.hide) {
$(listElem).hide();
}
if (data.mode != 'picture') {
relId = data.genIdA;
}
var i, im;
// if picHost is not defined check for info from attachment
if (!data.picHost && cma.getAttachment) {
for (i = 0, im = list.length; i < im; i += 1) {
slide = store.getTiddlerText(list[i].title + '##slide');
// check for slide field
if (!slide) {
slide = (list[i].fields.slide)? list[i].fields.slide : '';
}
thumbURI = me.getPictureInfo(list[i].title, data, 'thumbHost');
picURI = (data.conf.href) ? data.conf.href : (slide) ? cma.getAttachment(slide.trim()) : thumbURI;
label = (data.list[i].label) ? data.list[i].label : '';
elem = this.createElement(data.tag, thumbURI, picURI, label, relId, label);
jQuery(elem).appendTo($(listElem)[0]);
}
}
else {
for (i = 0, im = list.length; i < im; i += 1) {
thumbURI = (data.thumbHost) ? data.thumbHost + encodeURIComponent(list[i].title) : encodeURIComponent(list[i].title);
picURI = (data.conf.href) ? data.conf.href : (data.picHost) ? data.picHost + encodeURIComponent(list[i].title) : encodeURIComponent(list[i].title);
label = (data.list[i].label) ? data.list[i].label : '';
elem = this.createElement(data.tag, thumbURI, picURI, label, relId, label);
jQuery(elem).appendTo($(listElem)[0]); }
}
// return data;
},
// click Event
buttonSlideShow: function () {
var data = $(this).data('data');
var index = data.conf.index || 0;
var elems;
elems = me.activateBox(data);
if (elems.length > index) {
$(elems[index]).trigger('click');
}
},
activateBox: function (data) {
$(data.selector).each(function (index, element) {
// if (! $(this).attr('title')) {
$(this).attr('title', $(element).children('img').attr('title'));
// }
});
if (['picture'].contains(data.mode)) {
return jQuery(data.selector).fancybox(data.conf);
}
else {
// mode: 'slide', 'selector',
return jQuery(data.selector).attr('rel', data.genIdA).fancybox(data.conf);
}
},
// activate Box
idGenerator: function () {
var date = new Date();
var random = Math.floor(Math.random() * 1000);
return Crypto.hexSha1Str(date.getTime() + '@' + document.location.href + '-' + random);
},
rdSlideInfo: function (list, data){
var co = config.options;
var fbLabel, fbInfo = '';
var lit, tmpLabel;
var titleRegexp;
var match;
for ( var i=0; i<list.length; i += 1 ) {
fbLabel = '';
// using the tiddler title as slideshow title is default behaviour
// If one doesn't want it set useTiddlerTitle to false
// cutExtension is default true
lit = list[i].title;
if (data.useTiddlerTitle) {
titleRegexp = /^(.*)\./mg; // ToDo check why it needs new init ?!?!
match = titleRegexp.exec(lit);
tmpLabel = (data.cutExtension && (match!==null)) ? match[1] : lit;
}
if (data.labelSection) {
fbLabel = store.getTiddlerText(list[i].title + '##' + data.labelSection);
fbLabel = (fbLabel) ? fbLabel : '';
if (!fbLabel) {
fbLabel = (list[i].fields[data.labelSection])? list[i].fields[data.labelSection] : '';
}
}
if (data.infoSection) {
fbInfo = store.getTiddlerText(lit + '##' + data.infoSection);
fbInfo = (fbInfo)? fbInfo : '';
if (!fbInfo) {
fbInfo = (list[i].fields[data.infoSection.toLowerCase()])? list[i].fields[data.infoSection.toLowerCase()] : '';
}
}
tmpLabel = (data.list[i].label) ? data.list[i].label : (fbLabel) ? fbLabel : tmpLabel;
data.list[i].label = tmpLabel;
data.list[i].fbInfo = fbInfo;
} // for
},
defaults: {
display: 'slide', // slide or picture
useTiddlerTitle: true,
cutExtension: true,
sortField: 'title',
labelSection: 'label',
infoSection: 'info'
},
handler: function (place, macroName, params, wikifier, paramString, tiddler) {
params = paramString.parseParams('pictureLink', null, true);
var btn;
var conf = {}; // everything, that comes from Defaults or Fancy sections
var data = {}; // data that will be used to decide
data = merge(data, me.defaults);
data.list= []; // label information
// pictureLink catches all unnamed params. It can be an array.
var pictureLink = getParam(params, 'pictureLink', undefined);
if (pictureLink) {
data.src = 'pictureLink';
data.display = 'pictureLink';
data.tag = 'pictureLink';
pictureLink = params[0].pictureLink;
if (pictureLink.length > 1) {
data.display = 'pictureLinkSlide'; // creates a slideshow
}
}
// if several pics are called several labels are needed.
// label is used, because title is used by fancyBox library allready.
var label = getParam(params, 'label', undefined);
if (label) {
label = params[0].label;
}
// paramContainer contains: tiddlerName, tiddlerName##sectionName or ##sectionName
// set defaults like: FancyBoxConfig
var configId = getParam(params, 'defaults', undefined);
if (configId) {
// get alternative setting if TW is offline.
conf = me.rdSettings(configId, (document.location.protocol === "file:"));
}
configId = getParam(params, 'fancy', undefined);
if (configId) {
var tmp = me.rdSettings(configId);
conf = merge(conf, tmp);
}
me.setFbAdvanced(conf, params);
data.conf = conf;
// params that set the data.src
var sourceSetters = ['tag', 'pictureLink'];
// params that set the data.display
// 'mode' is not a mode setter. It belongs to optionalParams
var displaySetters = ['tag', 'imageButton', 'button', 'imageStack', 'selector'];
// these are optional params which are used by FancyBoxPlugin.
pc = ['useTiddlerTitle', 'cutExtension', 'selector',
'thumbHost', 'picHost', 'labelSection', 'infoSection',
'tag', 'imageButton', 'imageStack', 'button', 'mode', 'sortField', 'rbLink'];
var setDisplay, setSrc;
for (i=0, im=pc.length; i<im; i += 1) {
content = getParam(params, pc[i], '');
content = (content in me.helper) ? me.helper : content; // chk: true, false, null
setDisplay = false;
setSrc = false;
if (content) {
data[pc[i]] = content;
setDisplay = displaySetters.contains(pc[i]);
setSrc = sourceSetters.contains(pc[i]);
}
else if (data.conf[pc[i]]) {
data[pc[i]] = data.conf[pc[i]];
setDisplay = displaySetters.contains(pc[i]);
setSrc = sourceSetters.contains(pc[i]);
}
// don't modify them if not needed.
if (setDisplay) {data.display = pc[i];}
if (setSrc) {data.src = pc[i];}
} // for
var genId = me.idGenerator();
data.genIdA = 'A' + genId.substr(1, 5);
var tlist, tid;
switch (data.src) {
case 'tag':
tlist = store.getTaggedTiddlers(data.tag, data.sortField);
for (i = 0; i < tlist.length; i += 1) {
data.list[i] = {'label':''};
}
me.rdSlideInfo( tlist, data);
break; // 'tag'
case 'pictureLink':
tlist = [];
for (i = 0; i < pictureLink.length; i += 1) {
tid = store.getTiddler( pictureLink[i]);
if (tid) {
tlist.push(tid);
data.list[i] = {'label': (label && label[i]) ? label[i] : ''};
} // if (tid) ..
} // for ..
me.rdSlideInfo(tlist,data);
break; // 'tag'
}
// console.log({'data':data});
switch (data.display) {
case 'imageStack':
if ($.browser.mozilla || $.browser.webkit) {place = me.createStack();}
// fall through is by intention
case 'imageButton':
data.hide = true; // hide the DOM structure
data.tag = data.tag || 'button';
me.thumbList(tlist, data);
// createTiddlyButton(parent, text, tooltip, action, className, id, accessKey, attribs)
btn = createTiddlyButton(place, '', null, me.buttonSlideShow);
$(btn).removeClass('button');
wikify(data.imageButton || data.imageStack, btn);
$(btn).data('data', data);
return;
// break; // button
case 'button':
// check dependencies ToDo
data.tag = data.tag || 'button';
data.hide = true;
me.thumbList(tlist, data);
// createTiddlyButton(parent, text, tooltip, action, className, id, accessKey, attribs)
btn = createTiddlyButton(place, data.button, null, me.buttonSlideShow);
$(btn).data('data', data);
return;
// break; // button
case 'tag':
me.thumbList(tlist, data);
data.selector = '.twfb-' + data.tag + ' a';
me.activateBox(data);
break; // tag
case 'pictureLink':
case 'pictureLinkSlide':
me.thumbList(tlist, data);
data.selector = '#' + data.genIdA + ' a.imageLink';
me.activateBox(data);
break; // tag
case 'selector':
me.activateBox(data);
break; // xxx
// case 'slide':
default:
// limit selection to containing tiddler, if there is no selector param
// 'a.imageLink' is default produced by [img[picture.jpg][http:// .. ]]
// imageMacro is different ???!!! ToDo
var container = story.findContainingTiddler(place);
$(container).find('.viewer').attr('id', data.genIdA);
data.selector = '#' + data.genIdA + ' a.imageLink';
me.activateBox(data);
break; // xxx
} // switch
} // handler
}; // end plugin
})(jQuery);
//}}}