Jump to content

Widget:UPDEvents: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
<includeonly>
<includeonly>
<div id="upd-events-widget">
<div id="upd-events-widget" class="upd-events-widget">
   <div class="upd-events-loading">Loading upcoming events...</div>
   <div class="upd-events-loading">Loading upcoming events...</div>
   <ul class="upd-events-list" style="display:none;"></ul>
   <ul class="upd-events-list" style="display:none;"></ul>
</div>
</div>
<style>
.upd-events-widget {
  color: #000;
  font-size: 15px;
}
.upd-events-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.upd-events-list li {
  margin: 0;
  padding: 10px 0;
  border-bottom: 1px solid #e5e5e5;
}
.upd-events-list li:last-child {
  border-bottom: none;
}
.upd-events-list a {
  color: #0E6021 !important;
  text-decoration: none;
  font-weight: 600;
}
.upd-events-list a:hover {
  text-decoration: underline;
}
.upd-events-loading,
.upd-events-empty {
  color: #555;
  font-size: 14px;
}
</style>


<script>
<script>
(function () {
(function () {
   var container = document.getElementById('upd-events-widget');
   var container = document.getElementById('upd-events-widget');
   if (!container || container.dataset.loaded === '1') return;
 
   if (!container || container.dataset.loaded === '1') {
    return;
  }
 
   container.dataset.loaded = '1';
   container.dataset.loaded = '1';


   var loading = container.querySelector('.upd-events-loading');
   var loading = container.querySelector('.upd-events-loading');
   var pageUrl = 'https://upd.edu.ph/events-list/';
   var list = container.querySelector('.upd-events-list');
 
  loading.textContent =
    'Widget JavaScript is running. Requesting UP Diliman...';
 
  var endpoint = new URL(
    'https://upd.edu.ph/wp-admin/admin-ajax.php'
  );
 
  var params = {
    action: 'search_events',
    id: '1',
    scope: 'future',
    orderby: 'event_start_date,event_start_time,event_name',
    view: 'list-grouped',
    pno: '1'
  };
 
  Object.keys(params).forEach(function (key) {
    endpoint.searchParams.set(key, params[key]);
  });
 
  var finished = false;
 
  var timeout = setTimeout(function () {
    if (finished) return;
 
    finished = true;
    loading.textContent =
      'The UP Diliman request timed out or is blocked.';
  }, 10000);
 
  fetch(endpoint.toString(), {
    method: 'GET',
    mode: 'cors',
    credentials: 'omit'
  })
  .then(function (response) {
    if (finished) return null;
 
    if (!response.ok) {
      throw new Error('HTTP ' + response.status);
    }
 
    return response.text();
  })
  .then(function (html) {
    if (finished || html === null) return;
 
    finished = true;
    clearTimeout(timeout);
 
    var doc = new DOMParser().parseFromString(html, 'text/html');
 
    var anchors = doc.querySelectorAll(
      '#em-events-list-grouped-1 a[href*="/events/"], ' +
      '.em-events-list a[href*="/events/"]'
    );
 
    var seen = new Set();
    var events = [];
 
    anchors.forEach(function (anchor) {
      var heading = anchor.querySelector('h4');
      if (!heading) return;
 
      var title = heading.textContent.trim();
      var url;
 
      try {
        url = new URL(
          anchor.getAttribute('href'),
          'https://upd.edu.ph'
        ).href;
      } catch (e) {
        return;
      }


  loading.textContent = 'Testing connection to UP Diliman...';
      if (!title || seen.has(url)) return;


  fetch(pageUrl)
       if (!/^https:\/\/upd\.edu\.ph\/events\//i.test(url)) {
    .then(function (response) {
         return;
       if (!response.ok) {
         throw new Error('HTTP ' + response.status);
       }
       }
      return response.text();
    })
    .then(function (html) {
      var doc = new DOMParser().parseFromString(html, 'text/html');
      var anchors = doc.querySelectorAll(
        '#em-events-list-grouped-1 a[href*="/events/"]'
      );


      seen.add(url);
      events.push({
        title: title,
        url: url
      });
    });
    list.replaceChildren();
    if (events.length === 0) {
       loading.textContent =
       loading.textContent =
         'Page received (' + html.length +
         'The request succeeded, but no event titles were found.';
        ' characters). Event links found: ' + anchors.length;
 
     })
      return;
     .catch(function (error) {
     }
       loading.textContent = 'Connection failed: ' + error.message;
 
       console.error('UPEvents diagnostic:', error);
     events.slice(0, 5).forEach(function (event) {
       var li = document.createElement('li');
      var a = document.createElement('a');
 
      a.href = event.url;
      a.textContent = event.title;
      a.target = '_blank';
      a.rel = 'noopener noreferrer';
 
      li.appendChild(a);
       list.appendChild(li);
     });
     });
    loading.style.display = 'none';
    list.style.display = 'block';
  })
  .catch(function (error) {
    if (finished) return;
    finished = true;
    clearTimeout(timeout);
    console.error('UPEvents widget error:', error);
    loading.textContent =
      'Request failed: ' +
      error.name + ' — ' + error.message;
  });
})();
})();
</script>
</script>
</includeonly>
</includeonly>

Latest revision as of 03:24, 29 October 2026