Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1307 from Gozala/bug/toolbar@787390
Browse files Browse the repository at this point in the history
Bug 787390 - Toolbar & Frame API implementations. r=@ZER0
  • Loading branch information
Gozala committed Dec 19, 2013
2 parents 0cc808f + dd9779d commit aa3666f
Show file tree
Hide file tree
Showing 42 changed files with 2,784 additions and 139 deletions.
Binary file added examples/toolbar-api/data/favicon.ico
Binary file not shown.
21 changes: 21 additions & 0 deletions examples/toolbar-api/data/index.html
@@ -0,0 +1,21 @@
<!-- 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/. -->

<html>
<body>
<button id=post>post!</button>
</body>
<script>
window.addEventListener("message", event => {
console.log("Document message", event, event.data, event.source !== window && event.source === window.parent);
});
window.addEventListener("click", event => {
if (event.target.id === "post") {
console.log("click!")
window.parent.postMessage("ping!", "*");
}
});
console.log(window.parent === window)
</script>
</html>
48 changes: 48 additions & 0 deletions examples/toolbar-api/lib/main.js
@@ -0,0 +1,48 @@
/* 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/. */
"use strict";

const { Toolbar } = require("sdk/ui/toolbar");
const { Frame } = require("sdk/ui/frame");
const { Button } = require("sdk/ui/button");

let button = new Button({
id: "button",
label: "send!",
icon: "./favicon.ico",
onClick: () => {
frame.postMessage({
hello: "content"
});
}
});

let frame = new Frame({
url: "./index.html",
onAttach: () => {
console.log("frame was attached");
},
onReady: () => {
console.log("frame document was loaded");
},
onLoad: () => {
console.log("frame load complete");
},
onMessage: (event) => {
console.log("got message from frame content", event);
if (event.data === "ping!")
event.source.postMessage("pong!", event.source.origin);
}
});
let toolbar = new Toolbar({
items: [frame],
title: "Addon Demo",
hidden: false,
onShow: () => {
console.log("toolbar was shown");
},
onHide: () => {
console.log("toolbar was hidden");
}
});
9 changes: 9 additions & 0 deletions examples/toolbar-api/package.json
@@ -0,0 +1,9 @@
{
"name": "toolbar-api",
"title": "toolbar-api",
"id": "toolbar-api",
"description": "a toolbar api example",
"author": "",
"license": "MPL 2.0",
"version": "0.1"
}
5 changes: 5 additions & 0 deletions lib/diffpatcher/.travis.yml
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.4
- 0.5
- 0.6
14 changes: 14 additions & 0 deletions lib/diffpatcher/History.md
@@ -0,0 +1,14 @@
# Changes

## 1.0.1 / 2013-05-01

- Update method library version.

## 1.0.0 / 2012-11-09

- Test integration for browsers.
- New method library.

## 0.0.1 / 2012-10-22

- Initial release
18 changes: 18 additions & 0 deletions lib/diffpatcher/License.md
@@ -0,0 +1,18 @@
Copyright 2012 Irakli Gozalishvili. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
70 changes: 70 additions & 0 deletions lib/diffpatcher/Readme.md
@@ -0,0 +1,70 @@
# diffpatcher

