##// END OF EJS Templates
frontend: reorganize how we handle plugins
ergo -
r764:52c2b99a default
parent child Browse files
Show More
@@ -1,219 +1,225 b''
1 1 "use strict";
2 2 /** leak object to top level scope **/
3 3 var ccLog = undefined;
4 4 // global code-mirror logger;, to enable run
5 5 // Logger.get('ConnectionController').setLevel(Logger.DEBUG)
6 6 ccLog = Logger.get('ConnectionController');
7 7 ccLog.setLevel(Logger.OFF);
8 8
9 9 var ConnectionController;
10 10 var connCtrlr;
11 11 var registerViewChannels;
12 12
13 13 (function () {
14 14 ConnectionController = function (webappUrl, serverUrl, urls) {
15 15 var self = this;
16 16
17 17 var channels = ['broadcast'];
18 18 this.state = {
19 19 open: false,
20 20 webappUrl: webappUrl,
21 21 serverUrl: serverUrl,
22 22 connId: null,
23 23 socket: null,
24 24 channels: channels,
25 25 heartbeat: null,
26 26 channelsInfo: {},
27 27 urls: urls
28 28 };
29 29 this.channelNameParsers = [];
30 30
31 31 this.addChannelNameParser = function (fn) {
32 32 if (this.channelNameParsers.indexOf(fn) === -1) {
33 33 this.channelNameParsers.push(fn);
34 34 }
35 35 };
36 36
37 37 this.listen = function () {
38 38 if (window.WebSocket) {
39 39 ccLog.debug('attempting to create socket');
40 40 var socket_url = self.state.serverUrl + "/ws?conn_id=" + self.state.connId;
41 41 var socket_conf = {
42 42 url: socket_url,
43 43 handleAs: 'json',
44 44 headers: {
45 45 "Accept": "application/json",
46 46 "Content-Type": "application/json"
47 47 }
48 48 };
49 49 self.state.socket = new WebSocket(socket_conf.url);
50 50
51 51 self.state.socket.onopen = function (event) {
52 52 ccLog.debug('open event', event);
53 53 if (self.state.heartbeat === null) {
54 54 self.state.heartbeat = setInterval(function () {
55 55 if (self.state.socket.readyState === WebSocket.OPEN) {
56 56 self.state.socket.send('heartbeat');
57 57 }
58 58 }, 10000)
59 59 }
60 60 };
61 61 self.state.socket.onmessage = function (event) {
62 62 var data = $.parseJSON(event.data);
63 63 for (var i = 0; i < data.length; i++) {
64 64 if (data[i].message.topic) {
65 65 ccLog.debug('publishing',
66 66 data[i].message.topic, data[i]);
67 67 $.Topic(data[i].message.topic).publish(data[i])
68 68 }
69 69 else {
70 70 ccLog.warn('unhandled message', data);
71 71 }
72 72 }
73 73 };
74 74 self.state.socket.onclose = function (event) {
75 75 ccLog.debug('closed event', event);
76 76 setTimeout(function () {
77 77 self.connect(true);
78 78 }, 5000);
79 79 };
80 80
81 81 self.state.socket.onerror = function (event) {
82 82 ccLog.debug('error event', event);
83 83 };
84 84 }
85 85 else {
86 86 ccLog.debug('attempting to create long polling connection');
87 87 var poolUrl = self.state.serverUrl + "/listen?conn_id=" + self.state.connId;
88 88 self.state.socket = $.ajax({
89 89 url: poolUrl
90 90 }).done(function (data) {
91 91 ccLog.debug('data', data);
92 92 var data = $.parseJSON(data);
93 93 for (var i = 0; i < data.length; i++) {
94 94 if (data[i].message.topic) {
95 95 ccLog.info('publishing',
96 96 data[i].message.topic, data[i]);
97 97 $.Topic(data[i].message.topic).publish(data[i])
98 98 }
99 99 else {
100 100 ccLog.warn('unhandled message', data);
101 101 }
102 102 }
103 103 self.listen();
104 104 }).fail(function () {
105 105 ccLog.debug('longpoll error');
106 106 setTimeout(function () {
107 107 self.connect(true);
108 108 }, 5000);
109 109 });
110 110 }
111 111
112 112 };
113 113
114 114 this.connect = function (create_new_socket) {
115 115 var connReq = {'channels': self.state.channels};
116 116 ccLog.debug('try obtaining connection info', connReq);
117 117 $.ajax({
118 118 url: self.state.urls.connect,
119 119 type: "POST",
120 120 contentType: "application/json",
121 121 data: JSON.stringify(connReq),
122 122 dataType: "json"
123 123 }).done(function (data) {
124 124 ccLog.debug('Got connection:', data.conn_id);
125 125 self.state.channels = data.channels;
126 126 self.state.channelsInfo = data.channels_info;
127 127 self.state.connId = data.conn_id;
128 128 if (create_new_socket) {
129 129 self.listen();
130 130 }
131 131 self.update();
132 132 }).fail(function () {
133 133 setTimeout(function () {
134 134 self.connect(create_new_socket);
135 135 }, 5000);
136 136 });
137 137 self.update();
138 138 };
139 139
140 140 this.subscribeToChannels = function (channels) {
141 141 var new_channels = [];
142 142 for (var i = 0; i < channels.length; i++) {
143 143 var channel = channels[i];
144 144 if (self.state.channels.indexOf(channel)) {
145 145 self.state.channels.push(channel);
146 146 new_channels.push(channel)
147 147 }
148 148 }
149 149 /**
150 150 * only execute the request if socket is present because subscribe
151 151 * can actually add channels before initial app connection
152 152 **/
153 153 if (new_channels && self.state.socket !== null) {
154 154 var connReq = {
155 155 'channels': self.state.channels,
156 156 'conn_id': self.state.connId
157 157 };
158 158 $.ajax({
159 159 url: self.state.urls.subscribe,
160 160 type: "POST",
161 161 contentType: "application/json",
162 162 data: JSON.stringify(connReq),
163 163 dataType: "json"
164 164 }).done(function (data) {
165 165 self.state.channels = data.channels;
166 166 self.state.channelsInfo = data.channels_info;
167 167 self.update();
168 168 });
169 169 }
170 170 self.update();
171 171 };
172 172
173 173 this.update = function () {
174 174 for (var key in this.state.channelsInfo) {
175 175 if (this.state.channelsInfo.hasOwnProperty(key)) {
176 176 // update channels with latest info
177 177 $.Topic('/connection_controller/channel_update').publish(
178 178 {channel: key, state: this.state.channelsInfo[key]});
179 179 }
180 180 }
181 181 /**
182 182 * checks current channel list in state and if channel is not present
183 183 * converts them into executable "commands" and pushes them on topics
184 184 */
185 185 for (var i = 0; i < this.state.channels.length; i++) {
186 186 var channel = this.state.channels[i];
187 187 for (var j = 0; j < this.channelNameParsers.length; j++) {
188 188 this.channelNameParsers[j](channel);
189 189 }
190 190 }
191 191 };
192 192
193 193 this.run = function () {
194 194 this.connect(true);
195 195 };
196 196
197 197 $.Topic('/connection_controller/subscribe').subscribe(
198 198 self.subscribeToChannels);
199 199 };
200 200
201 201 $.Topic('/plugins/__REGISTER__').subscribe(function (data) {
202 // enable chat controller
203 202 if (window.CHANNELSTREAM_SETTINGS && window.CHANNELSTREAM_SETTINGS.enabled) {
203 connCtrlr = new ConnectionController(
204 CHANNELSTREAM_SETTINGS.webapp_location,
205 CHANNELSTREAM_SETTINGS.ws_location,
206 CHANNELSTREAM_URLS
207 );
208 registerViewChannels();
209
204 210 $(document).ready(function () {
205 211 connCtrlr.run();
206 212 });
207 213 }
208 214 });
209 215
210 216 registerViewChannels = function (){
211 217 // subscribe to PR repo channel for PR's'
212 218 if (templateContext.pull_request_data.pull_request_id) {
213 219 var channelName = '/repo$' + templateContext.repo_name + '$/pr/' +
214 220 String(templateContext.pull_request_data.pull_request_id);
215 221 connCtrlr.state.channels.push(channelName);
216 222 }
217 223 }
218 224
219 225 })();
@@ -1,14 +1,11 b''
1 1 <%
2 2 from pyramid.renderers import render as pyramid_render
3 3 from pyramid.threadlocal import get_current_registry, get_current_request
4 4 pyramid_registry = get_current_registry()
5 5 %>
6 6 % for plugin, config in getattr(pyramid_registry, 'rhodecode_plugins', {}).items():
7 7 % if config['template_hooks'].get('plugin_init_template'):
8 8 ${pyramid_render(config['template_hooks'].get('plugin_init_template'),
9 9 {'config':config}, request=get_current_request(), package='rc_ae')|n}
10 10 % endif
11 11 % endfor
12 <script>
13 $.Topic('/plugins/__REGISTER__').prepareOrPublish({});
14 </script>
@@ -1,177 +1,179 b''
1 1 ## -*- coding: utf-8 -*-
2 2 <!DOCTYPE html>
3 3
4 4 <%
5 5 c.template_context['repo_name'] = getattr(c, 'repo_name', '')
6 6
7 7 if hasattr(c, 'rhodecode_db_repo'):
8 8 c.template_context['repo_type'] = c.rhodecode_db_repo.repo_type
9 9 c.template_context['repo_landing_commit'] = c.rhodecode_db_repo.landing_rev[1]
10 10
11 11 if getattr(c, 'rhodecode_user', None) and c.rhodecode_user.user_id:
12 12 c.template_context['rhodecode_user']['username'] = c.rhodecode_user.username
13 13 c.template_context['rhodecode_user']['email'] = c.rhodecode_user.email
14 14 c.template_context['rhodecode_user']['notification_status'] = c.rhodecode_user.get_instance().user_data.get('notification_status', True)
15 15
16 16 c.template_context['visual']['default_renderer'] = h.get_visual_attr(c, 'default_renderer')
17 17 %>
18 18 <html xmlns="http://www.w3.org/1999/xhtml">
19 19 <head>
20 20 <title>${self.title()}</title>
21 21 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
22 22 <%def name="robots()">
23 23 <meta name="robots" content="index, nofollow"/>
24 24 </%def>
25 25 ${self.robots()}
26 26 <link rel="icon" href="${h.asset('images/favicon.ico', ver=c.rhodecode_version_hash)}" sizes="16x16 32x32" type="image/png" />
27 27
28 28 ## CSS definitions
29 29 <%def name="css()">
30 30 <link rel="stylesheet" type="text/css" href="${h.asset('css/style.css', ver=c.rhodecode_version_hash)}" media="screen"/>
31 31 <!--[if lt IE 9]>
32 32 <link rel="stylesheet" type="text/css" href="${h.asset('css/ie.css', ver=c.rhodecode_version_hash)}" media="screen"/>
33 33 <![endif]-->
34 34 ## EXTRA FOR CSS
35 35 ${self.css_extra()}
36 36 </%def>
37 37 ## CSS EXTRA - optionally inject some extra CSS stuff needed for specific websites
38 38 <%def name="css_extra()">
39 39 </%def>
40 40
41 41 ${self.css()}
42 42
43 43 ## JAVASCRIPT
44 44 <%def name="js()">
45 45 <script>
46 46 // setup Polymer options
47 47 window.Polymer = {lazyRegister: true, dom: 'shadow'};
48 48
49 49 // Load webcomponentsjs polyfill if browser does not support native Web Components
50 50 (function() {
51 51 'use strict';
52 52 var onload = function() {
53 53 // For native Imports, manually fire WebComponentsReady so user code
54 54 // can use the same code path for native and polyfill'd imports.
55 55 if (!window.HTMLImports) {
56 56 document.dispatchEvent(
57 57 new CustomEvent('WebComponentsReady', {bubbles: true})
58 58 );
59 59 }
60 60 };
61 61 var webComponentsSupported = (
62 62 'registerElement' in document
63 63 && 'import' in document.createElement('link')
64 64 && 'content' in document.createElement('template')
65 65 );
66 66 if (!webComponentsSupported) {
67 67 var e = document.createElement('script');
68 68 e.async = true;
69 69 e.src = '${h.asset('js/vendors/webcomponentsjs/webcomponents-lite.min.js', ver=c.rhodecode_version_hash)}';
70 70 document.head.appendChild(e);
71 71 } else {
72 72 onload();
73 73 }
74 74 })();
75 75 </script>
76 76
77 77 <script src="${h.asset('js/rhodecode/i18n/%s.js' % c.language, ver=c.rhodecode_version_hash)}"></script>
78 78 <script type="text/javascript">
79 79 // register templateContext to pass template variables to JS
80 80 var templateContext = ${h.json.dumps(c.template_context)|n};
81 81
82 82 var REPO_NAME = "${getattr(c, 'repo_name', '')}";
83 83 %if hasattr(c, 'rhodecode_db_repo'):
84 84 var REPO_LANDING_REV = '${c.rhodecode_db_repo.landing_rev[1]}';
85 85 var REPO_TYPE = '${c.rhodecode_db_repo.repo_type}';
86 86 %else:
87 87 var REPO_LANDING_REV = '';
88 88 var REPO_TYPE = '';
89 89 %endif
90 90 var APPLICATION_URL = "${h.url('home').rstrip('/')}";
91 91 var ASSET_URL = "${h.asset('')}";
92 92 var DEFAULT_RENDERER = "${h.get_visual_attr(c, 'default_renderer')}";
93 93 var CSRF_TOKEN = "${getattr(c, 'csrf_token', '')}";
94 94 % if getattr(c, 'rhodecode_user', None):
95 95 var USER = {name:'${c.rhodecode_user.username}'};
96 96 % else:
97 97 var USER = {name:null};
98 98 % endif
99 99
100 100 var APPENLIGHT = {
101 101 enabled: ${'true' if getattr(c, 'appenlight_enabled', False) else 'false'},
102 102 key: '${getattr(c, "appenlight_api_public_key", "")}',
103 103 % if getattr(c, 'appenlight_server_url', None):
104 104 serverUrl: '${getattr(c, "appenlight_server_url", "")}',
105 105 % endif
106 106 requestInfo: {
107 107 % if getattr(c, 'rhodecode_user', None):
108 108 ip: '${c.rhodecode_user.ip_addr}',
109 109 username: '${c.rhodecode_user.username}'
110 110 % endif
111 111 }
112 112 };
113 113 </script>
114 <%include file="/base/plugins_base.html"/>
114 115 <!--[if lt IE 9]>
115 116 <script language="javascript" type="text/javascript" src="${h.asset('js/excanvas.min.js')}"></script>
116 117 <![endif]-->
117 118 <script language="javascript" type="text/javascript" src="${h.asset('js/rhodecode/routes.js', ver=c.rhodecode_version_hash)}"></script>
118 119 <script language="javascript" type="text/javascript" src="${h.asset('js/scripts.js', ver=c.rhodecode_version_hash)}"></script>
119 120 <script>
120 121 var e = document.createElement('script');
121 122 e.src = '${h.asset('js/rhodecode-components.js', ver=c.rhodecode_version_hash)}';
122 123 document.head.appendChild(e);
123 124 </script>
124 125 <link rel="import" href="${h.asset('js/rhodecode-components.html', ver=c.rhodecode_version_hash)}">
125 126 ## avoide escaping the %N
126 127 <script>CodeMirror.modeURL = "${h.asset('') + 'js/mode/%N/%N.js?ver='+c.rhodecode_version_hash}";</script>
127 128
128 129
129 130 ## JAVASCRIPT EXTRA - optionally inject some extra JS for specificed templates
130 131 ${self.js_extra()}
131 132
132 133 <script type="text/javascript">
133 134 $(document).ready(function(){
134 135 show_more_event();
135 136 timeagoActivate();
136 137 })
137 138 </script>
138 139
139 140 </%def>
140 141
141 142 ## JAVASCRIPT EXTRA - optionally inject some extra JS for specificed templates
142 143 <%def name="js_extra()"></%def>
143 144 ${self.js()}
144 145
145 146 <%def name="head_extra()"></%def>
146 147 ${self.head_extra()}
147 <%include file="/base/plugins_base.html"/>
148
148 <script>
149 $.Topic('/plugins/__REGISTER__').prepareOrPublish({});
150 </script>
149 151 ## extra stuff
150 152 %if c.pre_code:
151 153 ${c.pre_code|n}
152 154 %endif
153 155 </head>
154 156 <body id="body">
155 157 <noscript>
156 158 <div class="noscript-error">
157 159 ${_('Please enable JavaScript to use RhodeCode Enterprise')}
158 160 </div>
159 161 </noscript>
160 162 ## IE hacks
161 163 <!--[if IE 7]>
162 164 <script>$(document.body).addClass('ie7')</script>
163 165 <![endif]-->
164 166 <!--[if IE 8]>
165 167 <script>$(document.body).addClass('ie8')</script>
166 168 <![endif]-->
167 169 <!--[if IE 9]>
168 170 <script>$(document.body).addClass('ie9')</script>
169 171 <![endif]-->
170 172
171 173 ${next.body()}
172 174 %if c.post_code:
173 175 ${c.post_code|n}
174 176 %endif
175 177 <rhodecode-toast id="notifications"></rhodecode-toast>
176 178 </body>
177 179 </html>
@@ -1,24 +1,16 b''
1 1 <script>
2 2 var CHANNELSTREAM_URLS = ${config['url_gen'](request)|n};
3 3 %if request.registry.rhodecode_plugins['channelstream']['enabled'] and c.rhodecode_user.username != h.DEFAULT_USER:
4 4 var CHANNELSTREAM_SETTINGS = {
5 5 'enabled': true,
6 6 'ws_location': '${request.registry.settings.get('channelstream.ws_url')}',
7 7 'webapp_location': '${h.url('/', qualified=True)[:-1]}'
8 8 };
9 9 %else:
10 10 var CHANNELSTREAM_SETTINGS = {
11 11 'enabled':false,
12 12 'ws_location': '',
13 13 'webapp_location': ''};
14 14 %endif
15 15
16 if (CHANNELSTREAM_SETTINGS.enabled) {
17 connCtrlr = new ConnectionController(
18 CHANNELSTREAM_SETTINGS.webapp_location,
19 CHANNELSTREAM_SETTINGS.ws_location,
20 CHANNELSTREAM_URLS
21 );
22 registerViewChannels();
23 }
24 16 </script>
General Comments 0
You need to be logged in to leave comments. Login now