blob: c770d71ff42e4b1198bd9763b788454d231f638d [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;
Felix Meschberger98f1be02010-03-03 12:48:20 +000023var ifStatusOK = false;
Felix Meschbergerf15dc7a2010-02-21 16:45:55 +000024
25/* displays a date in the user's local timezone */
26function localTm(time) {
27 return (time ? new Date(time) : new Date()).toLocaleString();
28}
29
30function doRepoAction(action, url) {
31 if ( !url ) {
32 Xalert('Invalid URI: ' + url, 'Error');
33 } else {
34 $.post(pluginRoot, {
35 'action' : action,
36 'url' : url
37 }, renderData, 'json');
38 }
39}
40
41function renderResource(res) {
42 // aply filtering
43 var match = searchField.val();
44 if (match) {
45 match = new RegExp( match );
46 if ( !match.test(res.presentationname) ) return;
47 }
48
49 // proceed with resource
50 var _id = res.symbolicname.replace(/\./g, '_');
51 var _tr = resTable.find('#' + _id);
52
53 if (_tr.length == 0) { // not created yet, create it
54 var _select = createElement('select', null, { name : 'bundle' }, [
55 createElement( 'option', null, { value : '-' }, [
56 text( i18n.selectVersion)
57 ]),
58 createElement( 'option', null, { value : res.id }, [
59 text( res.version + (res.installed ? ' *' : '') )
60 ])
61 ]);
62 _tr = tr( null, { 'id' : _id } , [
63 td( null, null, [ _select ] ),
64 td( null, null, [ text(res.presentationname) ] ),
65 td( null, null, [ text(res.installed ? res.version : '') ] )
66 ]);
67 resTable.append( _tr );
68 } else { // append the additional version
69 _tr.find( 'select' ).append (
70 createElement( 'option', null, { value : res.id }, [
71 text( res.version + (res.installed ? ' *' : '') )
72 ])
73 );
74 if (res.installed) _tr.find( 'td:eq(2)' ).text( res.version );
75 }
76}
77
78function renderRepository(repo) {
79 var _tr = repoTableTemplate.clone();
80 _tr.find('td:eq(0)').text( repo.name );
81 _tr.find('td:eq(1)').text( repo.url );
82 _tr.find('td:eq(2)').text( localTm(repo.lastModified) );
83 _tr.find('li:eq(0)').click(function() {
84 doRepoAction('refresh', repo.url);
85 });
86 _tr.find('li:eq(1)').click(function() {
87 doRepoAction('delete', repo.url);
88 });
89 repoTable.append(_tr);
90
91 for(var i in repo.resources) {
92 renderResource( repo.resources[i] );
93 }
94}
95
96function renderData(data) {
97 obrData = data;
98 repoTable.empty();
99 resTable.empty();
100 if ( data.status ) {
101 $('.statline').html(i18n.status_ok);
Felix Meschberger98f1be02010-03-03 12:48:20 +0000102 ifStatusOK.removeClass('ui-helper-hidden');
Felix Meschbergerf15dc7a2010-02-21 16:45:55 +0000103 for (var i in data.repositories ) {
104 renderRepository( data.repositories[i] );
105 }
106 } else {
107 $('.statline').html(i18n.status_no);
Felix Meschberger98f1be02010-03-03 12:48:20 +0000108 ifStatusOK.addClass('ui-helper-hidden');
Felix Meschbergerf15dc7a2010-02-21 16:45:55 +0000109 }
110}
111
112$(document).ready( function() {
113 repoTable = $('#repoTable tbody');
114 repoTableTemplate = repoTable.find('tr').clone();
115 addRepoUri = $('#addRepoUri');
116 resTable = $('#resTable tbody').empty();
117 searchField = $('#searchField');
Felix Meschberger98f1be02010-03-03 12:48:20 +0000118 ifStatusOK = $('#ifStatusOK');
Felix Meschbergerf15dc7a2010-02-21 16:45:55 +0000119
120 $('#addRepoBtn').click(function() {
121 doRepoAction('add', addRepoUri.val());
122 });
123 $('#searchBtn').click(function() {
124 renderData(obrData);
125 return false;
126 });
127
128 renderData(obrData);
129});