blob: 53e532b5a3d6d552f69175bcbcae48711b457892 [file] [log] [blame]
Felix Meschbergerf15dc7a2010-02-21 16:45:55 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18var repoTable = false;
19var repoTableTemplate = false;
20var addRepoUri = false;
21var resTable = false;
22var searchField = false;
23
24/* displays a date in the user's local timezone */
25function localTm(time) {
26 return (time ? new Date(time) : new Date()).toLocaleString();
27}
28
29function doRepoAction(action, url) {
30 if ( !url ) {
31 Xalert('Invalid URI: ' + url, 'Error');
32 } else {
33 $.post(pluginRoot, {
34 'action' : action,
35 'url' : url
36 }, renderData, 'json');
37 }
38}
39
40function renderResource(res) {
41 // aply filtering
42 var match = searchField.val();
43 if (match) {
44 match = new RegExp( match );
45 if ( !match.test(res.presentationname) ) return;
46 }
47
48 // proceed with resource
49 var _id = res.symbolicname.replace(/\./g, '_');
50 var _tr = resTable.find('#' + _id);
51
52 if (_tr.length == 0) { // not created yet, create it
53 var _select = createElement('select', null, { name : 'bundle' }, [
54 createElement( 'option', null, { value : '-' }, [
55 text( i18n.selectVersion)
56 ]),
57 createElement( 'option', null, { value : res.id }, [
58 text( res.version + (res.installed ? ' *' : '') )
59 ])
60 ]);
61 _tr = tr( null, { 'id' : _id } , [
62 td( null, null, [ _select ] ),
63 td( null, null, [ text(res.presentationname) ] ),
64 td( null, null, [ text(res.installed ? res.version : '') ] )
65 ]);
66 resTable.append( _tr );
67 } else { // append the additional version
68 _tr.find( 'select' ).append (
69 createElement( 'option', null, { value : res.id }, [
70 text( res.version + (res.installed ? ' *' : '') )
71 ])
72 );
73 if (res.installed) _tr.find( 'td:eq(2)' ).text( res.version );
74 }
75}
76
77function renderRepository(repo) {
78 var _tr = repoTableTemplate.clone();
79 _tr.find('td:eq(0)').text( repo.name );
80 _tr.find('td:eq(1)').text( repo.url );
81 _tr.find('td:eq(2)').text( localTm(repo.lastModified) );
82 _tr.find('li:eq(0)').click(function() {
83 doRepoAction('refresh', repo.url);
84 });
85 _tr.find('li:eq(1)').click(function() {
86 doRepoAction('delete', repo.url);
87 });
88 repoTable.append(_tr);
89
90 for(var i in repo.resources) {
91 renderResource( repo.resources[i] );
92 }
93}
94
95function renderData(data) {
96 obrData = data;
97 repoTable.empty();
98 resTable.empty();
99 if ( data.status ) {
100 $('.statline').html(i18n.status_ok);
101 for (var i in data.repositories ) {
102 renderRepository( data.repositories[i] );
103 }
104 } else {
105 $('.statline').html(i18n.status_no);
106 }
107}
108
109$(document).ready( function() {
110 repoTable = $('#repoTable tbody');
111 repoTableTemplate = repoTable.find('tr').clone();
112 addRepoUri = $('#addRepoUri');
113 resTable = $('#resTable tbody').empty();
114 searchField = $('#searchField');
115
116 $('#addRepoBtn').click(function() {
117 doRepoAction('add', addRepoUri.val());
118 });
119 $('#searchBtn').click(function() {
120 renderData(obrData);
121 return false;
122 });
123
124 renderData(obrData);
125});