summaryrefslogtreecommitdiff
path: root/toolkit/content/widgets/wizard.xml
blob: 3a8ec2cfeffcfddc3138e80f0aa49b01c705353e (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
<?xml version="1.0"?>
<!-- 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/. -->


<!DOCTYPE bindings [
  <!ENTITY % wizardDTD SYSTEM "chrome://global/locale/wizard.dtd">
  %wizardDTD;
]>

<bindings id="wizardBindings"
   xmlns="http://www.mozilla.org/xbl"
   xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
   xmlns:xbl="http://www.mozilla.org/xbl">

  <binding id="wizard-base">
    <resources>
      <stylesheet src="chrome://global/skin/wizard.css"/>
    </resources>
  </binding>

  <binding id="wizard" extends="chrome://global/content/bindings/general.xml#root-element">
    <resources>
      <stylesheet src="chrome://global/skin/wizard.css"/>
    </resources>
    <content>
      <xul:hbox class="wizard-header" anonid="Header"/>

      <xul:deck class="wizard-page-box" flex="1" anonid="Deck">
        <children includes="wizardpage"/>
      </xul:deck>
      <children/>

      <xul:hbox class="wizard-buttons" anonid="Buttons" xbl:inherits="pagestep,firstpage,lastpage"/>
    </content>

    <implementation>
      <property name="title" onget="return document.title;"
                             onset="return document.title = val;"/>

      <property name="canAdvance" onget="return this._canAdvance;"
                                  onset="this._nextButton.disabled = !val; return this._canAdvance = val;"/>
      <property name="canRewind" onget="return this._canRewind;"
                                 onset="this._backButton.disabled = !val; return this._canRewind = val;"/>

      <property name="pageStep" readonly="true" onget="return this._pageStack.length"/>

      <field name="pageCount">0</field>

      <field name="_accessMethod">null</field>
      <field name="_pageStack">null</field>
      <field name="_currentPage">null</field>

      <property name="wizardPages">
        <getter>
        <![CDATA[
          var xulns = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
          return this.getElementsByTagNameNS(xulns, "wizardpage");
        ]]>
        </getter>
      </property>

      <property name="currentPage" onget="return this._currentPage">
        <setter>
        <![CDATA[
          if (!val)
            return val;

          this._currentPage = val;

          // Setting this attribute allows wizard's clients to dynamically
          // change the styles of each page based on purpose of the page.
          this.setAttribute("currentpageid", val.pageid);
          if (this.onFirstPage) {
            this.canRewind = false;
            this.setAttribute("firstpage", "true");
            if (/Linux/.test(navigator.platform)) {
              this._backButton.setAttribute('hidden', 'true');
            }
          } else {
            this.canRewind = true;
            this.setAttribute("firstpage", "false");
            if (/Linux/.test(navigator.platform)) {
              this._backButton.setAttribute('hidden', 'false');
            }
          }

          if (this.onLastPage) {
            this.canAdvance = true;
            this.setAttribute("lastpage", "true");
          } else {
            this.setAttribute("lastpage", "false");
          }

          this._deck.setAttribute("selectedIndex", val.pageIndex);
          this._advanceFocusToPage(val);

          this._adjustWizardHeader();
          this._wizardButtons.onPageChange();

          this._fireEvent(val, "pageshow");

          return val;
        ]]>
        </setter>
      </property>

      <property name="pageIndex"
                onget="return this._currentPage ? this._currentPage.pageIndex : -1;">
        <setter>
        <![CDATA[
          if (val < 0 || val >= this.pageCount)
            return val;

          var page = this.wizardPages[val];
          this._pageStack[this._pageStack.length-1] = page;
          this.currentPage = page;

          return val;
        ]]>
        </setter>
      </property>

      <property name="onFirstPage" readonly="true"
                onget="return this._pageStack.length == 1;"/>

      <property name="onLastPage" readonly="true">
        <getter><![CDATA[
          var cp = this.currentPage;
          return cp && ((this._accessMethod == "sequential" && cp.pageIndex == this.pageCount-1) ||
                       (this._accessMethod == "random" && cp.next == ""));
         ]]></getter>
      </property>

      <method name="getButton">
        <parameter name="aDlgType"/>
        <body>
        <![CDATA[
          var btns = this.getElementsByAttribute("dlgtype", aDlgType);
          return btns.item(0) ? btns[0] : document.getAnonymousElementByAttribute(this._wizardButtons, "dlgtype", aDlgType);
        ]]>
        </body>
      </method>

      <field name="_canAdvance"/>
      <field name="_canRewind"/>
      <field name="_wizardHeader"/>
      <field name="_wizardButtons"/>
      <field name="_deck"/>
      <field name="_backButton"/>
      <field name="_nextButton"/>
      <field name="_cancelButton"/>

      <!-- functions to be added as oncommand listeners to the wizard buttons -->
      <field name="_backFunc">(function() { document.documentElement.rewind(); })</field>
      <field name="_nextFunc">(function() { document.documentElement.advance(); })</field>
      <field name="_finishFunc">(function() { document.documentElement.advance(); })</field>
      <field name="_cancelFunc">(function() { document.documentElement.cancel(); })</field>
      <field name="_extra1Func">(function() { document.documentElement.extra1(); })</field>
      <field name="_extra2Func">(function() { document.documentElement.extra2(); })</field>

      <field name="_closeHandler">(function(event) {
        if (document.documentElement.cancel())
          event.preventDefault();
      })</field>

      <constructor><![CDATA[
        this._canAdvance = true;
        this._canRewind = false;
        this._hasLoaded = false;

        this._pageStack = [];

        try {
          // need to create string bundle manually instead of using <xul:stringbundle/>
          // see bug 63370 for details
          this._bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
                                   .getService(Components.interfaces.nsIStringBundleService)
                                   .createBundle("chrome://global/locale/wizard.properties");
        } catch (e) {
          // This fails in remote XUL, which has to provide titles for all pages
          // see bug 142502
        }

        // get anonymous content references
        this._wizardHeader = document.getAnonymousElementByAttribute(this, "anonid", "Header");
        this._wizardButtons = document.getAnonymousElementByAttribute(this, "anonid", "Buttons");
        this._deck = document.getAnonymousElementByAttribute(this, "anonid", "Deck");

        this._initWizardButton("back");
        this._initWizardButton("next");
        this._initWizardButton("finish");
        this._initWizardButton("cancel");
        this._initWizardButton("extra1");
        this._initWizardButton("extra2");

        this._initPages();

        window.addEventListener("close", this._closeHandler, false);

        // start off on the first page
        this.pageCount = this.wizardPages.length;
        this.advance();

        // give focus to the first focusable element in the dialog
        window.addEventListener("load", this._setInitialFocus, false);
      ]]></constructor>

      <method name="getPageById">
        <parameter name="aPageId"/>
        <body><![CDATA[
          var els = this.getElementsByAttribute("pageid", aPageId);
          return els.item(0);
        ]]></body>
      </method>

      <method name="extra1">
        <body><![CDATA[
          if (this.currentPage)
            this._fireEvent(this.currentPage, "extra1");
        ]]></body>
      </method>

      <method name="extra2">
        <body><![CDATA[
          if (this.currentPage)
            this._fireEvent(this.currentPage, "extra2");
        ]]></body>
      </method>

      <method name="rewind">
        <body><![CDATA[
          if (!this.canRewind)
            return;

          if (this.currentPage && !this._fireEvent(this.currentPage, "pagehide"))
            return;

          if (this.currentPage && !this._fireEvent(this.currentPage, "pagerewound"))
            return;

          if (!this._fireEvent(this, "wizardback"))
            return;


          this._pageStack.pop();
          this.currentPage = this._pageStack[this._pageStack.length-1];
          this.setAttribute("pagestep", this._pageStack.length);
        ]]></body>
      </method>

      <method name="advance">
        <parameter name="aPageId"/>
        <body><![CDATA[
          if (!this.canAdvance)
            return;

          if (this.currentPage && !this._fireEvent(this.currentPage, "pagehide"))
            return;

          if (this.currentPage && !this._fireEvent(this.currentPage, "pageadvanced"))
            return;

          if (this.onLastPage && !aPageId) {
            if (this._fireEvent(this, "wizardfinish"))
              window.setTimeout(function() {window.close();}, 1);
          } else {
            if (!this._fireEvent(this, "wizardnext"))
              return;

            var page;
            if (aPageId)
              page = this.getPageById(aPageId);
            else {
              if (this.currentPage) {
                if (this._accessMethod == "random")
                  page = this.getPageById(this.currentPage.next);
                else
                  page = this.wizardPages[this.currentPage.pageIndex+1];
              } else
                page = this.wizardPages[0];
            }

            if (page) {
              this._pageStack.push(page);
              this.setAttribute("pagestep", this._pageStack.length);

              this.currentPage = page;
            }
          }
        ]]></body>
      </method>

      <method name="goTo">
        <parameter name="aPageId"/>
        <body><![CDATA[
          var page = this.getPageById(aPageId);
          if (page) {
            this._pageStack[this._pageStack.length-1] = page;
            this.currentPage = page;
          }
        ]]></body>
      </method>

      <method name="cancel">
        <body><![CDATA[
          if (!this._fireEvent(this, "wizardcancel"))
            return true;

          window.close();
          window.setTimeout(function() {window.close();}, 1);
          return false;
        ]]></body>
      </method>

      <method name="_setInitialFocus">
        <parameter name="aEvent"/>
        <body>
        <![CDATA[
          document.documentElement._hasLoaded = true;
          var focusInit =
            function() {
              // give focus to the first focusable element in the dialog
              if (!document.commandDispatcher.focusedElement)
                document.commandDispatcher.advanceFocusIntoSubtree(document.documentElement);

              try {
                var button =
                      document.documentElement._wizardButtons.defaultButton;
                if (button)
                  window.notifyDefaultButtonLoaded(button);
              } catch (e) { }
            };

          // Give focus after onload completes, see bug 103197.
          setTimeout(focusInit, 0);
        ]]>
        </body>
      </method>

      <method name="_advanceFocusToPage">
        <parameter name="aPage"/>
        <body>
        <![CDATA[
          if (!this._hasLoaded)
            return;

          document.commandDispatcher.advanceFocusIntoSubtree(aPage);

          // if advanceFocusIntoSubtree tries to focus one of our
          // dialog buttons, then remove it and put it on the root
          var focused = document.commandDispatcher.focusedElement;
          if (focused && focused.hasAttribute("dlgtype"))
            this.focus();
        ]]>
        </body>
      </method>

      <method name="_initPages">
        <body><![CDATA[
          var meth = "sequential";
          var pages = this.wizardPages;
          for (var i = 0; i < pages.length; ++i) {
            var page = pages[i];
            page.pageIndex = i;
            if (page.next != "")
              meth = "random";
          }
          this._accessMethod = meth;
        ]]></body>
      </method>

      <method name="_initWizardButton">
        <parameter name="aName"/>
        <body><![CDATA[
         var btn = document.getAnonymousElementByAttribute(this._wizardButtons, "dlgtype", aName);
         if (btn) {
           btn.addEventListener("command", this["_"+aName+"Func"], false);
           this["_"+aName+"Button"] = btn;
         }
         return btn;
        ]]></body>
      </method>

      <method name="_adjustWizardHeader">
        <body><![CDATA[
          var label = this.currentPage.getAttribute("label");
          if (!label && this.onFirstPage && this._bundle) {
            if (/Mac/.test(navigator.platform)) {
              label = this._bundle.GetStringFromName("default-first-title-mac");
            } else {
              label = this._bundle.formatStringFromName("default-first-title", [this.title], 1);
            }
          } else if (!label && this.onLastPage && this._bundle) {
            if (/Mac/.test(navigator.platform)) {
              label = this._bundle.GetStringFromName("default-last-title-mac");
            } else {
              label = this._bundle.formatStringFromName("default-last-title", [this.title], 1);
            }
          }
          this._wizardHeader.setAttribute("label", label);
          this._wizardHeader.setAttribute("description", this.currentPage.getAttribute("description"));
        ]]></body>
      </method>

      <method name="_hitEnter">
        <parameter name="evt"/>
        <body>
        <![CDATA[
          if (!evt.defaultPrevented)
            this.advance();
        ]]>
        </body>
      </method>

      <method name="_fireEvent">
        <parameter name="aTarget"/>
        <parameter name="aType"/>
        <body>
        <![CDATA[
          var event = document.createEvent("Events");
          event.initEvent(aType, true, true);

          // handle dom event handlers
          var noCancel = aTarget.dispatchEvent(event);

          // handle any xml attribute event handlers
          var handler = aTarget.getAttribute("on"+aType);
          if (handler != "") {
            var fn = new Function("event", handler);
            var returned = fn.apply(aTarget, [event]);
            if (returned == false)
              noCancel = false;
          }

          return noCancel;
        ]]>
        </body>
      </method>

    </implementation>

    <handlers>
      <handler event="keypress" keycode="VK_RETURN"
               group="system" action="this._hitEnter(event)"/>
      <handler event="keypress" keycode="VK_ESCAPE" group="system">
        if (!event.defaultPrevented)
          this.cancel();
      </handler>
    </handlers>
  </binding>

  <binding id="wizardpage" extends="chrome://global/content/bindings/wizard.xml#wizard-base">
    <implementation>
      <field name="pageIndex">-1</field>

      <property name="pageid" onget="return this.getAttribute('pageid');"
                              onset="this.setAttribute('pageid', val);"/>

      <property name="next"   onget="return this.getAttribute('next');"
                              onset="this.setAttribute('next', val);
                                     this.parentNode._accessMethod = 'random';
                                     return val;"/>
    </implementation>
  </binding>

#ifdef XP_MACOSX
  <binding id="wizard-header" extends="chrome://global/content/bindings/wizard.xml#wizard-base">
    <content>
      <xul:stack class="wizard-header-stack" flex="1">
        <xul:vbox class="wizard-header-box-1">
          <xul:vbox class="wizard-header-box-text">
            <xul:label class="wizard-header-label" xbl:inherits="xbl:text=label"/>
          </xul:vbox>
        </xul:vbox>
        <xul:hbox class="wizard-header-box-icon">
          <xul:spacer flex="1"/>
          <xul:image class="wizard-header-icon" xbl:inherits="src=iconsrc"/>
        </xul:hbox>
      </xul:stack>
    </content>
  </binding>

  <binding id="wizard-buttons" extends="chrome://global/content/bindings/wizard.xml#wizard-base">
    <content>
      <xul:vbox flex="1">
        <xul:hbox class="wizard-buttons-btm">
          <xul:button class="wizard-button" dlgtype="extra1" hidden="true"/>
          <xul:button class="wizard-button" dlgtype="extra2" hidden="true"/>
          <xul:button label="&button-cancel-mac.label;" class="wizard-button" dlgtype="cancel"/>
          <xul:spacer flex="1"/>
          <xul:button label="&button-back-mac.label;" accesskey="&button-back-mac.accesskey;"
                      class="wizard-button wizard-nav-button" dlgtype="back"/>
          <xul:button label="&button-next-mac.label;" accesskey="&button-next-mac.accesskey;"
                      class="wizard-button wizard-nav-button" dlgtype="next"
                      default="true" xbl:inherits="hidden=lastpage" />
          <xul:button label="&button-finish-mac.label;" class="wizard-button"
                      dlgtype="finish" default="true" xbl:inherits="hidden=hidefinishbutton" />
        </xul:hbox>
      </xul:vbox>
    </content>

    <implementation>
      <method name="onPageChange">
        <body><![CDATA[
          this.setAttribute("hidefinishbutton", !(this.getAttribute("lastpage") == "true"));
        ]]></body>
      </method>
    </implementation>

  </binding>

#else

  <binding id="wizard-header" extends="chrome://global/content/bindings/wizard.xml#wizard-base">
    <content>
      <xul:hbox class="wizard-header-box-1" flex="1">
        <xul:vbox class="wizard-header-box-text" flex="1">
          <xul:label class="wizard-header-label" xbl:inherits="xbl:text=label"/>
          <xul:label class="wizard-header-description" xbl:inherits="xbl:text=description"/>
        </xul:vbox>
        <xul:image class="wizard-header-icon" xbl:inherits="src=iconsrc"/>
      </xul:hbox>
    </content>
  </binding>

  <binding id="wizard-buttons" extends="chrome://global/content/bindings/wizard.xml#wizard-base">
    <content>
      <xul:vbox class="wizard-buttons-box-1" flex="1">
        <xul:separator class="wizard-buttons-separator groove"/>
        <xul:hbox class="wizard-buttons-box-2">
          <xul:button class="wizard-button" dlgtype="extra1" hidden="true"/>
          <xul:button class="wizard-button" dlgtype="extra2" hidden="true"/>
          <xul:spacer flex="1" anonid="spacer"/>
#ifdef XP_UNIX
          <xul:button label="&button-cancel-unix.label;" class="wizard-button"
                      dlgtype="cancel" icon="cancel"/>
          <xul:spacer style="width: 24px"/>
          <xul:button label="&button-back-unix.label;" accesskey="&button-back-unix.accesskey;"
                      class="wizard-button" dlgtype="back" icon="go-back"/>
          <xul:deck class="wizard-next-deck" anonid="WizardButtonDeck">
            <xul:hbox>
              <xul:button label="&button-finish-unix.label;" class="wizard-button"
                          dlgtype="finish" default="true" flex="1"/>
            </xul:hbox>
            <xul:hbox>
              <xul:button label="&button-next-unix.label;" accesskey="&button-next-unix.accesskey;"
                          class="wizard-button" dlgtype="next" icon="go-forward"
                          default="true" flex="1"/>
            </xul:hbox>
          </xul:deck>
#else
          <xul:button label="&button-back-win.label;" accesskey="&button-back-win.accesskey;"
                      class="wizard-button" dlgtype="back" icon="go-back"/>
          <xul:deck class="wizard-next-deck" anonid="WizardButtonDeck">
            <xul:hbox>
              <xul:button label="&button-finish-win.label;" class="wizard-button"
                          dlgtype="finish" default="true" flex="1"/>
            </xul:hbox>
            <xul:hbox>
              <xul:button label="&button-next-win.label;" accesskey="&button-next-win.accesskey;"
                          class="wizard-button" dlgtype="next" icon="go-forward"
                          default="true" flex="1"/>
            </xul:hbox>
          </xul:deck>
          <xul:button label="&button-cancel-win.label;" class="wizard-button"
                      dlgtype="cancel" icon="cancel"/>
#endif
        </xul:hbox>
      </xul:vbox>
    </content>

    <implementation>
      <field name="_wizardButtonDeck" readonly="true">
        document.getAnonymousElementByAttribute(this, "anonid", "WizardButtonDeck");
      </field>

      <method name="onPageChange">
        <body><![CDATA[
          if (this.getAttribute("lastpage") == "true") {
            this._wizardButtonDeck.setAttribute("selectedIndex", 0);
          } else {
            this._wizardButtonDeck.setAttribute("selectedIndex", 1);
          }
        ]]></body>
      </method>

      <property name="defaultButton" readonly="true">
        <getter><![CDATA[
          const kXULNS =
            "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
          var buttons = this._wizardButtonDeck.selectedPanel
                            .getElementsByTagNameNS(kXULNS, "button");
          for (var i = 0; i < buttons.length; i++) {
            if (buttons[i].getAttribute("default") == "true" &&
                !buttons[i].hidden && !buttons[i].disabled)
              return buttons[i];
          }
          return null;
        ]]></getter>
      </property>
    </implementation>
  </binding>
#endif

</bindings>