Wrap embed js in anonymous function

This commit is contained in:
Jeremy Zhang 2017-04-13 00:42:32 +00:00
parent 56dc12fcc2
commit a0003388ae
2 changed files with 430 additions and 425 deletions

View File

@ -5,12 +5,13 @@
/* global bot_client_id */
/* global moment */
var logintimer; // timer to keep track of user inactivity after hitting login
var fetchtimeout; // fetch routine timer
var last_message_id; // last message tracked
var selected_channel = guild_id; // user selected channel, defaults to #general channel
(function () {
var logintimer; // timer to keep track of user inactivity after hitting login
var fetchtimeout; // fetch routine timer
var last_message_id; // last message tracked
var selected_channel = guild_id; // user selected channel, defaults to #general channel
function element_in_view(element, fullyInView) {
function element_in_view(element, fullyInView) {
var pageTop = $(window).scrollTop();
var pageBottom = pageTop + $(window).height();
var elementTop = $(element).offset().top;
@ -21,18 +22,18 @@ function element_in_view(element, fullyInView) {
} else {
return ((elementTop <= pageBottom) && (elementBottom >= pageTop));
}
}
}
function query_guild() {
function query_guild() {
var funct = $.ajax({
dataType: "json",
url: "/api/query_guild",
data: {"guild_id": guild_id}
});
return funct.promise();
}
}
function create_authenticated_user() {
function create_authenticated_user() {
var funct = $.ajax({
method: "POST",
dataType: "json",
@ -40,9 +41,9 @@ function create_authenticated_user() {
data: {"guild_id": guild_id}
});
return funct.promise();
}
}
function create_unauthenticated_user(username) {
function create_unauthenticated_user(username) {
var funct = $.ajax({
method: "POST",
dataType: "json",
@ -50,9 +51,9 @@ function create_unauthenticated_user(username) {
data: {"username": username, "guild_id": guild_id}
});
return funct.promise();
}
}
function fetch(channel_id, after=null) {
function fetch(channel_id, after=null) {
var funct = $.ajax({
method: "GET",
dataType: "json",
@ -60,9 +61,9 @@ function fetch(channel_id, after=null) {
data: {"guild_id": guild_id,"channel_id": channel_id, "after": after}
});
return funct.promise();
}
}
function post(channel_id, content) {
function post(channel_id, content) {
var funct = $.ajax({
method: "POST",
dataType: "json",
@ -70,9 +71,9 @@ function post(channel_id, content) {
data: {"guild_id": guild_id, "channel_id": channel_id, "content": content}
});
return funct.promise();
}
}
$(function(){
$(function(){
$("#loginmodal").modal({
dismissible: false, // Modal can be dismissed by clicking outside of the modal
opacity: .5, // Opacity of modal background
@ -92,25 +93,25 @@ $(function(){
initialize_embed(data);
//$('#loginmodal').modal('open');
});
});
});
function lock_login_fields() {
function lock_login_fields() {
$("#loginProgress").show();
$("#discordlogin_btn").attr("disabled",true);
$("#custom_username_field").prop("disabled",true);
logintimer = setTimeout(function() {
unlock_login_fields();
}, 60000);
}
}
function unlock_login_fields() {
function unlock_login_fields() {
$("#loginProgress").hide();
$("#discordlogin_btn").attr("disabled",false);
$("#custom_username_field").prop("disabled",false);
clearTimeout(logintimer);
}
}
function initialize_embed(guildobj) {
function initialize_embed(guildobj) {
$('#loginmodal').modal('close');
unlock_login_fields();
if (guildobj === undefined) {
@ -121,17 +122,17 @@ function initialize_embed(guildobj) {
} else {
prepare_guild(guildobj);
}
}
}
function prepare_guild(guildobj) {
function prepare_guild(guildobj) {
fill_channels(guildobj.channels);
fill_discord_members(guildobj.discordmembers);
fill_authenticated_users(guildobj.embedmembers.authenticated);
fill_unauthenticated_users(guildobj.embedmembers.unauthenticated);
run_fetch_routine();
}
}
function fill_channels(channels) {
function fill_channels(channels) {
var template = $('#mustache_channellistings').html();
Mustache.parse(template);
$("#channels-list").empty();
@ -140,6 +141,9 @@ function fill_channels(channels) {
if (chan.read) {
var rendered = Mustache.render(template, {"channelid": chan.channel.id, "channelname": chan.channel.name});
$("#channels-list").append(rendered);
$("#channel-" + chan.channel.id.toString()).click({"channel_id": chan.channel.id.toString()}, function(event) {
select_channel(event.data.channel_id);
});
if (chan.channel.id == selected_channel) {
if (chan.write) {
$("#messagebox").prop('disabled', false);
@ -153,17 +157,17 @@ function fill_channels(channels) {
}
}
$("#channel-"+selected_channel).parent().addClass("active");
}
}
function mention_member(member_id) {
function mention_member(member_id) {
if (!$('#messagebox').prop('disabled')) {
$('#messagebox').val( $('#messagebox').val() + "[@" + member_id + "] " );
$('.button-collapse').sideNav('hide');
$("#messagebox").focus();
}
}
}
function fill_discord_members(discordmembers) {
function fill_discord_members(discordmembers) {
var template = $('#mustache_authedusers').html();
Mustache.parse(template);
$("#discord-members").empty();
@ -175,9 +179,9 @@ function fill_discord_members(discordmembers) {
mention_member(event.data.member_id);
});
}
}
}
function fill_authenticated_users(users) {
function fill_authenticated_users(users) {
var template = $('#mustache_authedusers').html();
Mustache.parse(template);
$("#embed-discord-members").empty();
@ -189,9 +193,9 @@ function fill_authenticated_users(users) {
mention_member(event.data.member_id);
});
}
}
}
function fill_unauthenticated_users(users) {
function fill_unauthenticated_users(users) {
var template = $('#mustache_unauthedusers').html();
Mustache.parse(template);
$("#embed-unauth-users").empty();
@ -200,13 +204,13 @@ function fill_unauthenticated_users(users) {
var rendered = Mustache.render(template, {"username": member.username, "discriminator": member.discriminator});
$("#embed-unauth-users").append(rendered);
}
}
}
function wait_for_discord_login() {
function wait_for_discord_login() {
_wait_for_discord_login(0);
}
}
function _wait_for_discord_login(index) {
function _wait_for_discord_login(index) {
setTimeout(function() {
var usr = create_authenticated_user();
usr.done(function(data) {
@ -221,9 +225,9 @@ function _wait_for_discord_login(index) {
}
});
}, 5000);
}
}
function select_channel(channel_id) {
function select_channel(channel_id) {
if (selected_channel != channel_id) {
selected_channel = channel_id;
last_message_id = null;
@ -232,9 +236,9 @@ function select_channel(channel_id) {
clearTimeout(fetchtimeout);
run_fetch_routine();
}
}
}
function replace_message_mentions(message) {
function replace_message_mentions(message) {
var mentions = message.mentions;
for (var i = 0; i < mentions.length; i++) {
var mention = mentions[i];
@ -243,13 +247,13 @@ function replace_message_mentions(message) {
message.content = message.content.replace("<@&" + guild_id + ">", "@everyone");
}
return message;
}
}
function getPosition(string, subString, index) {
function getPosition(string, subString, index) {
return string.split(subString, index).join(subString).length;
}
}
function format_bot_message(message) {
function format_bot_message(message) {
if (message.author.id == bot_client_id && (message.content.includes("**") && ( (message.content.includes("<")&&message.content.includes(">")) || (message.content.includes("[") && message.content.includes("]")) ))) {
var usernamefield = message.content.substring(getPosition(message.content, "**", 1)+3, getPosition(message.content, "**", 2)-1);
message.content = message.content.substring(usernamefield.length+7);
@ -257,16 +261,16 @@ function format_bot_message(message) {
message.author.discriminator = usernamefield.split("#")[1];
}
return message;
}
}
function parse_message_time(message) {
function parse_message_time(message) {
var mome = moment(message.timestamp);
message.formatted_timestamp = mome.toDate().toString();
message.formatted_time = mome.format("h:mm A");
return message;
}
}
function parse_message_attachments(message) {
function parse_message_attachments(message) {
for (var i = 0; i < message.attachments.length; i++) {
var attach = "";
if (message.content.length != 0) {
@ -276,9 +280,9 @@ function parse_message_attachments(message) {
message.content += attach;
}
return message;
}
}
function handle_last_message_mention() {
function handle_last_message_mention() {
var lastmsg = $("#chatcontent p:last-child");
var content = lastmsg.text().toLowerCase();
var username_discrim = $("#currentusername").text().toLowerCase();
@ -286,9 +290,9 @@ function handle_last_message_mention() {
lastmsg.css( "color", "#ff5252" );
lastmsg.css( "font-weight", "bold" );
}
}
}
function escapeHtml(unsafe) { /* http://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript */
function escapeHtml(unsafe) { /* http://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript */
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
@ -297,12 +301,12 @@ function escapeHtml(unsafe) { /* http://stackoverflow.com/questions/6234773/can-
.replace(/'/g, "&#039;");
}
function nl2br (str, is_xhtml) { /* http://stackoverflow.com/questions/2919337/jquery-convert-line-breaks-to-br-nl2br-equivalent/ */
function nl2br (str, is_xhtml) { /* http://stackoverflow.com/questions/2919337/jquery-convert-line-breaks-to-br-nl2br-equivalent/ */
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
}
function fill_discord_messages(messages, jumpscroll) {
function fill_discord_messages(messages, jumpscroll) {
if (messages.length == 0) {
return last_message_id;
}
@ -325,9 +329,9 @@ function fill_discord_messages(messages, jumpscroll) {
target: "_blank"
});
return last;
}
}
function run_fetch_routine() {
function run_fetch_routine() {
var channel_id = selected_channel;
var fet;
var jumpscroll;
@ -371,9 +375,9 @@ function run_fetch_routine() {
fetchtimeout = setTimeout(run_fetch_routine, 10000);
}
});
}
}
function update_embed_userchip(authenticated, avatar, username, userid, discrim=null) {
function update_embed_userchip(authenticated, avatar, username, userid, discrim=null) {
if (authenticated) {
$("#currentuserimage").show();
$("#currentuserimage").attr("src", avatar);
@ -382,14 +386,14 @@ function update_embed_userchip(authenticated, avatar, username, userid, discrim=
$("#currentuserimage").hide();
$("#currentusername").text(username + "#" + userid);
}
}
}
$("#discordlogin_btn").click(function() {
$("#discordlogin_btn").click(function() {
lock_login_fields();
wait_for_discord_login();
});
});
$("#custom_username_field").keyup(function(event){
$("#custom_username_field").keyup(function(event){
if (event.keyCode == 13) {
if (!(new RegExp(/^[a-z\d\-_\s]+$/i).test($(this).val()))) {
Materialize.toast('Illegal username provided! Only alphanumeric, spaces, dashes, and underscores allowed in usernames.', 10000);
@ -410,12 +414,12 @@ $("#custom_username_field").keyup(function(event){
Materialize.toast('Illegal username provided! Only alphanumeric, spaces, dashes, and underscores allowed in usernames.', 10000);
}
unlock_login_fields();
})
});
}
}
});
});
$("#messagebox").keyup(function(event){
$("#messagebox").keyup(function(event){
if ($(this).val().length == 1) {
$(this).val($.trim($(this).val()));
}
@ -439,21 +443,22 @@ $("#messagebox").keyup(function(event){
});
funct.always(function() {
$("#messagebox").attr('readonly', false);
})
});
}
});
});
$('#guild-btn').sideNav({
$('#guild-btn').sideNav({
menuWidth: 300, // Default is 300
edge: 'left', // Choose the horizontal origin
closeOnClick: true, // Closes side-nav on <a> clicks, useful for Angular/Meteor
draggable: true // Choose whether you can drag to open on touch screens
}
);
}
);
$('#members-btn').sideNav({
$('#members-btn').sideNav({
menuWidth: 300, // Default is 300
edge: 'right', // Choose the horizontal origin
draggable: true // Choose whether you can drag to open on touch screens
}
);
}
);
})();

View File

@ -109,7 +109,7 @@
{% raw %}
<script id="mustache_channellistings" type="text/template">
<li><a class="waves-effect truncate" id="channel-{{channelid}}" onclick='select_channel("{{channelid}}");'><span class="channel-hash">#</span> {{channelname}}</a></li>
<li><a class="waves-effect truncate" id="channel-{{channelid}}"><span class="channel-hash">#</span> {{channelname}}</a></li>
</script>
<script id="mustache_authedusers" type="text/template">
@ -126,8 +126,8 @@
{% endraw %}
<script>
var guild_id = "{{ guild_id }}";
var bot_client_id = "{{ client_id }}";
const guild_id = "{{ guild_id }}";
const bot_client_id = "{{ client_id }}";
</script>
<script type="text/javascript" src="{{ url_for('static', filename='js/embed.js') }}"></script>