move code up one directory
[atutor.git] / mods / _standard / social / lib / js / container.js
1 var Container = Class.create();
2 Container.prototype = {
3         maxHeight: 4096,
4         
5         initialize: function() {
6                 // rpc services our container supports
7                 gadgets.rpc.register('resize_iframe', this.setHeight);
8                 gadgets.rpc.register('set_pref', this.setUserPref);
9                 gadgets.rpc.register('set_title', this.setTitle);
10                 gadgets.rpc.register('requestNavigateTo', this.requestNavigateTo);
11         },
12         
13         setHeight: function(height) {
14                 if ($(this.f) != undefined) {
15                         // compensate for margin/padding offsets in some browsers (ugly hack but functional)
16                         height += 28;
17                         // change the height of the gadget iframe, limit to maxHeight height
18                         if (height > gadgets.container.maxHeight) {
19                                 height = gadgets.container.maxHeight;
20                         }
21                         Element.setStyle($(this.f), {'height':height+'px'});
22                 }
23         },
24         
25         _parseIframeUrl: function(url) {
26                 // parse the iframe url to extract the key = value pairs from it
27                 var ret = new Object();
28                 var hashParams = url.replace(/#.*$/, '').split('&');
29                 var param = key = val = '';
30                 for (i = 0 ; i < hashParams.length; i++) {
31                         param = hashParams[i];
32                         key = param.substr(0, param.indexOf('='));
33                         val = param.substr(param.indexOf('=') + 1);
34                         ret[key] = val;
35                 }
36                 return ret;
37         },
38         
39         setUserPref: function(editToken, name, value) {
40                 // we use the security token to tell our backend who this is (app/mod/viewer)
41                 // since it's the only fail safe way of doing so
42                 if ($(this.f) != undefined) {
43                         var params = gadgets.container._parseIframeUrl($(this.f).src);
44                         //TODO use params.st to make the store request, it holds the owner / viewer / app id / mod id required
45                         new Ajax.Request('./mods/_standard/social/set_prefs.php', {method: 'get', parameters: { name: name, value: value, st: params.st }});
46                 }
47         },
48         
49         setTitle: function(title) {
50                 var element = $(this.f+'_title');
51                 if (element != undefined) {
52                         // update the title, and make sure we don't break it's html
53                         element.update(title.replace(/&/g, '&amp;').replace(/</g, '&lt;'));
54                 }
55         },
56         
57         _getUrlForView: function(view, person, app, mod) {
58                 if (view === 'home') {
59                         return './mods/_standard/social/index.php';
60                 } else if (view === 'profile') {
61                         return '/sprofile.php?id='+person;
62                 } else if (view === 'canvas') {
63                         return './mods/_standard/social/applications.php?app_id='+app;
64                 } else {
65                         return null;
66                 }
67         },
68         
69         requestNavigateTo: function(view, opt_params) {
70                 if ($(this.f) != undefined) {
71                         var params = gadgets.container._parseIframeUrl($(this.f).src);
72                         var url = gadgets.container._getUrlForView(view, params.owner, params.aid, params.mid);
73                         if (opt_params) {
74                                 var paramStr = Object.toJSON(opt_params);
75                                 if (paramStr.length > 0) {
76                                         url += '?appParams=' + encodeURIComponent(paramStr);
77                                 }
78                         }
79                         if (url && document.location.href.indexOf(url) == -1) {
80                                 document.location.href = url;
81                         }
82                 }
83         }
84 }
85
86 /**
87  * Create the container class on page load
88  */
89 gadgets.container = new Container();