Repository navigation
Expand file tree
/
Copy pathdynamicContent.js
More file actions
161 lines (155 loc) · 5.21 KB
/
Copy pathdynamicContent.js
File metadata and controls
161 lines (155 loc) · 5.21 KB
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
const URL = 'https://arenanodeapp.herokuapp.com/data';
// const url = 'http://localhost:5000/data';
const ARENA_URL = 'https://are.na/';
/**CONST to navigate the different part of the arean JSON**/
const BLOCK = 'Block';
const CHANNEL = 'Channel';
const MEDIA = 'Media';
const LINK = 'Link';
const ATTACHMENT = 'Attachment';
const IMAGE = 'Image';
const TEXT = 'Text';
/*************************************/
let json;
let isGrid = true;
let divPosition = [];
// let arena_index = 0;
$.get(URL, data => {
json = data;
createContent(data);
});
/**
* this function creates the dynamic content
* @param {Array} data Array of JSON files
*/
function createContent(data) {
console.log(data);
for (const channel of data) {
const contents = channel.contents;
const COLOR = createColor();
//sort the content by date
contents.sort((a, b) => {
let dateA = new Date(a.created_at), dateB = new Date(b.created_at);
// console.log(dateA, dateB);
return dateB - dateA;
});
// console.log(contents.created_at);
//we go trough all the elements of the json file
let i = 0;
for (let content of contents) {
// here we get the bounds of the container div
// and we set the position of the div randomly
let containerBounds = BoundsById('myContainer');
divPosition.push({
top: (Math.random() * (containerBounds.height - 400)),
left: (Math.random() * (containerBounds.width - 400))
});
//here we generate the div and we attach html and position to it
let d = document.createElement('div');
$(d).addClass('inspirationContent')
.css("background-image", "url(" + imgUrl(content) + ")")//" /*+ url + */"
.css("background-color", COLOR)
.html(htmlContent(content))
// .offset(divPosition[i])
.appendTo($('#dynamicContent')) //main div
i++;
}
}
}
/**
* this function returns the link to an image
* it handles various exception putting a plceholder
* image in the case the JSON doesn't provides a link to one
* @param {JSON} data JSON file with the data
* @returns the link to an image
*/
function imgUrl(data) {
// first we check if it is a 'Block' content or 'Channel'
if (data.base_class.startsWith(BLOCK)) {
// does i have an image
if (data.image != null) {
return data.image.thumb.url;//if yes we return the link
} else {
return 'img/no.png';// otherwise a placeholder image
}
} else if (data.base_class.startsWith(CHANNEL)) {
// in the case of a channel we return the user avatar image
return data.user.avatar_image.thumb;
} else return 'img/no.png';// if it is none of the above we put a placeholder image
}
/**
* this function returns the link the source of the arena content
* it handles various exception
* @param {JSON} data JSON file with the data
* @returns HTML <a> tag with link to the source
*/
function htmlContent(data) {
// first we check if it is a 'Block' content or 'Channel'
if (data.base_class.startsWith(BLOCK)) {
if (data.class.startsWith(TEXT)) {
// Text content should link to the arena original page
let link = ARENA_URL + 'block/' + data.id;
return aTag(link, data.title);
} else if (data.class.startsWith(MEDIA) || data.class.startsWith(LINK) || data.class.startsWith(IMAGE)) {
// Media, Link and Image can be handled the same way
// returning the source url
return aTag(data.source.url, data.title);
} else if (data.class.startsWith(ATTACHMENT)) {
// Link needs to return the attachment link (amazon link)
let link = data.attachment.url;
return aTag(link, data.title);
} else return data.title;
} else if (data.base_class.startsWith(CHANNEL)) {
// Channel returns the url of the original channel
let link = ARENA_URL + data.user.slug + '/' + data.slug;
return aTag(link, data.title);
} else return 'FAIL';
}
function aTag(link, text) {
const d = document.createElement('div')
d.setAttribute('class', 'content')
const a = document.createElement('a')
a.setAttribute('href', link);
a.innerText = text;
d.appendChild(a);
return d
// console.log(a);
// return ' <a href="' + link + '" target="_blank">' + text + '</a>';
}
function HTMLtitle(title) {
return '<a>' + title + '</a>';
}
function imgTag(str) {
return ' <img src="' + str + '">';
}
function grid() {
let content = document.getElementById('content');
let myClass = document.getElementsByClassName('inspirationContent');
let temp = myClass;
// console.log(temp);
let i = 0;
if (!isGrid) {
for (let el of myClass) {
el.style.top = '0px';
el.style.left = '0px';
el.style.position = 'relative';
el.style.float = 'left';
}
} else {
for (let el of myClass) {
let containerBounds = BoundsById('myContainer');
el.style.top = divPosition[i].top + 'px';
el.style.left = divPosition[i].left + 'px';
el.style.position = 'absolute';
el.style.float = 'none';
i++;
}
}
isGrid = !isGrid;
}
function createColor(){
let r = Math.floor((Math.random() * 2.55) * 100);
let g = Math.floor((Math.random() * 2.55) * 100);
let b = Math.floor((Math.random() * 2.55) * 100);
return "rgba(" + r + "," + g + ", " + b + ", "+ 0.7 + ")";
}