| Line 1: |
Line 1: |
| | + | /** |
| | + | * Add a "create new item" link in the dropdown menu for when you want to |
| | + | * add an item to a property, but the item does not exist. |
| | + | * |
| | + | * https://phabricator.wikimedia.org/T107693 |
| | + | * |
| | + | * @author [[User:Efly]] |
| | + | */ |
| | | | |
| | + | mw.notify( $('<span>Gadget <a href="https://wiki.personaldata.io/wiki/User:Podehaye/Gadget-CreateNewItem.js">CreateNewItem</a> loaded this time!</span>') ); |
| | + | |
| | + | ( function ( $, mw ) { |
| | + | 'use strict'; |
| | + | |
| | + | // To translators: |
| | + | // $1 is the original error message (e.g. "No match was found.") |
| | + | // $2 is the add new item link's text in the sidebar of the page (e.g. "Create a new item") |
| | + | switch ( mw.config.get( 'wgUserLanguage' ) ) { |
| | + | default: |
| | + | case 'en': |
| | + | mw.messages.set( { |
| | + | 'not-found-message': '$1. $2...' |
| | + | } ); |
| | + | break; |
| | + | case 'nl': |
| | + | mw.messages.set( { |
| | + | 'not-found-message': '$1. $2...' |
| | + | } ); |
| | + | break; |
| | + | case 'zh': |
| | + | case 'zh-hans': |
| | + | case 'zh-cn': |
| | + | case 'zh-sg': |
| | + | case 'zh-my': |
| | + | mw.messages.set( { |
| | + | 'not-found-message': '$1。$2...' |
| | + | } ); |
| | + | break; |
| | + | case 'zh-hant': |
| | + | case 'zh-tw': |
| | + | case 'zh-hk': |
| | + | case 'zh-mo': |
| | + | mw.messages.set( { |
| | + | 'not-found-message': '$1。$2...' |
| | + | } ); |
| | + | break; |
| | + | } |
| | + | |
| | + | function openNewItemPopup( currentField ) { |
| | + | // .submit( requestDeletion ) |
| | + | var currentLabel = currentField.val(); |
| | + | // console.log("openNewItemPopup"); |
| | + | mw.loader.using( 'oojs-ui-core' ).done( function () { |
| | + | $( function () { |
| | + | $.get( '/wiki/Special:NewItem?label=' + currentLabel, function ( data ) { |
| | + | var popupWindow = $( '<div>' ) |
| | + | .attr( 'id', 'createnewitem' ) |
| | + | .append( $( data ).find( '#bodyContent' ) ) |
| | + | .find( 'form' ).attr( 'target', '_blank' ) |
| | + | .find( ':submit' ).on( 'click', function () { |
| | + | // console.log("Close popup"); |
| | + | popup.toggle( false ); |
| | + | /* console.log(currentLabel); |
| | + | currentField.val("Creating New Entity..."); |
| | + | window.setTimeout(function () { |
| | + | currentField.val(currentLabel); |
| | + | }, 3000);*/ |
| | + | return true; |
| | + | } ) |
| | + | .closest( '#createnewitem' ); |
| | + | |
| | + | var popup = new OO.ui.PopupWidget( { |
| | + | $content: popupWindow, |
| | + | padded: true, |
| | + | width: currentField.width() + 200, |
| | + | head: true |
| | + | } ); |
| | + | popup.$element.attr( 'style', 'z-index: 9999999;' ); |
| | + | currentField.parent().append( popup.$element ); |
| | + | popup.toggle( true ); |
| | + | } ); |
| | + | } ); |
| | + | } ); |
| | + | } |
| | + | |
| | + | function init() { |
| | + | var currentField = $( 'input' ); |
| | + | var willEntitySelectorListUpdate = false; |
| | + | $( document ).on( 'input propertychange paste', '.wikibase-snakview-value-container:has(.ui-suggester-input)', function () { |
| | + | currentField = $( this ).find( '.ui-suggester-input' ).first(); |
| | + | // console.log("Entering: "+currentFieldText); |
| | + | willEntitySelectorListUpdate = true; |
| | + | } ); |
| | + | $( document ).on( 'DOMSubtreeModified', '.ui-entityselector-list', function () { |
| | + | if ( willEntitySelectorListUpdate ) { |
| | + | var firstLi = $( this ).find( 'li' ).first(); |
| | + | var isNotFound = firstLi.hasClass( 'ui-entityselector-notfound' ); |
| | + | // console.log('Current Text Field content: '+currentField.val()+". Not found: "+isNotFound); |
| | + | if ( isNotFound ) { |
| | + | willEntitySelectorListUpdate = false; |
| | + | var $innerA = firstLi.find( 'a' ).first(); |
| | + | $innerA.on( 'click', function () { |
| | + | firstLi.remove(); |
| | + | openNewItemPopup( currentField, firstLi ); |
| | + | return false; |
| | + | } ); |
| | + | $innerA.text( mw.message( 'not-found-message', $innerA.text(), $( '#n-special-newitem' ).text() ) ); |
| | + | } |
| | + | } |
| | + | } ); |
| | + | } |
| | + | |
| | + | $( function () { |
| | + | mw.hook( 'wikibase.entityPage.entityLoaded' ).add( init ); |
| | + | } ); |
| | + | |
| | + | }( jQuery, mediaWiki ) ); |