node-red-contrib-salesforce-chatter
Version:
A processing node of Node-RED nodes for Salesforce Chatter.
432 lines (403 loc) • 15.7 kB
HTML
<!--
Copyright 2014 Atsushi Kojo.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="force-chatter content">
<div class="form-row">
<label for="node-input-force"><i class="fa fa-user"></i> Log in as</label>
<input type="text" id="node-input-force">
</div>
<div class="form-row">
<label for="node-input-operation"><i class="fa fa-wrench"></i> Operation</label>
<select type="text" id="node-input-operation">
<option value="post_feed">Post Feed</option>
</select>
</div>
<div class="form-row input-group-row hidden">
<label for="node-input-group"><i class="fa fa-group"></i> Group</label>
<select id="node-input-group" >
</select>
</div>
<div class="form-row input-query-row hidden">
<label for="node-input-query"><i class="fa fa-search"></i> Search string</label>
<input type="text" id="node-input-query" >
</div>
<div class="form-row input-to-row hidden">
<label for="node-input-to"><i class="fa fa-envelope"></i> To</label>
<select id="node-input-to" >
</select>
</div>
</script>
<script type="text/x-red" data-help-name="force-chatter content">
<p>Salesforce Chatter node accept the following params:</p>
<ul>
<li>msg.payload</li>
<li>msg.binaryBuffer + msg.filename</li>
</ul>
<h3>Inputs</h3>
<dl class="message-properties">
<dt class="optional">
payload <span class="property-type">array</span>
</dt>
<dd>
The message's structure to post:
<pre>[{
text: 'Message to send',
mention: 'example@gmail.com' //this is optional
}]</pre>
</dd>
</dl>
<dl class="message-properties">
<dt class="optional">
binaryBuffer <span class="property-type">array</span>
</dt>
<dt class="optional">
filename <span class="property-type">string</span>
</dt>
<dd>Binary Buffer data of image and it's filename (for example `image1.png` ).</dd>
</dl>
<h3>Outputs</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">object</span></dt>
<dd>Outputs the execution result of the selected process.</dd>
</dl>
<h3>References</h3>
<ul>
<li>
<a href="https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/connect_responses_feed_element_page.htm">Feed Element Page</a> - See here for details on the syntax of the feed information output by get feed, get group feed, and search feed.
</li>
<li>
<a href="https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/connect_responses_feed_item.htm">Feed Item</a> - See here for details on the syntax of the feed information output by a feed post.
</li>
</ul>
</script>
<script type="text/javascript">
var globalVal = true;
RED.nodes.registerType("force-chatter content", {
category: "Social",
color: "#48ace4",
defaults: {
force: { type: "force", required: true },
operation: { value: "post_feed" },
group: { value: "" },
query: { value: "" },
to: { value: "" },
mention: { value: "" },
name: { value: "" },
loadUser: { value: "0" },
loadGroup: { value: "0" }
},
inputs: 1,
outputs: 1,
icon: "force.png",
label: function () {
var forceNode = RED.nodes.node(this.force);
return this.name || "force chatter content";
},
labelStyle: function () {
return this.name ? "node_label_italic" : "";
},
oneditprepare: function () {
if (globalVal) {
var val = parseInt($("#node-input-loadUser").attr('value', 0));
var val2 = parseInt($("#node-input-loadGroup").attr('value', 0));
globalVal = false;
}
chatterNode = RED.nodes.node(this.id);
var groupel = $(".input-group-row");
var queryel = $(".input-query-row");
var toel = $(".input-to-row");
var mentionel = $(".input-mention-row");
var loadel = $(".load-more-row");
var searchel = $(".search-row");
var selectel = $(".load-selected-row");
var selected = selectedMention();
val = parseInt($("#node-input-loadUser").val());
val2 = parseInt($("#node-input-loadGroup").val());
$("#node-input-loadUser")
.button()
.click(function () {
var valTemp = ++val;
$("#node-input-loadUser").val(valTemp);
pollChatterMention2(valTemp, val2);
});
$("#node-input-loadGroup")
.button()
.click(function () {
var val2Temp = ++val2;
$("#node-input-loadGroup").val(val2Temp);
pollChatterMention2(val, val2Temp);
});
$("#node-input-operation").change(function () {
if (window.chatterNodeIntervalId) {
window.clearTimeout(window.chatterNodeIntervalId);
delete window.chatterNodeIntervalId;
}
var id = $("#node-input-operation option:selected").val();
if (id != chatterNode.operation) {
$("#node-input-group").val("");
$("#node-input-query").val("");
$("#node-input-to").val("");
$("#node-input-mention").val("");
}
if (id == "get_group") {
groupel.show();
queryel.hide();
toel.hide();
loadel.hide();
searchel.hide();
$(".loadUserBtn").hide();
$(".loadGroupBtn").hide();
selectel.hide();
mentionel.hide();
window.chatterNodeIntervalId = window.setTimeout(pollChatterGroup, 2000);
} else if (id == "search_feed") {
groupel.hide();
queryel.show();
toel.hide();
loadel.hide();
searchel.hide();
mentionel.hide();
selectel.hide();
} else if (id == "post_feed") {
groupel.hide();
queryel.hide();
toel.show();
$(".loadUserBtn").show();
$(".loadGroupBtn").show();
selectel.show();
loadel.show();
searchel.show();
mentionel.show();
window.chatterNodeIntervalId = window.setTimeout(pollChatterMention, 2000);
} else {
selectel.hide();
groupel.hide();
queryel.hide();
toel.hide();
searchel.hide();
mentionel.hide();
loadel.hide();
}
});
},
oneditsave: function () {
val = parseInt($("#node-input-loadUser").val());
val2 = parseInt($("#node-input-loadGroup").val());
var id = $("#node-input-operation option:selected").val();
if (id == "post_feed") {
var selected = $("#node-input-mention").val();
if (selected) {
chatterNode.mention = selected.join(",");
} else {
chatterNode.mention = "";
}
if (window.chatterNodeIntervalId) {
window.clearTimeout(window.chatterNodeIntervalId);
delete window.chatterNodeIntervalId;
}
}
}
});
var chatterNode;
function updateGroupList(lists) {
var grouplist = $("#node-input-group");
grouplist.children("option").remove();
grouplist.append('<option value=""> == select Group == </option>');
$.each(lists.groups, function (i, opt) {
grouplist.append(
'<option value="' + opt.id + '">' + opt.name + "</option>"
);
});
grouplist.val(chatterNode.group);
}
function pollChatterGroup() {
var configid = $("#node-input-force").val();
configid = configid.replace("_ADD_", "");
var credentailId = null,
param = "";
credentailId = chatterNode.force || configid;
if (credentailId) {
var configNode = RED.nodes.node(configid);
param = "&logintype=" + configNode.logintype + "&loginurl=" + configNode.loginurl + "&username=" + configNode.username;
if (configNode.credentials) param += "&password=" + configNode.credentials.password;
$.getJSON("force-chatter/get-groups?id=" + chatterNode.id + "&credentials=" + credentailId + param,
function (data) {
if (data) {
updateGroupList(data);
delete window.chatterNodeIntervalId;
} else {
window.chatterNodeIntervalId = window.setTimeout(pollChatterGroup, 2000);
}
});
} else {
window.chatterNodeIntervalId = window.setTimeout(pollChatterGroup, 2000);
}
}
var listUser = []
var listGroup = []
var tempUser = {}
var tempGroup = {}
var firstFeedItem = {}
var flag = true
var flagnextPageUrlUser = true
var flagnextPageUrlGroup = true
function updateMentionList(lists) {
if (lists.users.nextPageUrl == null || !flagnextPageUrlUser) {
$("#node-input-loadUser").attr("disabled", true).css('background', '#727272')
flagnextPageUrlUser = false
}
if (lists.groups.nextPageUrl == null || !flagnextPageUrlGroup) {
$("#node-input-loadGroup").attr("disabled", true).css('background', '#727272')
flagnextPageUrlGroup = false
}
var mentionlist = $("#node-input-mention"),
users = lists.users,
groups = lists.groups,
opts = "";
mentionlist.children("optgroup").remove();
if (flag) {
firstFeedItem = users.users
firstGroup = groups.groups
}
if (JSON.stringify(tempUser) != JSON.stringify(users.users) && JSON.stringify(firstFeedItem) != JSON.stringify(users.users) || flag) {
tempUser = users.users
$.each(tempUser, function (i, opt) {
listUser.push(opt)
})
}
if (JSON.stringify(tempGroup) != JSON.stringify(groups.groups) && JSON.stringify(firstGroup[0].id) != JSON.stringify(groups.groups[0].id) || flag) {
tempGroup = groups.groups
$.each(tempGroup, function (i, opt) {
listGroup.push(opt)
})
}
flag = false
$("#node-input-loadUser").text("Load more Users (" + listUser.length + ")").css('padding', '10px')
opts += '<optgroup label="Users">';
$.each(listUser, function (i, opt) {
opts += '<option value="' + opt.id + '">' + opt.name + " [ " + opt.userType + " ]" + '</option>';
});
opts += '</optgroup>';
// $("#node-input-loadGroup").text("Load more Groups (" + listGroup.length + ")").css('padding', '10px')
opts += '<optgroup label="Groups">';
$.each(listGroup, function (i, opt) {
opts += '<option value="' + opt.id + '">' + opt.name + '</option>';
});
opts += '</optgroup>';
mentionlist.append(opts);
mentionlist.val(chatterNode.mention.split(','));
var selectedItems = [];
}
function searchMention() {
var keyword = document.getElementById("searchInput").value;
var selects = $("#node-input-mention").children("optgroup")[0].childNodes
selects.forEach(function (select) {
var txt = select.innerText;
if (txt.substring(0, keyword.length).toLowerCase() !== keyword.toLowerCase() && keyword.trim() !== "") {
$("#node-input-mention option[value='" + select.value + "']").hide()
} else {
$("#node-input-mention option[value='" + select.value + "']").show()
}
})
}
function selectedMention() {
var selectedUsers = "";
var selectedGroups = "";
$("#node-input-mention option:selected").each(function () {
var label = $(this).closest('optgroup').attr('label');
if (label == "Users") {
selectedUsers += $(this).text() + ", ";
} else {
selectedGroups += $(this).text() + ", ";
}
});
$("#usersSelected").text(selectedUsers);
$("#groupsSelected").text(selectedGroups);
}
var listGroupTo = [];
var tempGroupTo = {};
var flagGroupTo = true;
function updateToList(lists) {
var tolist = $("#node-input-to"),
opts = "";
tolist.children("option").remove();
tolist.children("optgroup").remove();
opts = '<option value=""> == select To == </option>';
opts += '<optgroup label="Groups">';
if (flagGroupTo) {
firstGroupTo = lists.groups.groups
}
if (JSON.stringify(tempGroupTo) != JSON.stringify(lists.groups.groups) && JSON.stringify(firstGroupTo[0].id) != JSON.stringify(lists.groups.groups[0].id) || flagGroupTo) {
tempGroupTo = lists.groups.groups
$.each(tempGroupTo, function (i, opt) {
listGroupTo.push(opt)
})
}
flagGroupTo = false
$.each(listGroupTo, function (i, opt) {
opts += '<option value="' + opt.id + '">' + opt.name + "</option>";
});
opts += "</optgroup>";
tolist.append(opts);
tolist.val(chatterNode.to);
}
function pollChatterMention() {
var configid = $("#node-input-force").val();
configid = configid.replace("_ADD_", "");
var credentailId = null,
param = "";
credentailId = chatterNode.force || configid;
if (credentailId) {
var configNode = RED.nodes.node(configid);
param = "&logintype=" + configNode.logintype + "&loginurl=" + configNode.loginurl + "&username=" + configNode.username;
if (configNode.credentials) param += "&password=" + configNode.credentials.password;
$.getJSON("force-chatter/get-mentions?id=" + chatterNode.id + "&credentials=" + credentailId + param + "&userPage=0" + "&groupPage=0",
function (data) {
if (data) {
updateMentionList(data);
updateToList(data);
selectedMention();
delete window.chatterNodeIntervalId;
} else {
window.chatterNodeIntervalId = window.setTimeout(pollChatterMention, 2000);
}
});
} else {
window.chatterNodeIntervalId = window.setTimeout(pollChatterMention, 2000);
}
}
function pollChatterMention2(userPage, groupPage) {
var configid = $("#node-input-force").val();
configid = configid.replace("_ADD_", "");
var credentailId = null,
param = "";
credentailId = chatterNode.force || configid;
if (credentailId) {
var configNode = RED.nodes.node(configid);
param = "&logintype=" + configNode.logintype + "&loginurl=" + configNode.loginurl + "&username=" + configNode.username;
if (configNode.credentials)
param += "&password=" + configNode.credentials.password;
$.getJSON("force-chatter/get-mentions?id=" + chatterNode.id + "&credentials=" + credentailId + param + "&userPage=" + userPage + "&groupPage=" + groupPage, function (data) {
if (data) {
updateMentionList(data);
updateToList(data);
delete window.chatterNodeIntervalId;
} else {
window.chatterNodeIntervalId = window.setTimeout(pollChatterMention2(userPage, groupPage), 2000);
}
}
);
} else {
window.chatterNodeIntervalId = window.setTimeout(pollChatterMention2(userPage, groupPage), 2000);
}
}
</script>