MediaWiki:Wikisource.data.js

From Wikisource
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
if(typeof ws == 'undefined') {
    ws = {};
}

/**
 * Api in order to get Wikisource  metadata
 * @author Tpt
 * @see https://wikisource.org/wiki/Wikisource:Microformat
 */
ws.data = {

  //Store metadata for internal use. Don't use it !
  data: {
    type: '',
    title: '',
    periodical: '',
    author: '',
    translator: '',
    school: '',
    publisher: '',
    year: '',
    place: '',
    key: '',
    progress: '',
    volume: '',
    chapter: '',
    pages: ''
  },

  /**
   * Return value for a metadata
   * @param id string the metadata id without 'ws-' prefix
   * @return string
   */
  get: function(id) {
    if(this.data[id] == '') {
      var $node = $('#ws-' + id);
      if($node.length > 0) {
        this.data[id] = $node.text();
      } else {
        $node = $('.ws-' + id);
        if($node.length > 0) {
          this.data[id] = $node.text();
        }
      }
    }
    return this.data[id];
  },

  /**
   * Return an associative array of all metadata
   * @return array
   */
  getAll: function() {
    for(var id in this.data) {
      this.data[id] = this.get(id);
    }
    return this.data;
  }
};