web2py-bootstrap3.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. (function($, undefined) {
  2. $.web2py.ajax_fields = function(target) {
  3. /*
  4. *this attaches something to a newly loaded fragment/page
  5. * Ideally all events should be bound to the document, so we can avoid calling
  6. * this over and over... all will be bound to the document
  7. */
  8. /*adds btn class to buttons*/
  9. $('button:not([class^="btn"])', target).addClass('btn btn-default');
  10. $("p.w2p-autocomplete-widget input").addClass('form-control');
  11. $('form input[type="submit"]:not([class^="btn"]), form input[type="button"]:not([class^="btn"])', target).addClass('btn btn-default');
  12. /* javascript for PasswordWidget*/
  13. $('input[type=password][data-w2p_entropy]', target).each(function() {
  14. web2py.validate_entropy($(this));
  15. });
  16. /* javascript for ListWidget*/
  17. $('ul.w2p_list', target).each(function() {
  18. function pe(ul, e) {
  19. var new_line = ml(ul);
  20. rel(ul);
  21. if ($(e.target).closest('li').is(':visible')) {
  22. /* make sure we didn't delete the element before we insert after */
  23. new_line.insertAfter($(e.target).closest('li'));
  24. } else {
  25. /* the line we clicked on was deleted, just add to end of list */
  26. new_line.appendTo(ul);
  27. }
  28. new_line.find(":text").focus();
  29. return false;
  30. }
  31. function rl(ul, e) {
  32. if ($(ul).find('li').length > 1) {
  33. /* only remove if we have more than 1 item so the list is never empty */
  34. $(e.target).closest('li').remove();
  35. }
  36. }
  37. function ml(ul) {
  38. /* clone the first field */
  39. var line = $(ul).find("li:first").clone(true);
  40. line.find(':text').val('');
  41. return line;
  42. }
  43. function rel(ul) {
  44. /* keep only as many as needed*/
  45. $(ul).find("li").each(function() {
  46. var trimmed = $.trim($(this).find(":text").val());
  47. if (trimmed == '') $(this).remove();
  48. else $(this).find(":text").val(trimmed);
  49. });
  50. }
  51. var ul = this;
  52. $(ul).find(":text").addClass('form-control').wrap("<div class='input-group'></div>").after('<div class="input-group-addon"><i class="glyphicon glyphicon-plus"></i></div><div class="input-group-addon"><i class="glyphicon glyphicon-minus"></i></div>').keypress(function(e) {
  53. return (e.which == 13) ? pe(ul, e) : true;
  54. }).next().click(function(e) {
  55. pe(ul, e);
  56. e.preventDefault();
  57. }).next().click(function(e) {
  58. rl(ul, e);
  59. e.preventDefault();
  60. });
  61. });
  62. }
  63. $(function() {
  64. $(".nav ul.dropdown-menu").each(function() {
  65. var toggle = jQuery(this).parent();
  66. if (toggle.parent().hasClass("nav")) {
  67. toggle.attr("data-w2pmenulevel", "l0");
  68. toggle.children("a")
  69. .addClass("dropdown-toggle")
  70. .append('<span class="caret"> </span>')
  71. .attr("data-toggle", "dropdown");
  72. } else {
  73. toggle.addClass("dropdown-submenu").removeClass("dropdown");
  74. };
  75. });
  76. });
  77. })(jQuery);