You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.6 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (c) 2017-2019 MuK IT GmbH.
  4. *
  5. * This file is part of MuK Web Utils
  6. * (see https://mukit.at).
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. **********************************************************************************/
  22. $.fn.textWidth = function(text, font) {
  23. if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
  24. $.fn.textWidth.fakeEl.text(text || this.val() || this.text()).css('font', font || this.css('font'));
  25. return $.fn.textWidth.fakeEl.width();
  26. };
  27. $.fn.dndHover = function(options) {
  28. return this.each(function() {
  29. var self = $(this);
  30. var collection = $();
  31. var dragenter = function(event) {
  32. if (collection.size() === 0) {
  33. self.trigger('dndHoverStart', [event]);
  34. }
  35. collection = collection.add(event.target);
  36. };
  37. var dragleave = function(event) {
  38. setTimeout(function() {
  39. collection = collection.not(event.target);
  40. if (collection.size() === 0) {
  41. self.trigger('dndHoverEnd', [event]);
  42. }
  43. }, 1);
  44. };
  45. var drop = function(event) {
  46. setTimeout(function() {
  47. collection = $();
  48. self.trigger('dndHoverEnd', [event]);
  49. }, 1);
  50. };
  51. if(options && options === 'destroy') {
  52. self.off('dragenter.dnd_hover');
  53. self.off('dragleave.dnd_hover');
  54. self.off('drop.dnd_hover');
  55. } else {
  56. self.on('dragenter.dnd_hover', dragenter);
  57. self.on('dragleave.dnd_hover', dragleave);
  58. self.on('drop.dnd_hover', drop);
  59. }
  60. });
  61. };
  62. $.ajaxTransport("+binary", function(options, originalOptions, jqXHR) {
  63. if (window.FormData && ((options.dataType && (options.dataType == 'binary')) ||
  64. (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) ||
  65. (window.Blob && options.data instanceof Blob))))) {
  66. return {
  67. send: function(headers, callback){
  68. var xhr = new XMLHttpRequest();
  69. var url = options.url,
  70. type = options.type,
  71. async = options.async || true,
  72. dataType = options.responseType || 'blob',
  73. data = options.data || null,
  74. username = options.username,
  75. password = options.password;
  76. xhr.addEventListener('load', function(){
  77. var data = {};
  78. data[options.dataType] = xhr.response;
  79. callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
  80. });
  81. xhr.open(type, url, async, username, password);
  82. for (var i in headers ) {
  83. xhr.setRequestHeader(i, headers[i] );
  84. }
  85. if (options.xhrFields) {
  86. for (var key in options.xhrFields) {
  87. if (key in xhr) {
  88. xhr[key] = options.xhrFields[key];
  89. }
  90. }
  91. }
  92. xhr.responseType = dataType;
  93. xhr.send(data);
  94. },
  95. abort: function(){
  96. jqXHR.abort();
  97. }
  98. };
  99. }
  100. });