ui.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. This file is part of CPEE.
  3. CPEE is free software: you can redistribute it and/or modify it under the terms
  4. of the GNU General Public License as published by the Free Software Foundation,
  5. either version 3 of the License, or (at your option) any later version.
  6. CPEE is distributed in the hope that it will be useful, but WITHOUT ANY
  7. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  8. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. CPEE (file COPYING in the main directory). If not, see
  11. <http://www.gnu.org/licenses/>.
  12. */
  13. function ui_click_tab(moi) { // {{{
  14. $(moi).trigger('click');
  15. } // }}}
  16. function ui_close_tab(moi){
  17. var active = $(moi).parent().attr('data-tab');
  18. var tabbed = $(moi).parent().parent().parent();
  19. var is_inactive = $(moi).parent().hasClass('inactive');
  20. $('*[data-tab=' + active + ']').remove();
  21. $('*[data-belongs-to-tab=' + active + ']').remove();
  22. if (!is_inactive)
  23. ui_click_tab($('ui-tabbar ui-tab.default'));
  24. }
  25. function ui_add_close(moi) {
  26. $(moi).append($('<ui-close>✖</ui-close>'));
  27. }
  28. function ui_empty_tab_contents(id) {
  29. $('ui-content ui-area[data-belongs-to-tab=' + id + ']').empty();
  30. }
  31. function ui_add_tab(tabbed,title,id,closeable,additionalclasses) {
  32. additionalclasses = typeof additionalclasses !== 'undefined' ? additionalclasses : '';
  33. if ($('ui-tabbar ui-tab[data-tab=' + id + ']').length > 0) {
  34. ui_activate_tab($('ui-tabbar ui-tab[data-tab=' + id + ']'));
  35. return false;
  36. } else {
  37. var instab = $("<ui-tab class='inactive" + (closeable ? ' closeable' : '') + (additionalclasses == '' ? '' : ' ' + additionalclasses) + "' data-tab='" + id + "'>" + title + "</ui-tab>");
  38. var insarea = $("<ui-area data-belongs-to-tab='" + id + "' class='inactive'></ui-area>");
  39. $(tabbed).find('ui-behind').before(instab);
  40. $(tabbed).find('ui-content').append(insarea);
  41. ui_add_close($('ui-tabbar ui-tab[data-tab=' + id + ']'));
  42. return true;
  43. }
  44. }
  45. function ui_clone_tab(tabbar,original,title,id,closeable,additionalclasses) {
  46. additionalclasses = typeof additionalclasses !== 'undefined' ? additionalclasses : '';
  47. var instab = $("<ui-tab class='inactive" + (closeable ? ' closeable' : '') + (additionalclasses == '' ? '' : ' ' + additionalclasses) + "' data-tab='" + id + "' id='tab_" + id + "'>" + title + "</ui-tab>");
  48. var insarea = original.clone();
  49. insarea.attr("data-belongs-to-tab",id);
  50. insarea.attr("class","inactive");
  51. $(tabbar).find('ui-behind').before(instab);
  52. $(tabbar).parent().append(insarea);
  53. ui_add_close($('ui-tabbed ui-tab[data-tab=' + id + ']'));
  54. }
  55. (function($) { //{{{
  56. $.fn.dragcolumn = function() {
  57. var drag = $(this);
  58. var prev = drag.prev();
  59. var next = drag.next();
  60. this.on("mousedown", function(e) {
  61. drag.addClass('draggable');
  62. $(document).one("mouseup", function(e) {
  63. drag.removeClass('draggable');
  64. e.preventDefault();
  65. });
  66. e.preventDefault();
  67. });
  68. $(document).on("mousemove", function(e) {
  69. if (!drag.hasClass('draggable'))
  70. return;
  71. // Assume 50/50 split between prev and next then adjust to
  72. // the next X for prev
  73. var total = prev.outerWidth() + next.outerWidth();
  74. var pos = e.pageX - prev.offset().left;
  75. if (pos > total) {
  76. pos = total;
  77. }
  78. var leftPercentage = pos / total;
  79. var rightPercentage = 1 - leftPercentage;
  80. prev.css('flex', leftPercentage.toString());
  81. next.css('flex', rightPercentage.toString());
  82. e.preventDefault();
  83. });
  84. }
  85. $.fn.dragresize = function() {
  86. var drag = $(this);
  87. var prev = drag.prev();
  88. var initpos = 0;
  89. var initheight = $("ui-content",prev).height();
  90. this.on("mousedown", function(e) {
  91. drag.addClass('draggable');
  92. initpos = e.pageY;
  93. $(document).one("mouseup", function(e) {
  94. drag.removeClass('draggable');
  95. e.preventDefault();
  96. });
  97. e.preventDefault();
  98. });
  99. $(document).on("mousemove", function(e) {
  100. if (!drag.hasClass('draggable'))
  101. return;
  102. var pos = initheight - (initpos - e.pageY);
  103. if (pos < 0)
  104. return;
  105. $("ui-content",prev).css('height', pos.toString());
  106. e.preventDefault();
  107. });
  108. }
  109. })(jQuery); //}}}
  110. function ui_activate_tab(moi) { // {{{
  111. var active = $(moi).attr('data-tab');
  112. var tabbed = $(moi).parent().parent();
  113. var tabs = [];
  114. $("ui-tabbar > ui-tab",tabbed).each(function(){
  115. if (!$(this).attr('class').match(/switch/)) {
  116. tabs.push($(this).attr('data-tab'));
  117. }
  118. });
  119. $(".inactive",tabbed).removeClass("inactive");
  120. $.each(tabs,function(a,b){
  121. if (b != active) {
  122. $("ui-tabbar ui-tab[data-tab=" + b + "]",tabbed).addClass("inactive");
  123. $("ui-content *[data-belongs-to-tab=" + b + "]",tabbed).addClass("inactive");
  124. }
  125. });
  126. } // }}}
  127. function ui_toggle_vis_tab(moi) {// {{{
  128. if ($(moi)[0].nodeName == 'UI-TABBED') {
  129. var tabbed = $(moi);
  130. }
  131. if ($(moi)[0].nodeName == 'UI-TAB') {
  132. var tabbed = $(moi).parent().parent();
  133. }
  134. if (tabbed) {
  135. tabbed.toggleClass('off');
  136. }
  137. }// }}}
  138. $(document).ready(function() {
  139. if (!($.browser.name == "Firefox" && $.browser.version >= 20) && !($.browser.name == "Chrome" && $.browser.version >= 30)) {
  140. $('body').children().remove();
  141. $('body').append('Sorry, only Firefox >= 20.0 and Chrom(e|ium) >= 17 for now.');
  142. }
  143. $('ui-rest ui-content ui-resizehandle').dragcolumn();
  144. $('*[is=x-ui] > ui-resizehandle').dragresize();
  145. $(document).on('click','ui-tabbar ui-tab.switch',function(){ui_toggle_vis_tab(this);});
  146. $(document).on('click','ui-tabbar ui-tab:not(.switch)',function(){ui_activate_tab(this);});
  147. ui_add_close($('ui-tabbar ui-tab.closeable'));
  148. $(document).on('click','ui-tabbar ui-tab.closeable ui-close',function(){ui_close_tab(this);});
  149. });