/public/js/js.js

https://bitbucket.org/thrabal/smartlib · JavaScript · 144 lines · 109 code · 28 blank · 7 comment · 28 complexity · f8927263768a03866aa7958748230066 MD5 · raw file

  1. /**
  2. * @author Tom
  3. */
  4. $(document).ready(function () {
  5. $('.istooltip').tooltip()
  6. $(".alert").alert()
  7. $('.nav-tabs').button()
  8. $(document).ajaxStart(function(){
  9. $('#loading').css({'top':( $(window).height() / 2 ) - 25 + 'px', 'z-index' : '99999'});
  10. $('#loading').show();
  11. });
  12. $(document).ajaxStop(function(){
  13. $('#loading').hide();
  14. });
  15. $('body').on('submit', "form.ajax", function(e){
  16. var myCallback;
  17. var data_method = 'POST'
  18. if($(this).attr('data-callback') != undefined) {myCallback = $(this).attr('data-callback');}
  19. if($(this).attr('data-method') != undefined) { data_method = $(this).attr('data-method'); }
  20. $.ajax({
  21. type: data_method,
  22. cache: false,
  23. url: $(this).attr('action'),
  24. data: $(this).serialize(),
  25. dataType: 'json',
  26. success: function(data) {
  27. successAjax(data.where, data.action, data.output, myCallback);
  28. }
  29. ,complete: function() {
  30. //complete(false);
  31. }
  32. });
  33. return false;
  34. });
  35. $('body').on('click', "a.ajax", function(e){
  36. var myCallback = null;
  37. var data_method = 'get';
  38. e.preventDefault();
  39. //$('#systemove-hlaseni').fadeOut();
  40. if($(this).attr('data-callback') != undefined) { myCallback = $(this).attr('data-callback'); }
  41. if($(this).attr('data-method') != undefined) { data_method = $(this).attr('data-method'); }
  42. $.ajax({
  43. type: data_method,
  44. cache: false,
  45. url: $(this).attr('href'),
  46. dataType: 'json',
  47. success: function(data) {
  48. successAjax(data.where, data.action, data.output, myCallback);
  49. }
  50. , complete : function(){
  51. //complete(false);
  52. }
  53. });
  54. return false;
  55. });
  56. // --- hide or show some element
  57. $('.hideable-menu').on('click', function(){
  58. var id_hide = $(this).attr('data-hide');
  59. if($(id_hide).css('display') == 'none')
  60. {
  61. $(id_hide).fadeIn();
  62. if( $(this).children(".left").length == 1){ $(this).children(".right").hide(); $(this).children(".left").css('display' , 'inline-block'); }
  63. }
  64. else
  65. {
  66. $(id_hide).fadeOut();
  67. if( $(this).children(".left").length == 1){ $(this).children(".left").hide(); $(this).children(".right").css('display' , 'inline-block'); }
  68. }
  69. })
  70. });
  71. function successAjax(where, action, output, myCallback)
  72. {
  73. var counter = 0;
  74. $.each(action, function() { // for each action
  75. if(this == "redirect")
  76. {
  77. window.location.href = where[counter]
  78. }
  79. if(this == "output")
  80. {
  81. $(where[counter]).hide();
  82. $(where[counter]).html(output[counter]).fadeTo('slow', 1);
  83. }
  84. else if(this == "append")
  85. {
  86. $(where[counter]).append(output[counter]).fadeTo('slow', 1);
  87. }else if(this == "remove")
  88. {
  89. $(where[counter]).fadeOut('slow', function() { $(where[counter]).remove(); });
  90. }else if(this == "ok")
  91. {
  92. $('#flash ' + where[counter]).html(output[counter]);
  93. $('#flash').css({'left' : parseInt($(window).width() / 2) - parseInt($('#flash').width() / 2), 'top' : '25px'});
  94. $('#flash').fadeIn();
  95. $('#ok').show();
  96. $('body').on('click', this, function() {
  97. $('#flash').fadeOut(function(){
  98. $('#ok').hide();
  99. });
  100. });
  101. }
  102. else if(this == 'warning')
  103. {
  104. $('#flash ' + where[counter]).html(output[counter]);
  105. $('#flash').css({'left' : parseInt($(window).width() / 2) - parseInt($('#flash').width() / 2), 'top' : '25px'});
  106. $('#flash').fadeIn();
  107. $('#warning').show();
  108. $('body').on('click', function() {
  109. $('#flash').fadeOut(function(){
  110. $('#warning').hide();
  111. });
  112. });
  113. }
  114. counter = counter + 1;
  115. }); // for each action
  116. }