User:Waihorace/wikitips.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//[[en:User:Kaldari/wikilove.js]]變化版<nowiki>
if (( wgCanonicalNamespace == 'User_talk' && wgAction == 'view' && skin == 'vector' )) {
  // Script depends on jQuery dialog widget
  mw.loader.using( 'jquery.ui', function() {
    // Construct object (to prevent namespace conflicts)
    wikiTips = {
 
      displayProgress: function( form, message ) {
        $('#'+form+' div').hide(); // remove everything else from the dialog box
        $('#'+form).append ( $('<div style="text-align:center;margin:3em 0;"></div>').html( message+'<br/><img src="http://upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif" />' ) );
      },
 
      displayError: function( form, error ) {
        $('#'+form+' div').hide(); // remove everything else from the dialog box
        $('#'+form).append ( $('<div style="color:#990000;margin-top:0.4em;"></div>').html( '錯誤: '+error ) );
      },
 
      getEditToken: function() {
        $.ajax({
          url: wgScriptPath + '/api.php?',
          data: 'action=query&prop=info&intoken=edit&titles=Main%20Page&indexpageids&format=json',
          dataType: 'json',
          success: function( data ) {
            if ( data.query.pages && data.query.pageids ) {
              var pageid = data.query.pageids[0];
              wikiTips.editToken = data.query.pages[pageid].edittoken;
            }
          }
        });
      },
 
      getEmailable: function() {
        // Test to see if the 'E-mail this user' link exists
        wikiTips.emailable = $('#t-emailuser').length ? true : false;
      },
 
      editPage: function( form, summary, template ) {
        $.ajax({
          url: wgScriptPath + '/api.php?',
          data: 'action=edit&title='+encodeURIComponent(mw.config.get('wgPageName'))+'&section=new&summary='+encodeURIComponent(summary)+'&text='+encodeURIComponent(template)+'&format=json&token='+encodeURIComponent(wikiTips.editToken),
          dataType: 'json',
          type: 'POST',
          success: function( data ) {
            if ( data.edit.result == "Success" ) {
              window.location.reload();
            } else {
              wikiTips.displayError( form, 'API結果不明。請嘗試登出維基百科,再次登入維基百科並返回本頁再次加入維基提示。' );
            }
          },
          error: function( xhr ) {
            wikiTips.displayError( form, '編輯錯誤。請嘗試重新整理本頁,並再次加入維基提示。' );
            //console.debug( xhr.responseText );
          }
        });
      },
 
      sendEmail: function( form, subject, text ) {
        $.ajax({
          url: wgScriptPath + '/api.php?',
          data: 'action=emailuser&target='+encodeURIComponent(wgTitle)+'&subject='+encodeURIComponent(subject)+'&text='+encodeURIComponent(text)+'&format=json&token='+encodeURIComponent(wikiTips.editToken),
          dataType: 'json',
          type: 'POST'
        });
      },
 
      launchDialog: function( interface, emailOption ) {
        // If user is emailable, add email checkbox to forms that request it
        if ( emailOption && typeof wikiTips.emailable !== 'undefined' && wikiTips.emailable ) {
          if ( typeof interface.data('emailCheckboxAppended') == 'undefined' ) {
            interface.append( $('<div style="margin-top:0.4em;"></div>').html('<input type="checkbox" name="notify"/> 用電郵通知這個用戶(注意:一般新用戶在編輯用戶討論頁時系統會有自動電郵)') );
            interface.data('emailCheckboxAppended', true);
          }
        }
        // Open the dialog box
        interface.dialog('open');
        // Retrieve the edit token so it will be available when the form is submitted
        wikiTips.getEditToken();
      },
 
      initialize: function() {
 
        // Function to make a list of images selectable
        $.fn.selectItem = function() {
          return this.each(function() {
            var containerId = this.id;
            var customId = 0;
            $('#'+containerId+' img').css('border', '8px solid transparent');
            $('#'+containerId+' img').each(function() {
              if (this.id == '') {
                this.id = 'select_img_' + customId;
                customId++;
              }
              $('#'+this.id).click(function() {
                  $('#'+containerId+'Selected').val(this.title);
                  $('#'+containerId+' img').css('border', '8px solid transparent');
                  $('#'+this.id).css('border', '8px solid #AED0EA');
              })
            });
          });
        };
 
        // List of all available edittipss
        var $edittipsList = [
          '提示:小小作品',
          '提示:維基化',
          '提示:關注度',
          '提示:廣告',
          '提示:版權',
          '新人建立用戶頁',
          '請建立用戶頁',
          '刪除鼓勵',
          '心得分享',
          '自訂信息'
        ];
 
        // Define edittips interface
        $wlEdittipss = $('<div id="edittipsForm" style="position:relative;"></div>')
          .append( $('<div style="margin-top:0.4em;"></div>').html( '選擇提示: ' ).append( $('<select id="edittipsSelect" style="padding:1px;vertical-align:baseline;"></select>') ) )
          .append( $('<div style="margin-top:0.4em;"></div>').html( '不帶簽名的留言(僅帶「提示:」開頭模板適用)、心得分享或自訂信息<br/>' ).append( $('<textarea name="message" id="edittipsMessage" style="width:99%" rows="4" cols="60"></textarea>') ) )
          .dialog({
            width: 500,
            autoOpen: false,
            title: '編輯提示',
            modal: true,
            buttons: { "發出這個提示": function() { 
              $(this).dialog({buttons:{}});
              if ( typeof wikiTips.editToken !== 'undefined' ) {
                if ( $('#edittipsForm input[name="notify"]').attr('checked') ) {
                  // Send email
                  wikiTips.sendEmail( 'edittipsForm', '維基百科編輯提示', '你好 '+wgTitle+'!\n\n有用戶給了你一個關於「'+$('#edittipsSelect').val()+'」的維基百科編輯提示,目前在你的中文維基百科用戶討論頁上待你查閱,希望你能積極參與維基百科。' );
                }
                // Perform edit to page
                wikiTips.displayProgress( 'edittipsForm', '正在向頁面加入維基提示,請稍等' );
                var template = '{{subst:User:Waihorace/tips/'+$('#edittipsSelect').val()+'|'+$('#edittipsMessage').val() +' ~~~~}}';
                wikiTips.editPage( 'edittipsForm', '', template );
              } else {
                wikiTips.displayError( 'edittipsForm', '無法完成編輯' );
              }
            }}
          });
 
        // Populate edittips select list
        $.each( $edittipsList, function(index, value) { $('#edittipsSelect').append( $('<option></option>').val(value).html(value) ); });







        // List of all available warnings
        var $warningsList = [
          'vand1',
          'vand2',
          'vand3',
          'vand4',
          'vand4im',
          'advert1',
          'advert2',
          'advert3',
          'advert4',
          'delete1',
          'delete2',
          'delete3',
          'delete4',
          'delete4im',
          'redirect1',
          'redirect2',
          'redirect3',
          'redirect4',
          'redirect4im',
          'tdel1',
          'tdel2',
          'tdel3',
          'tdel4'
        ];
 
        // Define warnings interface
        $wlwarnings = $('<div id="warningsForm" style="position:relative;"></div>')
          .append( $('<div style="margin-top:0.4em;"></div>').html( '選擇警告及層級: ' ).append( $('<select id="warningsSelect" style="padding:1px;vertical-align:baseline;"></select>') ) )
          .append( $('<div style="margin-top:0.4em;"></div>').html( '附言(可選)<br/>' ).append( $('<textarea name="message" id="warningsMessage" style="width:99%" rows="4" cols="60"></textarea>') ) )
          .dialog({
            width: 500,
            autoOpen: false,
            title: '警告用戶',
            modal: true,
            buttons: { "發出警告": function() { 
              $(this).dialog({buttons:{}});
              if ( typeof warnings.editToken !== 'undefined' ) {
                if ( $('#warningsForm input[name="notify"]').attr('checked') ) {
                  // Send email
                  warnings.sendEmail( 'warningsForm', '維基百科電子郵件', '你好 '+wgTitle+'!\n\n有用戶給了你一個關於「'+$('#warningsSelect').val()+'」的維基百科編輯警告,目前在你的中文維基百科用戶討論頁上待你查閱,希望你能積極參與維基百科,並注意相關的行為。' );
                }
                // Perform edit to page
                warnings.displayProgress( 'warningsForm', '正在向頁面加入編輯警告,請稍等' );
                var template = '{{subst:uw-'+$('#warningsSelect').val()+'||'+$('#warningsMessage').val() +' }}~~~~';
                warnings.editPage( 'warningsForm', '', template );
              } else {
                warnings.displayError( 'warningsForm', '無法完成編輯' );
              }
            }}
          });
 
        // Populate warnings select list
        $.each( $warningsList, function(index, value) { $('#warningsSelect').append( $('<option></option>').val(value).html(value) ); });

        // Insert new tab into page
        var menuEdittips = '<li id="wl-edittips"><span><a onclick="wikiTips.launchDialog( $wlEdittipss, true ); return false;" href="#">提示</a></span></li>';
        var menuWarings = '<li id="wl-warnings"><span><a onclick="warnings.launchDialog( $wlwarnings, true ); return false;" href="#">警告</a></span></li>';
        var wikiTipsMenu = '<div class="vectorMenu" id="p-wikitips"><h5><span>WikiTips</span><a href="#" style="padding:0 0.4em;width:auto;"><span style="display:inline-block;height:2.5em;width:16px;margin-right:14px;background-position:0 60%;background-image:url(http://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Crystal_Clear_app_ktip.png/20px-Crystal_Clear_app_ktip.png);background-repeat:no-repeat;">&nbsp;</span></a></h5><div class="menu"><ul>'+menuEdittips+'</ul></div></div>';
        $('#p-cactions').after(wikiTipsMenu);
 
      } // close initialize function
 
    } // close wikiTips object
    wikiTips.initialize();
    wikiTips.getEmailable();
  }) // close mw.loader
} // close if
//</nowiki>