User:Monster Domosed/common.js: Difference between revisions

No edit summary
No edit summary
Line 36: Line 36:


   function renderWidget($root, items) {
   function renderWidget($root, items) {
     var $list = $('<ul class="vector-menu-content-list recent-changes-list"></ul>');
     $root.empty();


     items.forEach(function (item, index) {
     items.forEach(function (item, index) {
Line 72: Line 72:


       $li.append($titleLink).append($meta);
       $li.append($titleLink).append($meta);
       $list.append($li);
       $root.append($li);
     });
     });


Line 83: Line 83:


     $moreLi.append($moreLink);
     $moreLi.append($moreLink);
     $list.append($moreLi);
     $root.append($moreLi);
 
    $root.empty().append($list);
   }
   }


Line 110: Line 108:
       function () {
       function () {
         $root.html(
         $root.html(
           '<div class="recent-changes-error">Could not load recent changes.</div>'
           '<li class="recent-changes-error">Could not load recent changes.</li>'
         );
         );
       }
       }
Line 127: Line 125:
   }
   }


   function getExistingPortletWidget() {
   function getExistingPortletList() {
    // Adjust selector if your actual portlet id differs
     var $portlet = $('#p-Recent_Changes, #p-RecentChanges').first();
     var $portlet = $('#p-Recent_Changes, #p-RecentChanges').first();
     if (!$portlet.length) {
     if (!$portlet.length) {
Line 134: Line 131:
     }
     }


     var $widget = $portlet.find('.recent-changes-widget').first();
     var $list = $portlet.find('.vector-menu-content-list').first();
    if (!$widget.length) {
      $widget = $('<div class="recent-changes-widget" data-limit="4" data-refresh="300000"></div>');


       // Put it inside the existing portlet content, not as a new sidebar section
    if (!$list.length) {
       var $content = $portlet.find('.vector-menu-content, .mw-portlet-body, .body').first();
       $list = $('<ul class="vector-menu-content-list recent-changes-list"></ul>')
        .attr('data-limit', '4')
        .attr('data-refresh', '300000');
 
       var $content = $portlet.find('.vector-menu-content').first();
       if ($content.length) {
       if ($content.length) {
         $content.append($widget);
         $content.append($list);
       } else {
       } else {
         $portlet.append($widget);
         $portlet.append($list);
      }
    } else {
      $list.addClass('recent-changes-list');
 
      if (!$list.data('limit')) {
        $list.attr('data-limit', '4');
      }
      if (!$list.data('refresh')) {
        $list.attr('data-refresh', '300000');
       }
       }
     }
     }


     return $widget;
     return $list;
   }
   }


   $(function () {
   $(function () {
     var $widget = getExistingPortletWidget();
     var $widget = getExistingPortletList();
     if ($widget.length) {
     if ($widget.length) {
       initWidget($widget);
       initWidget($widget);