~stephen-stewart/online-services-common-js/update-global-nav

« back to all changes in this revision

Viewing changes to build/util-share/util-share-debug.js

  • Committer: Stephen Stewart
  • Date: 2014-02-22 15:05:16 UTC
  • Revision ID: stephen.stewart@canonical.com-20140222150516-rkzti2c43ggwr2ta
import latest js, convert

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
YUI.add('util-share', function (Y, NAME) {
 
2
 
 
3
"use strict";
 
4
Y.namespace("U1.util");
 
5
 
 
6
// TODO web intents?
 
7
 
 
8
var Share = function(link, photo) {
 
9
 
 
10
    this.photo = photo;
 
11
    this.link = link;
 
12
 
 
13
};
 
14
 
 
15
Share.prototype = {
 
16
 
 
17
    getUrl: function() {
 
18
        if (this.url && this.url.length) {
 
19
            return this.url;
 
20
        } else {
 
21
            throw new Error("must set a url type before get");
 
22
        }
 
23
    },
 
24
 
 
25
    setUrl: function(type) {
 
26
        switch (type) {
 
27
            case 'facebook':
 
28
                this.url = this._facebookUrl(this.link, this.photo);
 
29
            break;
 
30
            case 'twitter':
 
31
                this.url = this._twitterUrl(this.link);
 
32
            break;
 
33
            default:
 
34
        }
 
35
 
 
36
        return this;
 
37
 
 
38
    },
 
39
 
 
40
    /**
 
41
     * Opens a window to share the link
 
42
     */
 
43
    open: function(w, h, name) {
 
44
        name = name || "U1Share";
 
45
        var url = this.getUrl(),
 
46
            conf = this.getConfString(w, h),
 
47
            u1Window;
 
48
        u1Window = Y.config.win.open(url, name, conf);
 
49
    },
 
50
 
 
51
    /**
 
52
     * Generated a conf string from width and height
 
53
        @method generateConfString
 
54
        @param {Number} w the window width (defaults to 420)
 
55
        @param {Number} h the window height (defaults to 480)
 
56
     */
 
57
    getConfString: function(w, h) {
 
58
        w = w || 420;
 
59
        h = h || 480;
 
60
        var win = Y.config.win,
 
61
            left_ = parseInt((win.screen.width/2)-(w/2), 10),
 
62
            top_ = parseInt((win.screen.height/2)-(h/2), 10);
 
63
        return "width="+w+",height="+h+",left="+left_+",top="+top_+",status=yes";
 
64
    },
 
65
 
 
66
    _facebookUrl: function(link, photo) {
 
67
        var url = "http://www.facebook.com/dialog/feed?",
 
68
            id = "166704313352575",
 
69
            display = "popup",
 
70
            redirect = "https://one.ubuntu.com/window-close/";
 
71
 
 
72
        if (link && link.length) {
 
73
 
 
74
            url += ["app_id=" + id,
 
75
                "link="+ window.encodeURI(link),
 
76
                "redirect_uri="+ window.encodeURI(redirect),
 
77
                "display="+ display
 
78
            ].join('&');
 
79
 
 
80
            if (photo && photo.length) {
 
81
                url += "&picture="+ window.encodeURI(photo);
 
82
            }
 
83
 
 
84
            return url;
 
85
 
 
86
        } else {
 
87
            throw new Error("_createFacebookUrl requires link to share");
 
88
        }
 
89
    },
 
90
 
 
91
    _twitterUrl: function(link) {
 
92
        var url = "https://twitter.com/share?";
 
93
 
 
94
        if (link && link.length) {
 
95
 
 
96
            url += [
 
97
                "url="+ window.encodeURI(link),
 
98
                "text="+ window.encodeURI("Sharing a link from UbuntuOne."),
 
99
                "hashtags=ubuntuone"
 
100
            ].join('&');
 
101
 
 
102
 
 
103
            return url;
 
104
 
 
105
        } else {
 
106
            throw new Error("createTwitterUrl requires link to share");
 
107
        }
 
108
    }
 
109
 
 
110
};
 
111
 
 
112
Y.U1.util.Share = Share;
 
113
 
 
114
 
 
115
}, '@VERSION@', {"requires": ["base", "node"]});