summaryrefslogtreecommitdiffstats
path: root/comm/suite/chatzilla/xul/content/install-plugin/install-plugin.js
blob: 3116d9a2f3d37b78b92dc3cd2d0360a737a73a96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var client;
var plugin;

function onLoad()
{
    client = window.arguments[0];
    client.installPluginDialog = window;
    window.getMsg = client.messageManager.getMsg;
    window.MSG_ALERT = client.mainWindow.MSG_ALERT;

    hookEvent("chk-name-auto",    "command", changeAutoName);
    hookEvent("txt-source",       "input",   sourceChange);
    hookEvent("btn-browse",       "command", browseForSource);

    // Center on CZ:
    var ow = client.mainWindow;
    window.sizeToContent();
    window.moveTo(ow.screenX + Math.max((ow.outerWidth  - window.outerWidth ) / 2, 0), 
                  ow.screenY + Math.max((ow.outerHeight - window.outerHeight) / 2, 0));
}

function changeAutoName(event)
{
    var useAutoName = document.getElementById("chk-name-auto");
    var pluginName = document.getElementById("txt-name");
    if (useAutoName.checked)
    {
        pluginName.setAttribute("disabled", "true");
        sourceChange(null);
    }
    else
    {
        pluginName.removeAttribute("disabled");
    }
}

function sourceChange(event)
{
    var useAutoName = document.getElementById("chk-name-auto");
    var pluginName = document.getElementById("txt-name");
    var sourceLoc = document.getElementById("txt-source");

    if (useAutoName.checked)
    {
        var ary = sourceLoc.value.match(/([^\/]+?)(\..{0,3}){0,2}$/);
        pluginName.value = (ary ? ary[1] : sourceLoc.value);
    }
}

function browseForSource(event)
{
    var rv = pickOpen(client.mainWindow.MSG_INSTALL_PLUGIN_SELECT_SOURCE,
                      "*.js;*.zip;*.jar");

    if (("file" in rv) && rv.file)
    {
        rv.path = rv.file.path;
        rv.spec = rv.picker.fileURL.spec;
    }

    if (rv.reason == 0)
    {
        var sourceLoc = document.getElementById("txt-source");
        sourceLoc.value = rv.spec;
        sourceChange(null);
    }
}

function doOK()
{
    var pluginName = document.getElementById("txt-name");
    var pluginSource = document.getElementById("txt-source");
    if (!pluginName.value)
    {
        alert(client.mainWindow.MSG_INSTALL_PLUGIN_ERR_SPEC_NAME);
        return false;
    }

    client.dispatch("install-plugin", {name: pluginName.value,
                                       url: pluginSource.value});
    delete client.installPluginDialog;
}

function doCancel()
{
    delete client.installPluginDialog;
}

function hookEvent(id, event, handler)
{
    var item = document.getElementById(id);
    item.addEventListener(event, handler, false);
}