application.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // This is a manifest file that'll be compiled into including all the files listed below.
  2. // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
  3. // be included in the compiled file accessible from http://example.com/assets/application.js
  4. // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
  5. // the compiled file.
  6. //
  7. //= require ./app/lib/core/jquery-3.6.0.js
  8. //= require ./app/lib/core/jquery-ui-1.11.4.js
  9. //= require ./app/lib/core/underscore-1.8.3.js
  10. //= require ./app/lib/animations/velocity.min.js
  11. //= require ./app/lib/animations/velocity.ui.js
  12. //not_used= require_tree ./app/lib/spine
  13. //= require ./app/lib/spine/spine.coffee
  14. //= require ./app/lib/spine/ajax.coffee
  15. //= require ./app/lib/spine/local.coffee
  16. //= require ./app/lib/spine/route.coffee
  17. //= require ./app/lib/flot/jquery.flot.js
  18. //= require ./app/lib/flot/jquery.flot.selection.js
  19. //not_used= require_tree ./app/lib/bootstrap
  20. //= require ./app/lib/bootstrap/dropdown.js
  21. //= require ./app/lib/bootstrap/tooltip.js
  22. //= require ./app/lib/bootstrap/popover.js
  23. //= require ./app/lib/bootstrap/popover-enhance.js
  24. // modified by Felix Jan-2014
  25. //= require ./app/lib/bootstrap/modal.js
  26. //= require ./app/lib/bootstrap/tab.js
  27. //= require ./app/lib/bootstrap/transition.js
  28. //= require ./app/lib/bootstrap/button.js
  29. //= require ./app/lib/bootstrap/collapse.js
  30. //= require ./app/lib/bootstrap/bootstrap-timepicker.js
  31. //= require ./app/lib/bootstrap/bootstrap-datepicker.js
  32. //= require ./app/lib/rangy/rangy-core.js
  33. //= require ./app/lib/rangy/rangy-classapplier.js
  34. //= require ./app/lib/rangy/rangy-textrange.js
  35. //= require ./app/lib/rangy/rangy-highlighter.js
  36. //= require_tree ./app/lib/base
  37. //= require ./app/index.coffee
  38. // IE8 workaround for missing console.log
  39. if (!window.console) {
  40. window.console = {}
  41. }
  42. if (!console.log) {
  43. console.log = function(){}
  44. }
  45. // No-op to mark translatable strings.
  46. function __(str) {
  47. return str
  48. }
  49. function escapeRegExp(str) {
  50. return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  51. }
  52. Date.prototype.getWeek = function() {
  53. var onejan = new Date(this.getFullYear(),0,1);
  54. return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
  55. }
  56. function difference(object1, object2, depth = 0, maxDepth = 10) {
  57. object1 = object1 || {};
  58. object2 = object2 || {};
  59. var changes = {};
  60. if (depth > maxDepth) {
  61. return changes;
  62. }
  63. _.uniq(Object.keys(object1).concat(Object.keys(object2))).forEach(function(name) {
  64. if (name in object1 && name in object2) {
  65. if (_.isObject(object1[name]) && !_.isArray(object1[name]) && _.isObject(object2[name]) && !_.isArray(object2[name])) {
  66. var diff = difference(object1[name], object2[name], depth + 1, maxDepth);
  67. if (!_.isEmpty(diff)) {
  68. changes[name] = diff;
  69. }
  70. } else if (!_.isEqual(object1[name], object2[name])) {
  71. changes[name] = object2[name];
  72. }
  73. } else {
  74. changes[name] = object2[name]
  75. }
  76. })
  77. return changes;
  78. }
  79. // returns the byte length of an utf8 string
  80. // taken from http://stackoverflow.com/questions/5515869/string-length-in-bytes-in-javascript
  81. function byteLength(str) {
  82. var s = str.length
  83. for (var i=str.length-1; i>=0; i--) {
  84. var code = str.charCodeAt(i)
  85. if (code > 0x7f && code <= 0x7ff) s++
  86. else if (code > 0x7ff && code <= 0xffff) s+=2
  87. if (code >= 0xDC00 && code <= 0xDFFF) i-- //trail surrogate
  88. }
  89. return s
  90. }
  91. // clone, just data, no instances of objects
  92. function clone(item, full) {
  93. // just return/clone false conditions
  94. if (!item) { return item }
  95. var itemType = item.constructor.name
  96. // IE behavior // doesn't know item.constructor.name, detect it by underscore
  97. if (itemType === undefined) {
  98. if (_.isArray(item)) {
  99. itemType = 'Array'
  100. }
  101. else if (_.isNumber(item)) {
  102. itemType = 'Number'
  103. }
  104. else if (_.isString(item)) {
  105. itemType = 'String'
  106. }
  107. else if (_.isBoolean(item)) {
  108. itemType = 'Boolean'
  109. }
  110. else if (_.isFunction(item)) {
  111. itemType = 'Function'
  112. }
  113. else if (_.isObject(item)) {
  114. itemType = 'Object'
  115. }
  116. }
  117. // ignore certain objects
  118. var acceptedInstances = [ 'Object', 'Number', 'String', 'Boolean', 'Array' ]
  119. if (full) {
  120. acceptedInstances.push('Function')
  121. }
  122. // check if item is accepted to get cloned
  123. if (itemType && !_.contains(acceptedInstances, itemType)) {
  124. console.log('no acceptedInstances', itemType, item)
  125. console.trace()
  126. return
  127. }
  128. // copy array
  129. var result;
  130. if (itemType == 'Array') {
  131. result = []
  132. item.forEach(function(child, index, array) {
  133. result[index] = clone( child, full )
  134. });
  135. }
  136. // copy function
  137. else if (itemType == 'Function') {
  138. result = item.bind({})
  139. }
  140. // copy object
  141. else if (itemType == 'Object') {
  142. result = {}
  143. for(var key in item) {
  144. if (item.hasOwnProperty(key)) {
  145. result[key] = clone(item[key], full)
  146. }
  147. }
  148. }
  149. // copy others
  150. else {
  151. result = item
  152. }
  153. return result
  154. }
  155. // taken from https://github.com/epeli/underscore.string/blob/master/underscored.js
  156. function underscored(str) {
  157. return str.trim().replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase();
  158. }
  159. function toCamelCase(str) {
  160. return str
  161. .replace(/\s(.)/g, function($1) { return $1.toUpperCase(); })
  162. .replace(/\s/g, '')
  163. .replace(/^(.)/, function($1) { return $1.toUpperCase(); });
  164. };
  165. function isRetina(){
  166. if (window.matchMedia) {
  167. var mq = window.matchMedia("only screen and (min--moz-device-pixel-ratio: 1.3), only screen and (-o-min-device-pixel-ratio: 2.6/2), only screen and (-webkit-min-device-pixel-ratio: 1.3), only screen and (min-device-pixel-ratio: 1.3), only screen and (min-resolution: 1.3dppx)");
  168. return (mq && mq.matches || (window.devicePixelRatio > 1));
  169. }
  170. }
  171. jQuery.event.special.remove = {
  172. remove: function(e) {
  173. if (e.handler) e.handler();
  174. }
  175. };
  176. // checkbox-replacement helper
  177. // native checkbox focus behaviour is the following:
  178. // tab to checkbox: :focus state and focus outline
  179. // click on checkbox: :focus state but no focus outline
  180. $('body').on('click', '.checkbox-replacement, .radio-replacement', function(event){
  181. $(event.currentTarget).find('input').addClass('is-active')
  182. });
  183. $('body').on('blur', '.checkbox-replacement input, .radio-replacement input', function(){
  184. $(this).removeClass('is-active')
  185. });
  186. // remove attributes by regex
  187. // http://stackoverflow.com/questions/8968767/remove-multiple-html5-data-attributes-with-jquery
  188. jQuery.fn.removeAttrs = function(regex) {
  189. return this.each(function() {
  190. var $this = $(this),
  191. names = [];
  192. $.each(this.attributes, function(i, attr) {
  193. if (attr && attr.specified && regex.test(attr.name)) {
  194. $this.removeAttr(attr.name);
  195. }
  196. });
  197. });
  198. };
  199. // based on jquery serializeArray
  200. // changes
  201. // - set type based on data('field-type')
  202. // - also catch [disabled] params
  203. // - return multiple type to make sure that the data is always array
  204. jQuery.fn.extend( {
  205. serializeArrayWithType: function() {
  206. var r20 = /%20/g,
  207. rbracket = /\[\]$/,
  208. rCRLF = /\r?\n/g,
  209. rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
  210. rsubmittable = /^(?:input|select|textarea|keygen)/i;
  211. var rcheckableType = ( /^(?:checkbox|radio)$/i );
  212. return this.map( function() {
  213. // We dont use jQuery.prop( this, "elements" ); here anymore
  214. // because it did not work out for IE 11
  215. var elements = $(this).find('*').filter(':input');
  216. return elements ? jQuery.makeArray( elements ) : this;
  217. } )
  218. .filter( function() {
  219. var type = this.type;
  220. return this.name &&
  221. rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
  222. ( this.checked || !rcheckableType.test( type ) );
  223. } )
  224. .map( function( i, elem ) {
  225. var $elem = jQuery( this );
  226. var val = $elem.val();
  227. var type = $elem.data('field-type');
  228. var multiple = $elem.prop('multiple');
  229. // in jQuery 3, select-multiple with nothing selected returns an empty array
  230. // https://jquery.com/upgrade-guide/3.0/#breaking-change-select-multiple-with-nothing-selected-returns-an-empty-array
  231. if (multiple === true && typeof val === 'object' && val.length == 0){
  232. val = null;
  233. }
  234. var result;
  235. if ( val == null ) {
  236. // be sure that also null values are transferred
  237. // https://github.com/zammad/zammad/issues/944
  238. if ($elem.prop('multiple')) {
  239. result = { name: elem.name, value: null, type: type, multiple: multiple }
  240. } else {
  241. result = null
  242. }
  243. }
  244. else if ( Array.isArray( val ) || multiple) {
  245. result = jQuery.map( val, function( val ) {
  246. return { name: elem.name, value: val.replace( rCRLF, "\r\n" ), type: type, multiple: multiple };
  247. } );
  248. }
  249. else {
  250. result = { name: elem.name, value: val.replace( rCRLF, "\r\n" ), type: type, multiple: multiple };
  251. }
  252. return result;
  253. } ).get();
  254. }
  255. } );
  256. // Add support for "passive" events in jQuery listeners.
  257. // https://stackoverflow.com/a/62177358/17674471
  258. // TODO: This might be already fixed in jQuery 4, evaluate and remove the block below if that's the case.
  259. jQuery.event.special.touchstart = {
  260. setup: function( _, ns, handle ) {
  261. this.addEventListener('touchstart', handle, { passive: !ns.includes('noPreventDefault') });
  262. }
  263. };
  264. jQuery.event.special.touchmove = {
  265. setup: function( _, ns, handle ) {
  266. this.addEventListener('touchmove', handle, { passive: !ns.includes('noPreventDefault') });
  267. }
  268. };
  269. jQuery.event.special.wheel = {
  270. setup: function( _, ns, handle ){
  271. this.addEventListener('wheel', handle, { passive: true });
  272. }
  273. };
  274. jQuery.event.special.mousewheel = {
  275. setup: function( _, ns, handle ){
  276. this.addEventListener('mousewheel', handle, { passive: true });
  277. }
  278. };
  279. // start application
  280. (function(){
  281. new App.Run();
  282. })();