This repository has been archived by the owner on Dec 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomtopic.js
111 lines (106 loc) · 3.21 KB
/
randomtopic.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
// -*- coding: utf-8 -*-
// Random Topics
// version 0.2
/*
// example
$(function(){
$("div.fluits").randomTopic({
maxTopics:3,
cssClassFirst:'first',
intervalCount:3
});
});
*/
(function($){
$.fn.randomTopic = function(options) {
var rndTopic = {
maxTopics : 6, // Max topics (標準値: 6)
cssClassOdd: '', // 表示する奇数番目の Topic に追加するクラス
cssClassEven: '', // 表示する偶数番目の Topic に追加するクラス
cssClassFirst: '', // 各繰り返しの最初の Topic に追加するクラス
intervalCount: 0, // 繰り返しの数
fixClass : 'fix', // 上位に固定表示されるクラス
rndClass : 'entry', // ランダム表示されるクラス
shuffle: function(arr) {
for(var i=0; i<arr.length; i++) {
var swap = this.getRndMax(arr.length);
if(i!=swap) {
var tmp = arr[i];
arr[i] = arr[swap];
arr[swap] = tmp;
}
}
return arr;
},
getRndMax: function(max) {
var num = 100;
while(num < max * 10) { num *= 10; }
return Math.floor((Math.random() * num)) % max;
}
};
if(options) {
if(options.fixClass) {
rndTopic.fixClass = options.fixClass;
}
if(options.rndClass) {
rndTopic.rndClass = options.rndClass;
}
if(!isNaN(options.maxTopics) && options.maxTopics > 0) {
rndTopic.maxTopics = options.maxTopics;
}
if(options.cssClassOdd) {
rndTopic.cssClassOdd = options.cssClassOdd;
}
if(options.cssClassEven) {
rndTopic.cssClassEven = options.cssClassEven;
}
if(options.cssClassFirst) {
rndTopic.cssClassFirst = options.cssClassFirst;
}
if(!isNaN(options.intervalCount) && options.intervalCount > 0) {
rndTopic.intervalCount = options.intervalCount;
}
}
return this.each(function() {
var topics = $('.' + rndTopic.fixClass + ',.' + rndTopic.rndClass, $(this));
topics.remove();
var arrFix = [];
var arr = [];
for(var tIdx=0; tIdx<topics.length; tIdx++) {
var t = $(topics[tIdx]);
if(t.hasClass(rndTopic.fixClass)) {
arrFix.push(t);
} else {
arr.push(t);
}
}
var arrArrange = arrFix.concat(rndTopic.shuffle(arr));
var addClass = function (elem, cls) {
if(cls) { elem.addClass(cls); }
};
var rmClass = function (elem, cls) {
if(cls) { elem.removeClass(cls); }
};
for(var i=0; i<rndTopic.maxTopics; i++) {
if(i < arrArrange.length) {
var t = arrArrange[i];
if(i%2 == 0) {
addClass(t, rndTopic.cssClassOdd);
rmClass(t, rndTopic.cssClassEven);
} else {
addClass(t, rndTopic.cssClassEven);
rmClass(t, rndTopic.cssClassOdd);
}
if(rndTopic.intervalCount > 0) {
if(i%rndTopic.intervalCount == 0) {
addClass(t, rndTopic.cssClassFirst);
} else {
rmClass(t, rndTopic.cssClassFirst);
}
}
$(this).append(t);
}
}
});
};
})(jQuery);