Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: vulic on November 03, 2011, 07:24:53 AM

Title: URL popup for smf 2
Post by: vulic on November 03, 2011, 07:24:53 AM
This is small code modification to show popup windows for http url and ftp url and then for the name of the url.

The modification is inspired by URL popup (http://custom.simplemachines.org/mods/index.php?mod=451) for smf 1.


My first message was little messy so I change the post.

The smf 2.0.1 default behavior.

1. When text selected and click on hyperlink button show popup window to enter the URL.The result look like this SMF forum (http://www.simplemachines.org/community/index.php).
2. When no text selected and click on hyperlink button show popup window to enter the URL. The result look like this http://www.simplemachines.org/community/index.php (http://www.simplemachines.org/community/index.php).

Behavior with the code modification.

1. When text selected and click on hyperlink button show popup window to enter the URL.The result look like this SMF forum (http://www.simplemachines.org/community/index.php).
2a. When no text selected and click on hyperlink button show popup window to enter the URL. Then show another popup window to enter the name for URL. If some text is entered the result look like this SMF forum (http://www.simplemachines.org/community/index.php).
2b. When no text selected and click on hyperlink button show popup window to enter the URL. Then show another popup window to enter the name for URL. If no text is entered the result look like this http://www.simplemachines.org/community/index.php (http://www.simplemachines.org/community/index.php).

The same, changed behavior is for the FTP link.

If you ask why change the default behavior. Answer is question. How many user no that he/she should select some text for pretty naming URL?
And in the end. Modification of the code does not destroy the default behavior. It adds the new option.


I change the code in file /Themes/default/scripts/editor.js line 807-842.

// Insert a URL link.
smc_Editor.prototype.insertLink = function(sType)
{
    if (sType == 'email')
        var sPromptText = oEditorStrings['prompt_text_email'];
    else if (sType == 'ftp')
        var sPromptText = oEditorStrings['prompt_text_ftp'];
    else
        var sPromptText = oEditorStrings['prompt_text_url'];

    // IE has a nice prompt for this - others don't.
    if (sType != 'email' && sType != 'ftp' && is_ie)
        this.smf_execCommand('createlink', true, sText);

    else
    {
        // Ask them where to link to.
        var sText = prompt(sPromptText, sType == 'email' ? '' : (sType == 'ftp' ? 'ftp://' : 'http://'));
        if (!sText)
            return;

        if (sType == 'email' && sText.indexOf('mailto:') != 0)
            {
            sText = 'mailto:' + sText;
            this.insertText('<a href="' + sText + '">' + sText + '</a>' + '&nbsp;');
            }
        else
        {
        // Check if we have text selected and if not force us to have some.
            var oCurText = this.getSelect(true, true);
            if (oCurText.toString().length != 0)
            {
                this.smf_execCommand('unlink');
                this.smf_execCommand('createlink', false, sText);
            }
            else // If no text selected show popup window to enter text.
            {
                var urlNAME = prompt('Enter URL name.Leave blank for straight URL.','');
                if (urlNAME == null )  //Cancel pressed.
                {
                }
                 else if (urlNAME == '' || urlNAME == ' ')  // OK pressed but with no text so use URL only.
                {
                    this.insertText('<a href="' + sText + '">' + sText + '</a>' + '&nbsp;');
                }
                else // OK pressed with text so use name for the URL.
                {
                    this.insertText('<a href="' + sText + '">' + urlNAME + '</a>' + '&nbsp;');
                }
            }
        }
    }
}
Title: Re: URL popup for smf 2
Post by: vulic on November 04, 2011, 03:24:17 PM
OK, I screw up text selecting for mail in previous code.
And second issue is IE. My code modification does not change default behavior for Internet Explorer.
My operating system is Debian stable and I have no Windows install. So I test Internet Explorer 7 under Wine in Debian.

This is fixed version of code. I fixed selecting text for mail and IE issue.

// Insert a URL link.
smc_Editor.prototype.insertLink = function(sType)
{
if (sType == 'email')
var sPromptText = oEditorStrings['prompt_text_email'];
else if (sType == 'ftp')
var sPromptText = oEditorStrings['prompt_text_ftp'];
else
var sPromptText = oEditorStrings['prompt_text_url'];
// Ask them where to link to.
var sText = prompt(sPromptText, sType == 'email' ? '' : (sType == 'ftp' ? 'ftp://' : 'http://'));
if (!sText)
return;
if (sType == 'email' && sText.indexOf('mailto:') != 0)
{
sText = 'mailto:' + sText;
}
// Check if we have text selected and if not ask for some text in second popup window.
var oCurText = this.getSelect(true, true);
if (oCurText.toString().length != 0)
{
this.smf_execCommand('unlink');
this.smf_execCommand('createlink', false, sText);
}
else if (sType == 'email')
{
this.insertText('<a href="' + sText + '">' + sText + '</a>' + '&nbsp');
}
else
{
var urlNAME = prompt('Enter URL name.Leave blank for straight URL.','');
if (urlNAME == null )  // Cancel pressed.
{
}
else if (urlNAME == '' || urlNAME == ' ')  // OK pressed but with no text so use URL only.
{
this.insertText('<a href="' + sText + '">' + sText + '</a>' + '&nbsp;');
}
else // OK pressed with text so use name for the URL.
{
this.insertText('<a href="' + sText + '">' + urlNAME + '</a>' + '&nbsp;');
}
}
}
Title: Re: URL popup for smf 2
Post by: ApplianceJunk on November 04, 2011, 04:47:23 PM
QuoteThe modification is inspired by URL popup for smf 1.

Did you try to install that mod on SMF 2?
I think it may still work with 2.0
Title: Re: URL popup for smf 2
Post by: vulic on November 04, 2011, 05:27:52 PM
Yes I did. And it did't work. Then I try installing by hand but with no success. Code has changed a lot since version 1.
Title: Re: URL popup for smf 2
Post by: Manu.G on November 11, 2011, 06:15:14 PM
I was searching for this little popup, because my forum members screaming for it.
My problem is at the moment, what exactly did you replace with your code?

Is it possible to post a little instruction for it? For stupid cows like me :)
Title: Re: URL popup for smf 2
Post by: vulic on November 12, 2011, 03:34:20 PM
 I explained everything in my first message. I don't know how to be more specific. English is not my native language.
Default behavior is intact. I just added second pop up window to enter some text for URL address if no text is selected before. See again my first post section 2a and 2b.

edit: The correct code with modifications is in my second message.

edit 2: The modifications is for WYSIWYG editor. To change to WYSIWYG editor click on selected button.

(https://www.simplemachines.org/community/proxy.php?request=http%3A%2F%2Fwww.dodaj.rs%2Ff%2F2W%2Fpw%2FiHCWdNp%2Fsmf2.png&hash=1019ce36e3c226178f74b7b3fca52e74afbe3e31)