Skip to content

Commit

Permalink
enhancement: sort alphabetically (#28)
Browse files Browse the repository at this point in the history
* enhancement: sort alphabetically

* code cleanup
  • Loading branch information
NenoR96 authored Apr 27, 2021
1 parent 876f506 commit d3cbd5a
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 36 deletions.
150 changes: 133 additions & 17 deletions control/content/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@
<label for="emptyStateMessageOff">Off</label>
</div>
</div>
<div class="mdc-layout-grid__inner mdc-layout-grid__cell--span-6" id="emptyStateMessageDiv" hidden>
<div class="mdc-layout-grid__inner mdc-layout-grid__cell--span-6" id="emptyStateMessageDiv"
style="display: none;">
<div class="mdc-layout-grid__cell--span-10">
<span class="mdc-layout-grid__cell--span-2 text-body-light">Text:</span>
<div class="mdc-layout-grid__cell--span-4">
Expand All @@ -282,6 +283,52 @@

</div>
</div>
<div class="mdc-layout-grid__inner block">
<span class="mdc-layout-grid__cell--span-2 text-body-light">Sort by</span>
<div class="mdc-layout-grid__cell--span-2">
<div class="mdc-form-field">
<div class="mdc-radio">
<input class="mdc-radio__native-control" type="radio" id="sortBy0" name="sortBy"
onchange="onSelectSortType(0)" checked>
<div class=" mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
<div class="mdc-radio__inner-circle"></div>
</div>
<div class="mdc-radio__ripple"></div>
</div>
<label class="radio-label" for="sortBy0">Natural order</label>
</div>
</div>
<div class="mdc-layout-grid__cell--span-2">
<div class="mdc-form-field">
<div class="mdc-radio">
<input class="mdc-radio__native-control" type="radio" id="sortBy1" name="sortBy"
onchange="onSelectSortType(1)">
<div class=" mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
<div class="mdc-radio__inner-circle"></div>
</div>
<div class="mdc-radio__ripple"></div>
</div>
<label class="radio-label" for="sortBy1">A-Z</label>
</div>
</div>
<div class="mdc-layout-grid__cell--span-2">
<div class="mdc-form-field">
<div class="mdc-radio">
<input class="mdc-radio__native-control" type="radio" id="sortBy2"
name="sortBy" onchange="onSelectSortType(2)">
<div class="mdc-radio__background">
<div class="mdc-radio__outer-circle"></div>
<div class="mdc-radio__inner-circle"></div>
</div>
<div class="mdc-radio__ripple"></div>
</div>
<label class="radio-label" for="sortBy2">Z-A</label>
</div>
</div>
</div>

</div>
<script>
Analytics.init();
Expand All @@ -293,6 +340,7 @@
function init() {
Config.get()
.then(result => {
updateRecordsForSorting();
// This way, we will keep old instances to use PSW navigate by default (so we don't break backward compatibility) and new instances to navigate to CW by default.
if (result && !Object.keys(result.data).length) result.data.navigateToCwByDefault = true;
config = new Config(result.data); querystring.value = config.querystring;
Expand Down Expand Up @@ -331,11 +379,17 @@
colorIndicator.checked = true;
}

if(config?.sortBy === Config.SORTING.ASCENDING) {
sortBy1.checked = true;
} else if(config?.sortBy === Config.SORTING.DESCENDING) {
sortBy2.checked = true;
}

if (config.emptyState) {
emptyStateMessageOn.checked = true;
blankStateMessage.value = config.emptyStateMessage;
} else {
emptyStateMessageDiv.setAttribute('style', 'visibility: hidden;')
emptyStateMessageDiv.setAttribute('style', 'display: none;')
emptyStateMessageOff.checked = true;
}
let elements = document.getElementsByClassName("tagInputContainer");
Expand All @@ -345,13 +399,13 @@
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = "block";
}
}
else {
publicWritePrivacy.checked = true;
}
else {
publicWritePrivacy.checked = true;
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = "none";
}
}
}
})
.catch(err => {
console.error(err)
Expand All @@ -362,16 +416,16 @@
let elements = document.getElementsByClassName("tagInputContainer");
if (config.writePrivacy === Config.WRITE_PRIVACY.PRIVATE) {
config.writePrivacy = Config.WRITE_PRIVACY.PUBLIC;
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = "none";
}
}
else {
config.writePrivacy = Config.WRITE_PRIVACY.PRIVATE;
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = "block";
}
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = "none";
}
}
else {
config.writePrivacy = Config.WRITE_PRIVACY.PRIVATE;
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = "block";
}
}
}

