addon-sdk/source/examples/reading-data/lib/main.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/examples/reading-data/lib/main.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,53 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +var self = require("sdk/self");
    1.10 +var { Panel } = require("sdk/panel");
    1.11 +var { ToggleButton } = require("sdk/ui");
    1.12 +
    1.13 +function replaceMom(html) {
    1.14 +  return html.replace("World", "Mom");
    1.15 +}
    1.16 +exports.replaceMom = replaceMom;
    1.17 +
    1.18 +exports.main = function(options, callbacks) {
    1.19 +  console.log("My ID is " + self.id);
    1.20 +
    1.21 +  // Load the sample HTML into a string.
    1.22 +  var helloHTML = self.data.load("sample.html");
    1.23 +
    1.24 +  // Let's now modify it...
    1.25 +  helloHTML = replaceMom(helloHTML);
    1.26 +
    1.27 +  // ... and then create a panel that displays it.
    1.28 +  var myPanel = Panel({
    1.29 +    contentURL: "data:text/html," + helloHTML,
    1.30 +    onHide: handleHide
    1.31 +  });
    1.32 +
    1.33 +  // Create a widget that displays the image.  We'll attach the panel to it.
    1.34 +  // When you click the widget, the panel will pop up.
    1.35 +  var button = ToggleButton({
    1.36 +    id: "test-widget",
    1.37 +    label: "Mom",
    1.38 +    icon: './mom.png',
    1.39 +    onChange: handleChange
    1.40 +  });
    1.41 +
    1.42 +  // If you run cfx with --static-args='{"quitWhenDone":true}' this program
    1.43 +  // will automatically quit Firefox when it's done.
    1.44 +  if (options.staticArgs.quitWhenDone)
    1.45 +    callbacks.quit();
    1.46 +}
    1.47 +
    1.48 +function handleChange(state) {
    1.49 +  if (state.checked) {
    1.50 +    myPanel.show({ position: button });
    1.51 +  }
    1.52 +}
    1.53 +
    1.54 +function handleHide() {
    1.55 +  button.state('window', { checked: false });
    1.56 +}

mercurial