-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathsamples.js
More file actions
159 lines (128 loc) · 7.1 KB
/
Copy pathsamples.js
File metadata and controls
159 lines (128 loc) · 7.1 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
var data = [];
async function Start() {
data = await $.getJSON('/samples.json').fail(function () { showFailed(); });
showSearchFromUrl();
openSampleFromUrl();
}
// Show faild loading json file
function showFailed() {
$("#sampleList").empty();
$("#sampleList").append('<div class="w-100"><div class="alert alert-danger" role="alert">Sorry, was unable to load the samples from \'<strong>/samples.json</strong>\' file.</div></div>');
}
// Show all samples and categories
function showSampleCards() {
var search = $.QueryString.search;
if (!search) {
$('#sampleList').empty();
$.each(data.Categories, function (index, value) {
$('#sampleList').append(`<div class="w-100"><a name="${value.Id}"></a><h3 class="fw-light">${value.Title} <small class="text-muted">(${value.NumberOfSamples})</small></h3><p>${value.Description}</p></div>`);
$.each(value.Samples, function (index, value) {
$('#sampleList').append(`<div class="col"><div class="card shadow-sm"><img class="card-img-top" src="${value.Path}/${value.Screenshot}" loading="lazy" alt="${value.Title}" /><div class="card-body"><h5 class="card-title">${value.Title}</h5><p class="card-text">${value.Description}</p><div class="d-flex justify-content-between align-items-center"><div class="btn-group"><button type="button" class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal" aria-describedby="Run the sample in a new popup" data-bs-target="#sampleModal" data-bs-id="${value.Id}" data-bs-title="${value.Title}" data-bs-path="${value.Path}" data-bs-source="${data.GitHub}${value.Source}"><small>Run Sample</small></button><a href="${value.Path}" target="_blank" class="btn btn-sm btn-outline-secondary" role="button" aria-describedby="Open sample in a new Tab"><small>Open In New Tab</small></a><a href="${data.GitHub}${value.Source}" target="_blank" class="btn btn-sm btn-outline-secondary" role="button" aria-describedby="Show the Source Code"><small>Source Code</small></a></div></div></div></div></div>`);
});
});
}
}
// Show sample search result from url if any
function showSearchFromUrl() {
var search = $.QueryString.search;
if (search) {
search = search.replace(/[^a-zA-Z0-9 ]/g, '');
$('#searchBox').val(search);
$('#sampleList').empty();
$('#sampleList').append(`<div class="w-100"><h3 class="fw-light">Search results for <small class="text-muted">${search}</small></h3></div>`);
var query = search.toLowerCase();
var found = false;
var count = 0;
$.each(data.Categories, function (index, value) {
$.each(value.Samples, function (index, value) {
if (value.Title.toLowerCase().indexOf(query) >= 0 ||
value.Description.toLowerCase().indexOf(query) >= 0 ||
value.Keywords.toLowerCase().indexOf(query) >= 0) {
$('#sampleList').append(`<div class="col"><div class="card shadow-sm"><img class="card-img-top" src="${value.Path}/${value.Screenshot}" loading="lazy" alt="${value.Title}" /><div class="card-body"><h5 class="card-title">${value.Title}</h5><p class="card-text">${value.Description}</p><div class="d-flex justify-content-between align-items-center"><div class="btn-group"><button type="button" class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#sampleModal" data-bs-id="${value.Id}" data-bs-title="${value.Title}" data-bs-path="${value.Path}" data-bs-source="${data.GitHub}${value.Source}")"><small>Run Sample</small></button><a href="${value.Path}" target="_blank" class="btn btn-sm btn-outline-secondary" role="button"><small>Open In New Tab</small></a><a href="${data.GitHub}${value.Source}" target="_blank" class="btn btn-sm btn-outline-secondary" role="button"><small>Source Code</small></a></div></div></div></div></div>`);
found = true;
count += 1;
}
});
});
if (!found) $("#sampleList").append('<div class="w-100"><div class="alert alert-warning" role="alert">Sorry, no samples matching your search criteria were found.</div></div>');
$('#resultsCount').html(count > 0 ? `<p>${count} result${count === 1 ? ' is' : 's are'} available. Use the tab key to navigate.</p>`: '');
}
}
// Open sample from Url if any
function openSampleFromUrl() {
var sample = $.QueryString.sample;
if (sample) {
var found = false;
$.each(data.Categories, function (index, value) {
$.each(value.Samples, function (index, value) {
if (value.Id === sample) {
$('.modal-title').text(value.Title);
$('#sampleModalSource').attr('href', `${data.GitHub}${value.Source}`);
$('#sampleModalPath').attr('src', value.Path);
found = true;
}
});
});
if (found) $("#sampleModal").modal('show');
}
}
// handel opening sample model
$('#sampleModal').on('show.bs.modal', function (event) {
var button = event.relatedTarget;
if (button) {
$('.modal-title').text(button.getAttribute('data-bs-title'));
$('#sampleModalSource').attr('href', button.getAttribute('data-bs-source'));
$('#sampleModalPath').attr('src', button.getAttribute('data-bs-path'));
$.QueryString.sample = button.getAttribute('data-bs-id');
history.replaceState({}, '', "?" + $.param($.QueryString));
}
});
// handel closing sample model
$('#sampleModal').on('hide.bs.modal', function () {
$.QueryString.sample = '';
history.replaceState({}, '', "?" + $.param($.QueryString));
});
// handel search input
$('input[type=search]').on('input', function () {
clearTimeout(this.delay);
this.delay = setTimeout(function () {
$(this).trigger('search');
}.bind(this), 800);
}).on('search', function () {
var search = $(this).val().trim();
if (search) {
$.QueryString.search = search;
history.replaceState({}, '', "?" + $.param($.QueryString));
showSearchFromUrl();
} else {
$.QueryString.search = '';
history.replaceState({}, '', "?" + $.param($.QueryString));
showSampleCards();
}
});
// handel search button
$('form').submit(function (event) {
var search = $('input[type=search]').val().trim();
if (search) {
$('input[type=search]').trigger('search');
}
event.preventDefault();
});
// Change style inside the iframe
$('#sampleModalPath').on('load', function () {
$(this).contents().find("body").css("font-family", "Segoe UI, Roboto, Helvetica Neue, Arial, sans-serif");
$(this).contents().find("body").css("font-size", "14px");
});
(function ($) {
$.QueryString = (function (paramsArray) {
let params = {};
for (let i = 0; i < paramsArray.length; ++i) {
let param = paramsArray[i]
.split('=', 2);
if (param.length !== 2)
continue;
params[param[0]] = decodeURIComponent(param[1].replace(/\+/g, " "));
}
return params;
})(window.location.search.substr(1).split('&'))
})(jQuery);