@osimatic/helpers-js 1.5.4 → 1.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/media.js CHANGED
@@ -1,3 +1,6 @@
1
+ const { FormHelper } = require('./form_helper');
2
+ const { HTTPClient } = require('./http_client');
3
+
1
4
  class AudioMedia {
2
5
 
3
6
  static getPlayer(playUrl) {
@@ -6,37 +9,42 @@ class AudioMedia {
6
9
 
7
10
  static initPlayLinks(div) {
8
11
  // Affiche un lecteur audio
9
- div.find('.play_link').off('click').click(function () {
10
- let audio = $(AudioMedia.getPlayer($(this).data('play_url')));
11
- audio[0].play();
12
- $(this).after(audio);
13
- $(this).remove();
14
- return false;
12
+ div.querySelectorAll('.play_link').forEach(link => {
13
+ link.addEventListener('click', function(e) {
14
+ e.preventDefault();
15
+ const template = document.createElement('template');
16
+ template.innerHTML = AudioMedia.getPlayer(this.dataset.play_url);
17
+ const audio = template.content.firstChild;
18
+ audio.play();
19
+ this.insertAdjacentElement('afterend', audio);
20
+ this.remove();
21
+ });
15
22
  });
16
23
 
17
- div.find('.play_asynchronously_link').off('click').click(function () {
18
- //if (FormHelper.buttonLoader($(this), 'loading') != null) {
19
- let button = FormHelper.buttonLoader($(this), 'loading');
20
- AudioMedia.playAudioUrl($(this).data('url'), () => FormHelper.buttonLoader(button, 'reset'));
21
- //} else {
22
- // let button = $(this).attr('disabled', true).button('loading');
23
- // AudioMedia.playAudioUrl($(this).data('url'), () => button.attr('disabled', false).button('reset'));
24
- //}
25
- return false;
24
+ div.querySelectorAll('.play_asynchronously_link').forEach(link => {
25
+ link.addEventListener('click', function(e) {
26
+ e.preventDefault();
27
+ let button = FormHelper.buttonLoader(this, 'loading');
28
+ AudioMedia.playAudioUrl(this.dataset.url, () => FormHelper.buttonLoader(button, 'reset'));
29
+ });
26
30
  });
27
31
 
28
- div.find('.modal_play_link').off('click').click(function () {
29
- $('#modal_voice_message_play').on('show.bs.modal', function (event) {
30
- let button = $(event.relatedTarget);
31
- let modal = $(this);
32
-
33
- let player = modal.find('audio');
34
- player.prop('src', button.data('play_url'));
35
- player.play();
32
+ div.querySelectorAll('.modal_play_link').forEach(link => {
33
+ link.addEventListener('click', function(e) {
34
+ e.preventDefault();
35
+ const modalEl = document.getElementById('modal_voice_message_play');
36
+ if (!modalEl) return;
37
+ const currentLink = this;
38
+ modalEl.addEventListener('show.bs.modal', function handler(event) {
39
+ modalEl.removeEventListener('show.bs.modal', handler);
40
+ const player = modalEl.querySelector('audio');
41
+ if (player) {
42
+ player.src = (event.relatedTarget || currentLink).dataset.play_url;
43
+ player.play();
44
+ }
45
+ });
46
+ bootstrap.Modal.getOrCreateInstance(modalEl).show(this);
36
47
  });
37
-
38
- $('#modal_voice_message_play').modal('show', $(this));
39
- return false;
40
48
  });
41
49
  }
42
50
 
@@ -117,12 +125,12 @@ class AudioMedia {
117
125
 
118
126
  class VideoMedia {
119
127
  static initPlayPauseClick(videoElement) {
120
- $(videoElement).click(function(e) {
128
+ videoElement.addEventListener('click', function(e) {
121
129
  // handle click if not Firefox (Firefox supports this feature natively)
122
130
  if (typeof InstallTrigger === 'undefined') {
123
131
  // get click position
124
- let clickY = (e.pageY - $(this).offset().top);
125
- let height = parseFloat( $(this).height() );
132
+ let clickY = e.pageY - (this.getBoundingClientRect().top + window.scrollY);
133
+ let height = this.offsetHeight;
126
134
 
127
135
  // avoids interference with controls
128
136
  if (clickY > 0.82*height) return;
@@ -1,8 +1,10 @@
1
1
  require('./string');
2
2
  const { FlashMessage } = require('./flash_message');
3
+ const { toEl } = require('./util');
3
4
 
4
5
  class MultiFilesInput {
5
6
  static init(fileInput, setFilesList, nbMaxFiles, maxFileSize) {
7
+ fileInput = toEl(fileInput);
6
8
  let filesList = [];
7
9
  const formGroup = fileInput.closest('.form-group');
8
10
 
@@ -1,3 +1,4 @@
1
+ const { toEl } = require('./util');
1
2
 
2
3
  class MultipleActionInTable {
3
4
 
@@ -5,6 +6,7 @@ class MultipleActionInTable {
5
6
  // Idempotent : sans effet si les colonnes existent déjà.
6
7
  // Doit être appelé AVANT l'initialisation DataTable.
7
8
  static initCols(table, cellSelector = 'select') {
9
+ table = toEl(table);
8
10
  if (!table.classList.contains('table-action_multiple')) {
9
11
  return;
10
12
  }
@@ -12,8 +14,9 @@ class MultipleActionInTable {
12
14
  return;
13
15
  }
14
16
 
15
- if (!table.querySelector('thead tr th[data-key="select"]')) {
16
- table.querySelector('thead tr').insertAdjacentHTML('afterbegin', '<th class="' + cellSelector + '" data-key="select"></th>');
17
+ const theadTr = table.querySelector('thead tr');
18
+ if (theadTr && !theadTr.querySelector('th[data-key="select"]')) {
19
+ theadTr.insertAdjacentHTML('afterbegin', '<th class="' + cellSelector + '" data-key="select"></th>');
17
20
  }
18
21
  table.querySelectorAll('tbody tr:not(.no_items)').forEach(tr => {
19
22
  if (!tr.querySelector('td.select')) {
@@ -25,6 +28,7 @@ class MultipleActionInTable {
25
28
  // Initialise les colonnes (via initCols) puis branche les event handlers.
26
29
  // Peut être appelé après l'initialisation DataTable.
27
30
  static init(table, options = {}) {
31
+ table = toEl(table);
28
32
  const { cellSelector = 'select', imgArrow = '' } = options;
29
33
 
30
34
  if (!table.classList.contains('table-action_multiple')) {
@@ -140,6 +144,7 @@ class MultipleActionInTable {
140
144
  class MultipleActionInDivList {
141
145
  // init checkbox
142
146
  static init(contentDiv, options = {}) {
147
+ contentDiv = toEl(contentDiv);
143
148
  const { imgArrow = '' } = options;
144
149
 
145
150
  let buttonsDiv = MultipleActionInDivList.getButtonsDiv(contentDiv);
@@ -253,115 +258,4 @@ class MultipleActionInDivList {
253
258
  }
254
259
  }
255
260
 
256
- module.exports = { MultipleActionInTable, MultipleActionInDivList };
257
-
258
- /*
259
- // init checkbox
260
- function initTableActionMultiple(table) {
261
- if (!table.hasClass('table-action_multiple')) {
262
- return;
263
- }
264
-
265
- var divBtn = tableActionMultipleGetDivBtn(table);
266
- if (divBtn == null) {
267
- return;
268
- }
269
-
270
- if (table.find('thead tr th[data-key="select"]').length === 0) {
271
- table.find('thead tr').prepend($('<th class="select no-sort" data-key="select"></th>'));
272
- }
273
- table.find('tbody tr:not(.no_items)').each(function(idx, tr) {
274
- if ($(tr).find('td.select').length === 0) {
275
- $(tr).prepend($('<td class="select"><input type="checkbox" class="action_multiple_checkbox" name="'+$(tr).data('action_multiple_input_name')+'" value="'+$(tr).data('action_multiple_item_id')+'"></td>'));
276
- }
277
- });
278
-
279
- table.find('input.action_multiple_checkbox').each(function(idx, el) {
280
- var th = $(el).closest('table').find('thead tr th').first();
281
- if (th.find('input').length === 0) {
282
- // console.log(th);
283
- th.html('<input type="checkbox" class="action_multiple_check_all" />');
284
- // th.html('Coucou');
285
- }
286
- });
287
-
288
- table.find('input.action_multiple_checkbox').change(function() {
289
- majCheckbox(table);
290
- });
291
-
292
- table.find('input.action_multiple_check_all').off('click').click(function() {
293
- var table = $(this).closest('table');
294
- var checkbox = table.find('input.action_multiple_checkbox');
295
- var checkboxChecked = table.find('input.action_multiple_checkbox:checked');
296
- if (checkbox.length === checkboxChecked.length) {
297
- checkbox.prop('checked', false);
298
- }
299
- else {
300
- checkbox.prop('checked', true);
301
- }
302
- majCheckbox(table);
303
- });
304
- }
305
-
306
- function majCheckbox(table) {
307
- showButtonsAction(table);
308
-
309
- var allCheckbox = table.find('input.action_multiple_checkbox');
310
- var allCheckboxChecked = table.find('input.action_multiple_checkbox:checked');
311
- var checkboxSelectAll = table.find('thead tr th input.action_multiple_check_all');
312
- if (allCheckbox.length === allCheckboxChecked.length) {
313
- checkboxSelectAll.prop('checked', true);
314
- }
315
- else {
316
- checkboxSelectAll.prop('checked', false);
317
- }
318
- }
319
-
320
- function tableActionMultipleGetDivBtn(table) {
321
- var divTableResponsive = table.parent();
322
- var divBtn = divTableResponsive.next();
323
- if (divBtn.hasClass('action_multiple_buttons')) {
324
- return divBtn;
325
- }
326
- divBtn = divTableResponsive.parent().parent().parent().next();
327
- if (divBtn.hasClass('action_multiple_buttons')) {
328
- return divBtn;
329
- }
330
- return null;
331
- }
332
-
333
- function showButtonsAction(table) {
334
- var divBtn = tableActionMultipleGetDivBtn(table);
335
- if (divBtn == null) {
336
- return;
337
- }
338
-
339
- // console.log(divBtn);
340
- //var nbItems = $('input[name="' + checkbox.attr('name') + '"]:checked').length;
341
- var nbItems = table.find('input.action_multiple_checkbox:checked').length;
342
-
343
- if (nbItems > 0 && divBtn.is(':hidden')) {
344
- divBtn.removeClass('hide');
345
- }
346
- else if (nbItems === 0 && divBtn.is(':visible')) {
347
- divBtn.addClass('hide');
348
- }
349
-
350
- // affichage aucune action possible si aucun bouton n'est visible
351
- if (divBtn.is(':visible')) {
352
- divBtn.find('span.no_button').remove();
353
- if (divBtn.find('button:visible, a:visible').length === 0) {
354
- divBtn.find('img').after('<span class="no_button"><em>aucune action possible</em></span>');
355
- }
356
- }
357
- }
358
-
359
- $(function() {
360
- $('.action_multiple_buttons').prepend($('<img src="'+ROOT_PATH+DOSSIER_IMAGES+'arrow_ltr.png" alt="" /> &nbsp;'));
361
- $('.action_multiple_buttons').append($('<br/><br/>'));
362
-
363
- $('table.table-action_multiple').each(function(idx, table) {
364
- initTableActionMultiple($(table));
365
- });
366
- });
367
- */
261
+ module.exports = { MultipleActionInTable, MultipleActionInDivList };
@@ -1,4 +1,5 @@
1
1
  const L = require('leaflet');
2
+ const { toEl } = require('./util');
2
3
 
3
4
  /**
4
5
  * https://leafletjs.com/
@@ -8,6 +9,7 @@ const L = require('leaflet');
8
9
  class OpenStreetMap {
9
10
 
10
11
  constructor(mapContainer, options={}) {
12
+ mapContainer = toEl(mapContainer);
11
13
  /*let [lat, lng] = button.data('coordinates').split(',');
12
14
  let map = L.map('modal_map_canvas2').setView([lat, lng], 17);
13
15
 
@@ -27,6 +29,7 @@ class OpenStreetMap {
27
29
  }
28
30
 
29
31
  static createMap(mapContainer, options={}) {
32
+ mapContainer = toEl(mapContainer);
30
33
  if (!mapContainer.length) {
31
34
  return null;
32
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.5.4",
3
+ "version": "1.5.6",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "jest",
package/paging.js CHANGED
@@ -2,17 +2,21 @@
2
2
  //Bootstrap class pagination https://getbootstrap.com/docs/3.3/components/#pagination
3
3
 
4
4
  const { UrlAndQueryString } = require('./network');
5
+ const { toEl } = require('./util');
5
6
 
6
7
  class Pagination {
7
8
  static paginateCards(div, nbItemsPerPage) {
9
+ div = toEl(div);
8
10
  Pagination.paginate(div, div.querySelectorAll('.pagination_item'), nbItemsPerPage, null);
9
11
  }
10
12
 
11
13
  static paginateTable(table, select=null) {
14
+ table = toEl(table); select = toEl(select);
12
15
  Pagination.paginate(table, table.querySelectorAll('tbody tr:not(.hide)'), parseInt(table.dataset.max_rows), select);
13
16
  }
14
17
 
15
18
  static paginate(div, items, nbItemsPerPage, select=null, labelDisplayAll=null) {
19
+ div = toEl(div); select = toEl(select);
16
20
  let maxItems = nbItemsPerPage;
17
21
 
18
22
  if (!div) {
package/select_all.js CHANGED
@@ -1,8 +1,11 @@
1
+ const { toEl } = require('./util');
2
+
1
3
  class SelectAll {
2
4
 
3
5
  // Dans un form-group
4
6
 
5
7
  static initLinkInFormGroup(link) {
8
+ link = toEl(link);
6
9
  const linkClone = link.cloneNode(true);
7
10
  link.parentElement.replaceChild(linkClone, link);
8
11
  linkClone.addEventListener('click', function(e) {
@@ -43,6 +46,7 @@ class SelectAll {
43
46
  // Dans tableau
44
47
 
45
48
  static initInTable(table) {
49
+ table = toEl(table);
46
50
  const inputCheckAll = table.querySelector('tr input.check_all');
47
51
  if (!inputCheckAll) {
48
52
  return;
@@ -78,6 +82,7 @@ class SelectAll {
78
82
  // Dans un div
79
83
 
80
84
  static initDiv(contentDiv) {
85
+ contentDiv = toEl(contentDiv);
81
86
  contentDiv.querySelectorAll('input.check_all').forEach(inputCheckAll => {
82
87
  const div = inputCheckAll.closest('div.checkbox_with_check_all');
83
88
 
package/sortable_list.js CHANGED
@@ -1,5 +1,8 @@
1
+ const { toEl } = require('./util');
2
+
1
3
  class SortableList {
2
4
  static init(sortableList, clientYOffset=0) {
5
+ sortableList = toEl(sortableList);
3
6
  sortableList.querySelectorAll('[draggable="true"]').forEach(item => {
4
7
  item.addEventListener('dragstart', () => {
5
8
  // Adding dragging class to an item after a delay