[![Build Status](https://secure.travis-ci.org/Gozala/diffpatcher.png)](http://travis-ci.org/Gozala/diffpatcher)

[![Browser support](https://ci.testling.com/Gozala/diffpatcher.png)](http://ci.testling.com/Gozala/diffpatcher)

Diffpatcher is a small library that lets you treat hashes as if they were
git repositories.

## diff

Diff function that takes two hashes and returns delta hash.

```js
var diff = require("diffpatcher/diff")

diff({ a: { b: 1 }, c: { d: 2 } }, // hash#1
{ a: { e: 3 }, c: { d: 4 } }) // hash#2

// => { // delta
// a: {
// b: null, // -
// e: 3 // +
// },
// c: {
// d: 4 // ±
// }
// }
```

As you can see from the example above `delta` makes no real distinction between
proprety upadate and property addition. Try to think of additions as an update
from `undefined` to whatever it's being updated to.

## patch

Patch fuction takes a `hash` and a `delta` and returns a new `hash` which is
just like orginial but with delta applied to it. Let's apply delta from the
previous example to the first hash from the same example


```js
var patch = require("diffpatcher/patch")

patch({ a: { b: 1 }, c: { d: 2 } }, // hash#1
{ // delta
a: {
b: null, // -
e: 3 // +
},
c: {
d: 4 // ±
}
})

// => { a: { e: 3 }, c: { d: 4 } } // hash#2
```

That's about it really, just diffing hashes and applying thes diffs on them.


### rebase

And as Linus mentioned everything in git can be expressed with `rebase`, that
also pretty much the case for `diffpatcher`. `rebase` takes `target` hash,
and rebases `parent` onto it with `diff` applied.

## Install

npm install diffpatcher
45 changes: 45 additions & 0 deletions lib/diffpatcher/diff.js
@@ -0,0 +1,45 @@
"use strict";

var method = require("method/core")

// Method is designed to work with data structures representing application
// state. Calling it with a state should return object representing `delta`
// that has being applied to a previous state to get to a current state.
//
// Example
//
// diff(state) // => { "item-id-1": { title: "some title" } "item-id-2": null }
var diff = method("diff@diffpatcher")

// diff between `null` / `undefined` to any hash is a hash itself.
diff.define(null, function(from, to) { return to })
diff.define(undefined, function(from, to) { return to })
diff.define(Object, function(from, to) {
return calculate(from, to || {}) || {}
})

function calculate(from, to) {
var diff = {}
var changes = 0
Object.keys(from).forEach(function(key) {
changes = changes + 1
if (!(key in to) && from[key] != null) diff[key] = null
else changes = changes - 1
})
Object.keys(to).forEach(function(key) {
changes = changes + 1
var previous = from[key]
var current = to[key]
if (previous === current) return (changes = changes - 1)
if (typeof(current) !== "object") return diff[key] = current
if (typeof(previous) !== "object") return diff[key] = current
var delta = calculate(previous, current)
if (delta) diff[key] = delta
else changes = changes - 1
})
return changes ? diff : null
}

diff.calculate = calculate

module.exports = diff
5 changes: 5 additions & 0 deletions lib/diffpatcher/index.js
@@ -0,0 +1,5 @@
"use strict";

exports.diff = require("./diff")
exports.patch = require("./patch")
exports.rebase = require("./rebase")
54 changes: 54 additions & 0 deletions lib/diffpatcher/package.json
@@ -0,0 +1,54 @@
{
"name": "diffpatcher",
"id": "diffpatcher",
"version": "1.2.0",
"description": "Utilities for diff-ing & patch-ing hashes",
"keywords": [
"diff", "patch", "rebase", "hash", "changes", "versions"
],
"author": "Irakli Gozalishvili <rfobic@gmail.com> (http://jeditoolkit.com)",
"homepage": "https://github.com/Gozala/diffpatcher",
"repository": {
"type": "git",
"url": "https://github.com/Gozala/diffpatcher.git",
"web": "https://github.com/Gozala/diffpatcher"
},
"bugs": {
"url": "http://github.com/Gozala/diffpatcher/issues/"
},
"dependencies": {
"method": "~2.0.0"
},
"devDependencies": {
"test": "~0.x.0",
"phantomify": "~0.x.0",
"retape": "~0.x.0",
"tape": "~0.1.5"
},
"main": "./index.js",
"scripts": {
"test": "npm run test-node && npm run test-browser",
"test-browser": "node ./node_modules/phantomify/bin/cmd.js ./test/common.js",
"test-node": "node ./test/common.js",
"test-tap": "node ./test/tap.js"
},
"testling": {
"files": "test/tap.js",
"browsers": [
"ie/9..latest",
"chrome/25..latest",
"firefox/20..latest",
"safari/6..latest",
"opera/11.0..latest",
"iphone/6..latest",
"ipad/6..latest",
"android-browser/4.2..latest"
]
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/Gozala/diffpatcher/License.md"
}
]
}
21 changes: 21 additions & 0 deletions lib/diffpatcher/patch.js
@@ -0,0 +1,21 @@
"use strict";

var method = require("method/core")
var rebase = require("./rebase")

// Method is designed to work with data structures representing application
// state. Calling it with a state and delta should return object representing
// new state, with changes in `delta` being applied to previous.
//
// ## Example
//
// patch(state, {
// "item-id-1": { completed: false }, // update
// "item-id-2": null // delete
// })
var patch = method("patch@diffpatcher")
patch.define(Object, function patch(hash, delta) {
return rebase({}, hash, delta)
})

module.exports = patch
36 changes: 36 additions & 0 deletions lib/diffpatcher/rebase.js
@@ -0,0 +1,36 @@
"use strict";

var nil = {}
var owns = ({}).hasOwnProperty

function rebase(result, parent, delta) {
var key, current, previous, update
for (key in parent) {
if (owns.call(parent, key)) {
previous = parent[key]
update = owns.call(delta, key) ? delta[key] : nil
if (previous === null) continue
else if (previous === void(0)) continue
else if (update === null) continue
else if (update === void(0)) continue
else result[key] = previous
}
}
for (key in delta) {
if (owns.call(delta, key)) {
update = delta[key]
current = owns.call(result, key) ? result[key] : nil
if (current === update) continue
else if (update === null) continue
else if (update === void(0)) continue
else if (current === nil) result[key] = update
else if (typeof(update) !== "object") result[key] = update
else if (typeof(current) !== "object") result[key] = update
else result[key]= rebase({}, current, update)
}
}

return result
}

module.exports = rebase
3 changes: 3 additions & 0 deletions lib/diffpatcher/test/common.js
@@ -0,0 +1,3 @@
"use strict";

require("test").run(require("./index"))

0 comments on commit aa3666f

Please sign in to comment.