Jump to content

User:MidPhoto/location from heritage.js

From Meta, a Wikimedia project coordination wiki

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
jQuery(function ($) {
  var editor = $('#wpTextbox1');
  if (editor && editor.wikiEditor) {
    var HeritageLocation = {};

    HeritageLocation.descriptionText =
      "add [[Template:Object Location|{{Object Location}}]]" +
      " from the hertage api using the " +
      "[[m:User:MidPhoto/location_from_heritage.js|" +
      "location from heritage]] script";

    HeritageLocation.getObjectLocationTemplate = function (a, b) {
      var string =
        '{{Object Location|{0}|{1}|type:landmark_region:NL}}';
      string = string.replace('{0}', a);
      string = string.replace('{1}', b);
      return string;
    };

    HeritageLocation.callback = function (context) {
      var regex = /\{\{Gemeentelijk monument\|([^\}]+)\}\}/i;
      var gem_id = regex.exec(editor.text())[1];
      $.ajax({
        url: 'https://tools.wmflabs.org/heritage/api/api.php',
        dataType: 'json',
        data: {
          action: 'search',
          format: 'json',
          srcountry: 'nl-gem',
          srid: gem_id
        }
      }).success(function (data) {
        if (data.monuments.length == 1) {
          var description = $('input.mw-summary');
          var monument = data.monuments[0];
          var template = HeritageLocation.getObjectLocationTemplate(
            monument.lat, monument.lon);
          editor.append(template);
          description.val(description.val() +
            HeritageLocation.descriptionText);
        }
      });
    };
    editor.wikiEditor('addToToolbar', {
      section: 'advanced',
      group: 'insert',
      tools: {
        buttonId: {
          label: 'Add location from heritage api',
          type: 'button',
          icon: '//upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Gnome-globe.svg/26px-Gnome-globe.svg.png',
          action: {
            type: 'callback',
            execute: HeritageLocation.callback
          }
        }
      }
    });
  } else {
		console.log('loading failed :(');
  	
  }
});