Skip to content

Commit 40805d8

Browse files
committed
Change plugin page to work with multiple versions, and more
Allows for the newest version to show next to the name, but displays all available versions (if more than one). Shows the specific requirements next to each version, and displays it more properlly, instead of a stringified object. Fixes #7
1 parent 22df8f7 commit 40805d8

1 file changed

Lines changed: 63 additions & 1 deletion

File tree

micro_files/plugin-search.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,69 @@ function showResults(results, collapsed) {
6161
if (!collapsed) {
6262
collapseStr += " in";
6363
}
64+
65+
function separate_with_commas(tar_array) {
66+
let results = "";
67+
// Break apart tags with a comma and space
68+
for (let i = 0; i < tar_array.length; i++) {
69+
results += tar_array[i];
70+
if ((i + 1) < tar_array.length) {
71+
results += ', ';
72+
}
73+
}
74+
return results;
75+
}
76+
6477
results.forEach(function (item) {
65-
table.innerHTML += '<div class="panel panel-default">\n <div class="panel-heading" role="tab" id="h-' + item.Name + '">\n <h4 class="panel-title">\n <a role="button" data-toggle="collapse" data-parent="#results"\n href="#c-' + item.Name + '" aria-controls="c-' + item.Name + '">\n ' + item.Name + ' ' + item.Versions[0].Version + '\n </a>\n </h4>\n </div>\n <div id="c-' + item.Name + '" class="' + collapseStr + '" role="tabpanel" aria-labelledby="h-' + item.Name + '">\n <div class="panel-body">\n <p>' + item.Description + '</p>\n <p><span class="glyphicon glyphicon-tags" aria-hidden="true"></span> ' + item.Tags.toString() + '<p>\n <p><b>Require : </b>' + JSON.stringify(item.Versions[0].Require) + '</p>\n <p>To install this plugin, open micro from your CLI,\n press [Crtl + E] then run the command line below.\n Once you are done, restart micro.</p>\n <div class="well">&gt; plugin install ' + item.Name + '</div>\n </div>\n </div>\n </div>';
78+
// The displayed tag(s)
79+
let display_item_tag = separate_with_commas(item.Tags);
80+
// The displayed version(s)
81+
let display_item_allversions = '<p><b>Available versions: </b>';
82+
// The displayed require(s) list/version(s)
83+
let display_item_require = '';
84+
let current_requires = [];
85+
let requires_len = 0;
86+
87+
// Handle multiple versions in repo.json
88+
for (let i = 0; i < item.Versions.length; i++) {
89+
// Create a string of all versions other than the newest one
90+
// Append the versions into a string
91+
display_item_allversions += item.Versions[i].Version;
92+
// Save the len so we don't have to recalculate
93+
requires_len = Object.keys(item.Versions[i].Require).length;
94+
// Go through each Require obj, turning them to strings, and separate multiples with commas
95+
for (let x = 0; x < requires_len; x++) {
96+
// Gets an array of the object's key and value
97+
current_requires[x] = Object.entries(item.Versions[i].Require)[x];
98+
// Because of how Micro's repo.json is formatted, there's no space between the [<>=] and the version
99+
// This adds a space to be more visually appealing
100+
current_requires[x][1] = current_requires[x][1].match(RegExp('[<>=]+', 'g')) + ' ' + current_requires[x][1].match(RegExp('[^<>=]+', 'g'));
101+
// NOTE: I believe the require tag will always be 2 values per object - the requirement & the value
102+
// If that's not right, this'll need to be changed
103+
current_requires[x] = current_requires[x][0] + ' ' + current_requires[x][1];
104+
// Instead of running separate_with_commas, do it here to avoid another needless loop
105+
if ((x + 1) < requires_len) {
106+
current_requires[x] += ', ';
107+
}
108+
}
109+
// Append the requires into a string
110+
display_item_require += '</p>\n <p><b>' + item.Versions[i].Version + ' requires : </b>' + current_requires;
111+
// If not on the last version, add a comma
112+
if ((i + 1) < item.Versions.length) {
113+
display_item_allversions += ', ';
114+
// Linebreak so we display them below eachother
115+
display_item_require += '\n';
116+
}
117+
}
118+
119+
// If there's only a single version, don't show the "All versions: " thing
120+
if (item.Versions.length == 1) {
121+
display_item_allversions = '';
122+
} else {
123+
// If there are multiple, we close off the paragraph tag we opened in the declaration
124+
display_item_allversions += '</p>';
125+
}
126+
127+
table.innerHTML += '<div class="panel panel-default">\n <div class="panel-heading" role="tab" id="h-' + item.Name + '">\n <h4 class="panel-title">\n <a role="button" data-toggle="collapse" data-parent="#results"\n href="#c-' + item.Name + '" aria-controls="c-' + item.Name + '">\n ' + item.Name + ' ' + item.Versions[item.Versions.length - 1].Version + '\n </a>\n </h4>\n </div>\n <div id="c-' + item.Name + '" class="' + collapseStr + '" role="tabpanel" aria-labelledby="h-' + item.Name + '">\n <div class="panel-body">\n <p>' + item.Description + '</p>\n <p><span class="glyphicon glyphicon-tags" aria-hidden="true"></span> ' + display_item_tag + display_item_allversions + display_item_require + '</p>\n <p>To install this plugin, open micro from your CLI,\n press [Crtl + E] then run the command line below.\n Once you are done, restart micro.</p>\n <div class="well">&gt; plugin install ' + item.Name + '</div>\n </div>\n </div>\n </div>';
66128
});
67129
}

0 commit comments

Comments
 (0)