forked from contentstack/contentstack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom.min.js
More file actions
100 lines (89 loc) · 3.9 KB
/
custom.min.js
File metadata and controls
100 lines (89 loc) · 3.9 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
(function() {
if (!Contentstack) {
alert('Please add Contentstack Library');
} else {
var clearAll = document.getElementById('all');
var clearByContentType = document.getElementById('content_type');
var clearByQuery = document.getElementById('query');
var wrapper, Stack;
function DOMCreation(entry, html) {
if (entry) {
html += '<div class="panel panel-default"><div class="panel-heading" role="tab"><h4 class="panel-title"><a role="button" data-toggle="collapse" data-parent="#accordion" href="#' + entry.uid + '" aria-expanded="true" aria-controls="' + entry.uid + '">';
html += entry.title + '</a></h4></div><div id="' + entry.uid + '" class="panel-collapse collapse" role="tabpanel"><div class="panel-body">';
html += entry.url + ' </div></div></div>';
}
return html;
}
function singleEntry(contentTypeUid, entryUid) {
var contentTypeUid = contentTypeUid || "news"
var entryUid = entryUid || "news_uid"
Stack
.ContentType(contentTypeUid)
.Entry(entryUid)
.fetch()
.then(function(data) {
// result object with entry
var html = '<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">';
html = DOMCreation(data.toJSON(), html);
html += '</div>';
wrapper.innerHTML = html;
}, function(err) {
console.info('News could not fetched.');
});
}
function allEntries(contentTypeUid) {
var contentTypeUid = contentTypeUid || "news";
Stack
.ContentType(contentTypeUid)
.Query()
.find()
.then(function(data) {
if (data && data.length && data[0].length) {
var html = '<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">';
for (var i = 0, _i = data[0].length; i < _i; i++) {
html = DOMCreation(data[0][i].toJSON(), html);
}
html += '</div>';
wrapper.innerHTML = html;
} else {
console.info('News could not fetched.');
}
}, function(err) {
console.info('Error : ' + err);
});
}
function getAsset() {
Stack
.Assets()
.Query()
.toJSON()
.find()
.spread(function(result) {
console.log("Result: ", result);
// result object with entry
console.info("Result2 : ", result)
for (let i = 0, _i = result.length; i < _i; i++) {
// Image optimization
const imgUrl = Stack.imageTransform(result[i]['url'], {
quality: 50,
format: 'jpg'
})
console.log("Image URL : ", imgUrl)
}
}, function(err) {
// body...
console.info("Error: " + err);
});
}
window.onload = function() {
wrapper = document.getElementById('wrapper')
Stack = Contentstack.Stack({ 'api_key': 'blt123something', 'access_token': 'blt123something', 'environment': 'mobile' })
// get single the entry
// singleEntry("source", "blt123something")
// get all the entries
allEntries("blogs")
// get all the assets
getAsset();
}
}
}());