Skip to content

Instantly share code, notes, and snippets.

@luv2code
Last active December 31, 2015 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luv2code/7991595 to your computer and use it in GitHub Desktop.
Save luv2code/7991595 to your computer and use it in GitHub Desktop.
changed the logic to show the more button from using a count to using an api provided flag.
(function () {
var activeTabClass = "yj-active-tab";
var eventsLink;
var lastId;
var column = yam.$('#column-two');
var mainColumn = column.find('.yj-main-content').empty();
var eventList = yam.$("<div id='event-list'>").appendTo(mainColumn);
eventList.click(function () { eventList.empty(); }); //empty the content if an event item is clicked.
var loading = yam.$('<div id="notificationMenuSpinner" class="yj-spinner-loading-container"><div class="yj-spinner-loading">Loading...</div></div>').appendTo(mainColumn);
var moreButtonText = "<a id='moreButton' href='javascript:;'><h2>more</h2></a>";
function fetchResults() {
var url = 'https://www.yammer.com/api/v1/messages/attachments/3.json?filter=event&threaded=true&sort_by=created_at&reverse=true';
if(lastId) {
url = url + '&older_than=' + lastId;
}
loading.show();
mainColumn.find('#moreButton').remove();
//fetch results
yam.$.ajax({
url: url,
success: function (msgs) {
if(!msgs.messages) {
return;
}
var promises = [];
msgs.messages.forEach(function (msg) {
promises.push(yam.$.get(msg.attachments[0].inline_url).done(function (text) {
msg.rendered_content = text;
}));
});
yam.$.when.apply(yam.$, promises).done(function () {
msgs.messages.forEach(function (msg) {
eventList.append(msg.rendered_content);
eventList.append('<br />');
});
if(msgs.meta.older_available) {
mainColumn.append(moreButtonText).click(fetchResults);
}
loading.hide();
});
lastId = msgs.messages[msgs.messages.length - 1].id;
console.log("message count", msgs.messages.length);
},
failure: function () {
console.log(arguments);
alert("error retrieving events. see console for details");
}
});
}
if(!yam.$('#events-tab').length) {
eventsLink = yam.$('<li class="yj-header-menu-item yj-header-list-section-border" id="events-tab" data-yj-tabname="events">' +
'<a href="#/events" class="link has-left-border has-right-border" data-action="events" title="Events">' +
'<span class="yamicon yamicon-yam-spittle yamicon-regular yj-header-img"></span>' +
'<span class="text">Events</span>' +
'</a>' +
'</li>').appendTo('#header-jewels');
yam.$('#header-jewels').click(function (ev) {
//clean up content if user switches
if(yam.$(ev.target).parents('#events-tab').length === 0) {
mainColumn.empty();
eventList.empty();
lastId = null;
}
});
//set active tab
yam.$('#header-jewels').children().removeClass(activeTabClass);
eventsLink.addClass(activeTabClass);
eventsLink.click(function () {
fetchResults();
});
//prepare column content
column.find('.page-header-container').html('<div class="page-header-container"><div class="page-header" style="display: block;"><div><h2 class="header-content">Events</h2></div></div></div></div>');
fetchResults();
} else {
yam.$('#events-tab').remove();
loading.remove();
}
})();
/*
{
"id": 344134817,
"sender_id": 1507340463,
"replied_to_id": null,
"created_at": "2013/11/22 05:40:59 +0000",
"network_id": 10580,
"message_type": "update",
"sender_type": "user",
"url": "https://www.yammer.com/api/v1/messages/344134817",
"web_url": "https://www.yammer.com/monex.co.jp/messages/344134817",
"group_id": 3243699,
"body": {
"parsed": "(See attached Event)",
"plain": "",
"rich": "(See attached Event)"
},
"thread_id": 344134817,
"client_type": "Web",
"client_url": "https://www.yammer.com/",
"system_message": false,
"direct_message": false,
"chat_client_sequence": null,
"content_excerpt": "",
"language": "en",
"notified_user_ids": [],
"privacy": "public",
"attachments": [
{
"id": 11651849,
"type": "ymodule",
"name": "Events: Bo-Nen-Kai 2013, Chicago",
"url": "https://www.yammer.com/api/v1/ymodules/11651849",
"web_url": "https://www.yammer.com/monex.co.jp/messages/344134817",
"inline_url": "https://www.yammer.com/monex.co.jp/ymodule/11651849/inline",
"ymodule": {
"app_id": "events",
"icon_url": "https://mug0.assets-yammer.com/mugshot/images/16x16/3rd_party.png"
}
}
],
"liked_by": {
"count": 0,
"names": []
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment