ผู้ใช้:Geonuch/common.js/Geonotice.js

จาก วิกิซอร์ซ

หมายเหตุ: หลังเผยแพร่ คุณอาจต้องล้างแคชเว็บเบราว์เซอร์ของคุณเพื่อดูการเปลี่ยนแปลง

  • ไฟร์ฟอกซ์ / ซาฟารี: กด Shift ค้างขณะคลิก Reload หรือกด Ctrl-F5 หรือ Ctrl-R (⌘-R บนแมค)
  • กูเกิล โครม: กด Ctrl-Shift-R (⌘-Shift-R บนแมค)
  • อินเทอร์เน็ตเอกซ์พลอเรอร์ และ Edge: กด Ctrl ค้างขณะคลิก Refresh หรือกด Ctrl-F5
  • โอเปร่า: กด Ctrl-F5
(function() {
var notices = {

GB140322: /* last 6 chars of ID is date of last amendment in YYMMDD format - change this if making major amendment or adding a meetup; leave alone if minor amendment or removing a meetup */
{
   begin: '22 March 2014 00:01 UTC', /* yesterday's date - amend only if the ID was altered */
   end: '30 March 2014 17:00 UTC', /* set this to date of last meetup shown */
   corners: [ [60,-9], [50, 2] ],
   text: "Interested in having a chat with fellow Wikipedians? There are forthcoming meetups in: [[m:Meetup/Manchester/22|Manchester, 23 March]]; [[m:Meetup/Coventry/9|Coventry, 30 March]]; and [[m:Meetup/Edinburgh 5|Edinburgh, 30 March]]!" /* try to limit this to four meetups, no more than one per town/city, and no more than four weeks in advance; shorten month names to three letters if four meetups are shown */
}

/*******************************************
 *             End of list.
 *           Edit only above!
 *
 * Format is:
 *
 * ID:
 * { begin: 'date',
 *   end: 'date',
 *   corners: [ [lat,lon], [lat,lon] ],
 *   text: 'message'
 * }
 *
 * There also is an option to use country instead of corners.  For example:
 * ID:
 * { begin: 'date',
 *   end: 'date',
 *   country: 'US',
 *   text: 'message'
 * }
 *
 * See http://dev.maxmind.com/geoip/legacy/codes/iso3166 for full list of country codes
 *
 * * There should be commas between the notices, but not after the last one. BE SURE TO ESCAPE YOUR APOSTROPHES (put a backslash in front of it, like this: \')!
 * Use [[meta:w:en:link|link]] format for internal links, so that they work properly on the other projects where the geonotice is displayed.
 *
 *******************************************/
};

function geoWikiLinker (str, page, text) {
  text = text || page;
  return mw.html.element(
    'a', {
      href: mw.util.getUrl( page ),
      title: page
    }, text
  );
}

function addGeonoticeCss() {
  mw.util.addCSS( [
    '#watchlist-message .geonotice {',
      'width:78%;',
      'background:transparent;',
      'text-align:left;',
    '}',
    '#watchlist-message .geonotice span {',
      'font-size:106%;',
    '}',
    '#watchlist-message .geonotice small {',
      'font-style:italic;',
      'margin-left:.5em;',
    '}',
    '#watchlist-message .geonotice small a::before {',
      'content: "[";',
    '}',
    '#watchlist-message .geonotice small a::after {',
      'content: "]";',
    '}'
  ].join( ' ' ) );
}

function hideGeonotice(e) {
  e.preventDefault();

  var parentId = $(this).closest('li').attr('id');
  var date = new Date();

  date.setTime(date.getTime()+8640000000);

  var expireDate = date.toGMTString();

  document.cookie = 'hide' + parentId + '=1; expires=' + expireDate + ';path=/';
  
  $( '#' + parentId ).hide();
  $( '#geonotice-hr' ).hide();

  return false;
}

var firstnotice = true;
var regexForInternalLinks = /\[\[([^{|}\[\]\n]+)(?:\|(.*?))?\]\]/g;

function displayGeonotice(notice) {
  var geonoticeText = notice.text.replace( regexForInternalLinks, geoWikiLinker );

  if (firstnotice) {
    firstnotice = false;
    
    $('#watchlist-message').prepend(
      $( '<hr>' ).attr({ 'id' : 'geonotice-hr' })
    );
    
    addGeonoticeCss();
  }

  $('#watchlist-message').prepend(
    $('<li>')
      .attr({
        'class' : 'geonotice plainlinks',
        'id' : 'geonotice' + notice.id
      })
      .append(
        $( '<span>' )
          .html( geonoticeText )
        )
      .append( $( '<small>' )
        .append(
          $('<a>')
            .text( 'hide' )
            .click( hideGeonotice )
            .attr({ 'href' : '#' })
         )
      )
  );
}

if ( Geo !== undefined && notices ) {
  var now = new Date(),
      id, notice, minlat, maxlat, minlon, maxlon,
      startNotice, endNotice;

  for (id in notices) {
    if (!document.cookie.match('hidegeonotice'+id+'=1')) {

      notice = notices[id];
      notice.id = id;

      if (!notice || !notice.begin || !notice.end) {
        continue;
      }

      startNotice = Date.parse(notice.begin);
      endNotice = Date.parse(notice.end);

      if ( now.getTime() > startNotice &&
        now.getTime() < endNotice ) {
          if (notice.country && Geo.country === notice.country) {
            displayGeonotice(notice);
          } else {
            if (notice.corners) {
              minlat = Math.min(notice.corners[0][0], notice.corners[1][0]);
              maxlat = Math.max(notice.corners[0][0], notice.corners[1][0]);
              minlon = Math.min(notice.corners[0][1], notice.corners[1][1]);
              maxlon = Math.max(notice.corners[0][1], notice.corners[1][1]);

              // Geo coordinates can be empty string if unknown. parseFloat makes
              // these NaN, so that you do not get to see a notice in that case.
              if ( minlat < parseFloat( Geo.lat ) && parseFloat( Geo.lat ) < maxlat &&
                  minlon < parseFloat( Geo.lon ) && parseFloat( Geo.lon ) < maxlon ) {
                displayGeonotice(notice);
              }
            }
          }
      }
    }
  }
}
})();