/*global defaultHidden, bootbox*/
$(document).ready(function () {
'use strict';
/* Confirm delete */
$('.del').each(function (idx, obj) {
$(obj).click(function () {
bootbox.dialog({
message: "Are you sure you wish to delete this item?",
title: "Attention",
buttons: {
main: {
label: "No",
className: "btn-default",
callback: function () {
bootbox.hideAll();
}
},
danger: {
label: "Yes",
className: "btn-danger",
callback: function () {
window.location.href = $(obj).attr('href');
}
}
}
});
return false;
});
});
$('.confirmDel').each(function (idx, obj) {
$(obj).click(function () {
bootbox.dialog({
message: "Are you sure you wish to delete this item?",
title: "Attention",
buttons: {
main: {
label: "No",
className: "btn-default",
callback: function () {
bootbox.hideAll();
}
},
danger: {
label: "Yes",
className: "btn-danger",
callback: function () {
window.location.href = $(obj).attr('href');
}
}
}
});
return false;
});
});
/* Generate a password */
var generatePass = function (keylistalpha, keylistint, keylistspec, plength) {
var temp = '';
var len = plength / 2;
var len = len - 1;
var lenspec = plength - len - len;
var i;
for (i = 0; i < len; i++)
temp += keylistalpha.charAt(Math.floor(Math.random() * keylistalpha.length));
for (i = 0; i < lenspec; i++)
temp += keylistspec.charAt(Math.floor(Math.random() * keylistspec.length));
for (i = 0; i < len; i++)
temp += keylistint.charAt(Math.floor(Math.random() * keylistint.length));
temp = temp.split('').sort(function () {
return 0.5 - Math.random()
}).join('');
return temp;
};
$('.generator').removeClass('hide');
$('.generator').click(function () {
var keylistalpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ";
var keylistint = "123456789";
var keylistspec = "!@#_";
var plength = 16;
bootbox.dialog({
'title': 'Password generator',
'backdrop': 'static',
'message': '
',
'buttons': {
'generate': {
label: 'Generate',
className: 'btn-primary',
callback: function () {
var nkeylistalpha = $('#generatePassChars').val();
var nkeylistint = $('#generatePassNum').val();
var nkeylistspec = $('#generatePassSpec').val();
var nplength = $('#generatePassLen').val();
$('#generatePass').val(generatePass(nkeylistalpha, nkeylistint, nkeylistspec, nplength));
$('#generatePass').select();
return false;
}
},
'close': {
label: 'Close',
className: 'btn-default'
}
}
});
$('.generatePassContainer #copyToClipboard').unbind('click');
$('.generatePassContainer #copyToClipboard').click(function () {
$('#generatePass').select();
});
});
/* Account in modal window */
$('.account').unbind('click');
$('.account').click(function () {
$('#welcome-dropdown').click();
$.ajax({
type: 'GET',
url: $(this).attr('href'),
success: function (data) {
bootbox.dialog({
'title': 'Change account settings',
'className': "from-page",
'message': data,
'backdrop': true
});
}
});
return false;
});
/* Users in modal window */
$('.user-add').unbind('click');
$('.user-add').click(function () {
$.ajax({
type: 'GET',
url: $(this).attr('href'),
success: function (data) {
bootbox.dialog({
'title': 'User details',
'className': "from-page",
'message': data,
'backdrop': true
});
}
});
return false;
});
/* Categories in modal window */
$('.category-add').unbind('click');
$('.category-add').click(function () {
$.ajax({
type: 'GET',
url: $(this).attr('href'),
success: function (data) {
bootbox.dialog({
'title': 'Category details',
'className': "from-page",
'message': data,
'backdrop': true
});
}
});
return false;
});
/* Pretty checkboxes and radios */
/*
$.each($('input[type=checkbox]'), function () {
$(obj).prettyCheckable();
});
$.each($('input[type=radio]'), function (idx, obj) {
$(obj).prettyCheckable();
});
*/
/**/
$(document).on('click', '.bootbox', function (event) {
var target = event.target;
// Check the parentNodes until
// we hit the actual backdrop
while (target !== this) {
target = target.parentNode;
// If the click was inside a
// modal-dialog ABORT
if (target.className.indexOf('modal-dialog') !== -1) {
return;
}
}
// Fire some close Callback here
bootbox.hideAll();
});
/* Copy to clipboard */
var copyToClipboard = function (e) {
var clip = new ClipboardEvent('copy');
clip.clipboardData.setData('text/plain', "test");
clip.preventDefault();
e.target.dispatchEvent(clip);
}
// Drop down on hoover
jQuery('ul.nav li.dropdown').hover(function () {
jQuery(this).find('.dropdown-menu').stop(true, true).show();
jQuery(this).addClass('open');
}, function () {
jQuery(this).find('.dropdown-menu').stop(true, true).hide();
jQuery(this).removeClass('open');
});
jQuery('.dropup').hover(function () {
jQuery(this).find('.dropdown-menu').stop(true, true).show();
jQuery(this).addClass('open');
}, function () {
jQuery(this).find('.dropdown-menu').stop(true, true).hide();
jQuery(this).removeClass('open');
});
// Scroll to in main page
$("a.scrollTo").click(function () {
var btn = $(this),
target = $('#' + btn.data('target'));
$('html, body').animate({
scrollTop: target.offset().top - 65
}, 1000);
return false;
});
// Check if content has scollbar
$.fn.hasScrollBar = function () {
return this.get(0).scrollHeight > this.height();
};
var showQuickNav = function () {
var quickNav = $('#quickNav'),
container = $('html, body');
if (!container.hasScrollBar()) {
quickNav.removeClass('hidden');
} else {
quickNav.addClass('hidden');
}
};
$(window).on('resize', showQuickNav);
showQuickNav.call();
// Keep quickNav visible
var moveQuickNav = function () {
if ($('#quickNav').length > 0) {
var win = $(document),
winTop = win.scrollTop() + 60,
el = $('#quickNav'),
elRel = $('.generator'),
elRelBottom = elRel.offset().top + elRel.outerHeight(),
elTop = el.offset().top,
elDataDefault = el.data('default-position'),
elDefault = elDataDefault || elTop,
newTop = 0;
if (elDefault > elRelBottom) {
elDefault = elRelBottom;
}
if (winTop > 0 && elTop > 0) {
// Set the default position
el.data('default-position', elDefault);
// Moving
if (winTop > elTop) {
// down
newTop = winTop + 10;
}
if (winTop - 10 < elTop) {
// up
newTop = winTop;
}
if (newTop < elDefault) {
// check
newTop = elDefault;
}
if (newTop < elRelBottom) {
newTop = elRelBottom;
}
el.offset({
top: newTop
});
}
}
};
$(document).scroll(moveQuickNav);
/* Resize textarea to fit content */
function h(e) {
$(e).css({
'height': 'auto',
'overflow-y': 'hidden'
}).height(e.scrollHeight);
moveQuickNav.call(this);
}
$('textarea').each(function () {
h(this);
}).on('input', function () {
h(this);
});
/* search notes */
$.each($('.stories li a.note'), function (idx, obj) {
$(obj).attr('data-href', $(obj).attr('href'));
});
$.each($('form'), function (idx, obj) {
$(obj).attr('data-action', $(obj).attr('action'));
});
var searchFunction = function () {
var stories = $('.stories'),
items = $('.stories li'),
links = $('.stories a.note'),
value = $('.notes-search input').val() || '';
$.each(items, function (i, obj) {
var text = $(obj).text().toUpperCase();
if (text.indexOf(value.toUpperCase()) > -1 && (defaultHidden === false || (defaultHidden === true && value.length > 0))) {
$(obj).css("display", "block");
} else {
$(obj).css("display", "none");
}
});
$.each(links, function (i, obj) {
var link = $(obj);
if (value.length > 0) {
link.attr('href', link.attr('data-href') + '?s=' + value);
} else {
link.attr('href', link.attr('data-href'));
}
});
$.each($('form'), function (idx, obj) {
if (value.length > 0) {
$(obj).attr('action', $(obj).attr('data-action') + '?s=' + value);
} else {
$(obj).attr('action', $(obj).attr('data-action'));
}
});
$.each(stories, function (i, obj) {
if ($(obj).children('ul').children('li:visible').length > 0) {
$(obj).children('h2').css("display", "block");
} else {
$(obj).children('h2').css("display", "none");
}
});
if (value.length > 0) {
$('.notes-search-clear').removeClass('hide');
} else {
$('.notes-search-clear').addClass('hide');
}
// Show / hide quickNav
showQuickNav.call();
};
$('.notes-search').removeClass('hide');
$('.notes-search input').on('keyup', searchFunction);
$(".notes-search-clear").click(function () {
$(this).prev('input').val('').focus();
searchFunction();
});
searchFunction.call();
/* Focus on the search input*/
$("#s").focus();
});