function onSelectPlugin() {
Expand All @@ -389,11 +443,11 @@

function onSelectEmptyStateMessage(bool) {
if (bool) {
emptyStateMessageDiv.setAttribute('style', 'visibility: visible;')
emptyStateMessageDiv.setAttribute('style', 'display: block;')
blankStateMessage.value = config.emptyStateMessage;
}
else if (!bool)
emptyStateMessageDiv.setAttribute('style', 'visibility: hidden;')
emptyStateMessageDiv.setAttribute('style', 'display: none;')

config.emptyState = bool;
config.save();
Expand Down Expand Up @@ -457,6 +511,68 @@
config.save();
}

function onSelectSortType(sort) {
let sortType = sort === 0 ? 'default' : sort === 1 ? 'ascending' : 'descending';
config.sortBy = sortType;
config.save();
}

function updateRecordsForSorting() {
let hasRecords = false;
update();

function updateDone() {
console.log("GOTOVO AAAAA")
buildfire.notifications.alert({
title: "Update Finished",
message: `Your feature has been successfully updated! Please PUBLISH your app to see this update on mobile devices.`,
okButton: { text: 'Ok' }
}, function (e, data) {
if (e) console.error(e);
});
}

function update() {
let pageSize = 50, page = 0, allItems = [], filter = { "$json.titleIndex": { $exists: false } }
let get = function () {
buildfire.publicData.search({ filter, pageSize, page, recordCount: true }, "topics", function (err, data) {
console.log("EEEEEEEEEEEEEEEE", data)
if (data && data.result && data.result.length) {
console.log("AAAAAAAAAAAAA", hasRecords)

buildfire.notifications.alert({
title: "Update In Progress",
message: `We have made an update to allow you to sort items alphabetically. For this update to occur successfully, please stay on this screen for 5 to 20 seconds until you receive an “Update Finished” message.`,
okButton: { text: 'Ok' }
}, () => { });

allItems = allItems.concat(data.result);
if (data.totalRecord > allItems.length) {
page++;
get();
}
else {
let count = 0;
allItems.map(item => {
item.data.titleIndex = item.data.title.toLowerCase();
console.log("RADI UPDATE", item)
buildfire.publicData.update(item.id, item.data, "topics", (err, res) => {
console.log(res)
count++;
if (count === allItems.length) {
updateDone()
console.log("PROSO UPDATE")
}
})
});
}
}
})
}
get();
}
}

</script>
</body>

Expand Down
7 changes: 7 additions & 0 deletions control/content/js/classes/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Config {
this.emptyState = dataObj.emptyState || false;
this.emptyStateMessage = dataObj.emptyStateMessage ||'Create your first group now!';
this.navigateToCwByDefault = dataObj.navigateToCwByDefault || false;
this.sortBy = dataObj.sortBy || 'default';
}

static get PRIVACY() {
Expand All @@ -33,6 +34,12 @@ class Config {
COLOR: 'color'
}
}
static get SORTING() {
return {
ASCENDING: 'ascending',
DESCENDING: 'descending'
}
}

static get() {
return new Promise((resolve, reject) => {
Expand Down
Binary file added dist.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"author": "BuildFire",
"pluginName": "Dynamic List",
"pluginName": "Dynamic List [Ned Dev]",
"pluginDescription": "Give your users a great experience. This feature gives users the ability to create community driven lists, each can be public or private. Users can create link, groups, and sub-groups each can be linked to different plug-ins such as a social wall, the possibilities are limitless.",
"supportEmail": "[email protected]",
"supportSite":"https://learn.buildfire.com/en/articles/4759761-how-to-use-the-dynamic-list-plugin",
Expand Down
66 changes: 48 additions & 18 deletions widget/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ init();
function init() {
buildfire.spinner.show();
buildfire.appearance.titlebar.show();
updateRecordsForSorting();
buildfire.appearance.getAppTheme(function (err, theme) {
appTheme = theme.colors;
document.getElementsByClassName('widgetIcon')[1].style.setProperty('color', appTheme.icons, 'important');
Expand Down Expand Up @@ -127,6 +128,30 @@ function init() {
});
}

function updateRecordsForSorting() {
let pageSize = 50, page = 0, allItems = [], filter = { "$json.titleIndex": { $exists: false } }
let get = function () {
buildfire.userData.search({ filter, pageSize, page, recordCount: true }, "topics", function (err, data) {
if (data && data.result && data.result.length) {
allItems = allItems.concat(data.result);
if (data.totalRecord > allItems.length) {
page++;
get();
}
else {
allItems.map(item => {
item.data.titleIndex = item.data.title.toLowerCase();
buildfire.userData.update(item.id, item.data, "topics", (err, res) => {
console.log(res)
})
});
}
}
})
}
get();
}

function enforceUserLogin() {
authManager.getCurrentUser()
.then(user => {
Expand Down Expand Up @@ -182,6 +207,12 @@ function loadData(filterData) {
$type: "null"
},
};

let sort = {
type: 1
}
config?.sortBy === 'ascending' ? sort.titleIndex = 1 : (config?.sortBy === 'descending' ? sort.titleIndex = -1 : null);

if (filterData && Object.keys(filterData).length > 0) {
filter = {
...filter,
Expand All @@ -197,16 +228,17 @@ function loadData(filterData) {
}
})
}
Topic.getAllTopics(filter, null, {
type: 1
})
Topic.getAllTopics(filter, null, sort)
.then(topics => {
clearList();
buildfire.spinner.hide()
if (topics && topics.length === 0) {
scrollContainer.classList.add('bitmap');
return;
}
config?.sortBy === 'ascending' ? topics.sort((a, b) =>a.data.titleIndex.localeCompare(b.data.titleIndex))
: config?.sortBy === 'descending' ? topics.sort((a, b) =>b.data.titleIndex.localeCompare(a.data.titleIndex)) : null;

for (const obj of topics) {
let topic = new Topic({
...obj.data,
Expand Down Expand Up @@ -234,9 +266,7 @@ function loadData(filterData) {
})
}
else {
Topic.getTopics(config.privacy, filter, null, {
type: 1
})
Topic.getTopics(config.privacy, filter, null, sort)
.then(topics => {
clearList();
buildfire.spinner.hide()
Expand Down Expand Up @@ -753,14 +783,14 @@ function navigateTo(topic) {
let pluginData = config && config.pluginData ? config.pluginData : null;
if (!pluginData || Object.keys(pluginData).length === 0) {
const navigateToCwByDefault = (
config && !Object.keys(config).length
?
true
:
config && config.navigateToCwByDefault
?
config.navigateToCwByDefault
:
config && !Object.keys(config).length
?
true
:
config && config.navigateToCwByDefault
?
config.navigateToCwByDefault
:
false
);
buildfire.navigation.navigateToSocialWall({
Expand Down Expand Up @@ -930,17 +960,17 @@ function shareWithOthers(data) {
if (err) {
console.error(err)
} else {
buildfire.device.share({
buildfire.device.share({
subject: link.title,
text: link.description,
image: 'http://myImageUrl',
link: result.url
}, function (err,result) {
}, function (err, result) {
if (err)
console.log(err)
console.log(err)
else
console.log(result)
});
});
}
});
}
Expand Down
Loading

0 comments on commit d3cbd5a

Please sign in to comment.