window.$ = window.jQuery = require('jquery')
window.Popper = require('popper.js')
require('bootstrap')
$(document).ready(function () {
function pwcheck (e) {
var pw = $('#password').val()
var pwa = $('#password_repeat').val()
if (pwa !== pw) {
$('#password_repeat').addClass('invalid')
$('#repeatcheck').show()
$('#repeatcheck').html('The passwords do not match.')
} else {
$('#password_repeat').removeClass('invalid')
$('#repeatcheck').hide()
$('#repeatcheck').html('')
}
}
// http://www.xtf.dk/2011/08/center-new-popup-window-even-on.html
function PopupCenter (url, title, w, h) {
// Fixes dual-screen position Most browsers Firefox
var dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left
var dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height
var left = ((width / 2) - (w / 2)) + dualScreenLeft
var top = ((height / 2) - (h / 2)) + dualScreenTop
var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left)
// Puts focus on the newWindow
if (window.focus) {
newWindow.focus()
}
return newWindow
}
function removeAuthorization (clientId) {
$.ajax({
type: 'post',
url: '/api/oauth2/authorized-clients/revoke',
data: { client_id: clientId },
success: function (data) {
loadAuthorizations()
}
})
}
function loadAuthorizations () {
$('#clientlist').html('Loading list')
$.get({
url: '/api/oauth2/authorized-clients',
dataType: 'json',
success: function (data) {
if (!data.length) {
return $('#clientlist').html('There is nothing to show at this moment.')
}
$('#clientlist').html('')
for (var i in data) {
var html = ''
var client = data[i]
html += '
'
html += '
'
html += '
Published at ' + new Date(article.created_at) + '
'
html += '
' + article.content + '
'
html += '
'
}
$('.newsfeed').html(html)
}
})
}
if ($('#clientlist').length) {
loadAuthorizations()
}
if ($('#mcinclude').length) {
var customDef = $('#custominfo').val()
$('#mcinclude').change(function () {
$('.mcuname').slideToggle()
if (!this.checked) {
$('#custominfo').val(customDef)
} else {
if ($('#mcusername').val()) {
var mcname = 'mcu:' + $('#mcusername').val()
$('#custominfo').val(customDef ? customDef + ',' + mcname : mcname)
}
}
})
$('#mcusername').on('keyup', function () {
var mcname = 'mcu:' + $(this).val()
if ($(this).val() === '') {
$('#custominfo').val(customDef)
return
}
$('#custominfo').val(customDef ? customDef + ',' + mcname : mcname)
})
}
if ($('#removeAvatar').length) {
$('#removeAvatar').click(function (e) {
e.preventDefault()
$.ajax({
type: 'POST',
url: '/api/avatar/remove',
success: function (data) {
window.location.reload()
}
})
})
}
window.checkLoginState = function () {
var FB = window.FB
FB.getLoginStatus(function (response) {
$.ajax({
type: 'post',
url: '/api/external/facebook/callback',
dataType: 'json',
data: response,
success: function (data) {
if (data.error) {
alert(data.error)
return
}
window.location.reload()
}
}).fail(function () {
alert('An error occured.')
})
})
}
window.googlePOST = function (response) {
$.ajax({
type: 'post',
url: '/api/external/google/callback',
dataType: 'json',
data: response,
success: function (data) {
if (data.error) {
alert(data.error)
return
}
window.location.reload()
}
}).fail(function () {
alert('An error occured.')
})
}
$('.login-dialog-pop').click(function (e) {
e.preventDefault()
var url = $(this).attr('href')
var popup = PopupCenter(url, '_blank', 800, 620)
var timer = setInterval(function () {
if (popup.closed) {
clearInterval(timer)
window.location.reload()
}
}, 1000)
})
$('.accdisconnect').click(function (e) {
e.preventDefault()
var url = $(this).attr('href')
$.get({
url: url,
success: function (e) {
window.location.reload()
}
})
})
$('a[data-toggle]').click(function (e) {
var bn = $(this)
var target = $(bn.attr('data-toggle'))
var speed = bn.attr('data-speed') || 'fast'
$(target).slideToggle(speed)
})
})