Dotclear

Changeset 3869:97af47e1ee7d for admin/js


Ignore:
Timestamp:
09/11/18 15:45:42 (7 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Add badge capabilities (used mainly by dashboard modules)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • admin/js/common.js

    r3858 r3869  
    171171  }; 
    172172  var singleExpander = function singleExpander(line, callback) { 
    173     $('<button type="button" class="details-cmd" aria-label="' + dotclear.img_plus_alt + '">' + dotclear.img_plus_txt + '</button>').click(function (e) { 
     173    $('<button type="button" class="details-cmd" aria-label="' + dotclear.img_plus_alt + '">' + dotclear.img_plus_txt + '</button>').click(function(e) { 
    174174      toggleArrow(this); 
    175175      callback(line); 
     
    178178  }; 
    179179  var multipleExpander = function multipleExpander(line, lines, callback) { 
    180     $('<button type="button" class="details-cmd" aria-label="' + dotclear.img_plus_alt + '">' + dotclear.img_plus_txt + '</button>').click(function (e) { 
     180    $('<button type="button" class="details-cmd" aria-label="' + dotclear.img_plus_alt + '">' + dotclear.img_plus_txt + '</button>').click(function(e) { 
    181181      var action = toggleArrow(this); 
    182182      lines.each(function() { 
     
    446446      window.open($(this).attr('href')); 
    447447    }); 
    448   } 
     448  }, 
     449 
     450  badge: function($elt, options = null) { 
     451    // Cope with selector given as string or DOM element rather than a jQuery object 
     452    if (typeof $elt === 'string' || $elt instanceof Element) { 
     453      $elt = $($elt); 
     454    } 
     455 
     456    // Return if elt does not exist 
     457    if (!$elt.length) return; 
     458 
     459    // Cope with options 
     460    var opt = $.extend({ 
     461      /* sibling: define if the given element is a sibling of the badge or it's parent 
     462       *  true: use $elt.after() to add badge 
     463       *  false: use $elt.parent().append() to add badge (default) 
     464       */ 
     465      sibling: false, 
     466      /* id: badge unique class 
     467       *  this class will be used to delete all corresponding badge (used for removing and updating) 
     468       */ 
     469      id: 'default', 
     470      /* remove: will remove the badge if set to true */ 
     471      remove: false, 
     472      /* value: badge value */ 
     473      value: null, 
     474      /* inline: if set to true, the badge is an inline element (useful for menu item) rather than a block */ 
     475      inline: false, 
     476      /* icon: if set to true, the badge is attached to a dashboard icon (needed for correct positionning) */ 
     477      icon: false, 
     478      /* type: Override default background (which may vary) 
     479       *  by default badge background are soft grey for dashboard icons (see opt.icon) and bright red for all other elements 
     480       *  possible values: 
     481       *    'std':  bright red 
     482       *    'info': blue 
     483       *    'soft': soft grey 
     484       */ 
     485      type: '', 
     486      /* classes: additionnal badge classes */ 
     487      classes: '' 
     488    }, options); 
     489 
     490    // Set some constants 
     491    const classid = 'span.badge.badge-' + opt.id; // Pseudo unique class 
     492 
     493    // Set badgeable class to elt parent's (if sibling) or elt itself, if it is necessary 
     494    var $parent = (opt.sibling ? $elt.parent() : $elt); 
     495    if (!opt.inline && !opt.remove && !$parent.hasClass('badgeable')) { 
     496      $parent.addClass('badgeable'); 
     497    } 
     498 
     499    // Remove existing badge if exists 
     500    var $badge = (opt.sibling ? $parent.children(classid) : $elt.children(classid)); 
     501    if ($badge.length) { 
     502      $badge.remove(); 
     503    } 
     504 
     505    if (!opt.remove && opt.value !== null) { 
     506      // Add the new badge 
     507      const cls = 'badge badge-' + opt.id + ' ' + 
     508        (opt.inline ? 'badge-inline' : 'badge-block') + 
     509        (opt.icon ? ' badge-icon' : '') + 
     510        (opt.type !== '' ? ' badge-' + opt.type : '') + 
     511        (opt.classes !== '' ? ' ' + opt.classes : ''); 
     512      const xml = '<span class="' + cls + '" aria-hidden="true">' + opt.value + '</span>'; 
     513      if (opt.sibling) { 
     514        // Add badge after it's sibling 
     515        $elt.after(xml); 
     516      } else { 
     517        // Append badge to the elt 
     518        $elt.append(xml); 
     519      } 
     520    } 
     521  } 
     522 
    449523}; 
    450524/* On document ready 
Note: See TracChangeset for help on using the changeset viewer.

Sites map