in fcm-notifications/public/main.js [80:136]
Demo.prototype.displayAllUsers = function() {
var usersRef = firebase.database().ref('users');
usersRef.on('child_added', function(snapshot) {
// Create the HTML for a user.
var photoURL = snapshot.val().photoURL;
var displayName = snapshot.val().displayName;
var uid = snapshot.key;
var userTemplate =
'<div class="demo-user-container">' +
' <img class="demo-profile-pic" src="' + photoURL + '">' +
' <span class="demo-name">' + displayName + '</span>' +
' <span class="demo-notifications-enabled">(notifications enabled)</span>' +
' <label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="demo-follow-switch-' + uid + '">' +
' <input type="checkbox" id="demo-follow-switch-' + uid + '" class="mdl-switch__input">' +
' <span class="mdl-switch__label">Follow</span>' +
' </label>' +
'</div>';
// Create the DOM element from the HTML.
var div = document.createElement('div');
div.innerHTML = userTemplate;
var userElement = div.firstChild;
this.usersContainer.appendChild(userElement);
// Activate the Material Design Lite Switch element.
var materialSwitchContainer = userElement.getElementsByClassName('mdl-switch')[0];
if (componentHandler) {
componentHandler.upgradeElement(materialSwitchContainer);
}
// Check if the user has notifications enabled and show a flag if he has.
var notificationEnabledElement = userElement.getElementsByClassName('demo-notifications-enabled')[0];
var notificationsEnabledRef = snapshot.ref.child('notificationTokens');
notificationsEnabledRef.on('value', function(notificationsEnabledSnapshot) {
notificationEnabledElement.style.display = notificationsEnabledSnapshot.hasChildren() ? 'inline' : 'none';
});
this.listeners.push(notificationsEnabledRef);
// Listen for the Switch state from the Realtime database.
var switchElement = document.getElementById('demo-follow-switch-' + uid);
var followUserRef = firebase.database().ref('followers/' + uid + '/' + this.currentUid);
this.listeners.push(followUserRef);
followUserRef.on('value', function(followSnapshot) {
switchElement.checked = !!followSnapshot.val();
if (materialSwitchContainer.MaterialSwitch) {
materialSwitchContainer.MaterialSwitch.checkDisabled();
materialSwitchContainer.MaterialSwitch.checkToggleState();
}
});
// Listen for switch state changes from the user.
switchElement.addEventListener('change', function() {
followUserRef.set(!!switchElement.checked);
});
}.bind(this));
this.listeners.push(usersRef);
};