Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / webapp / js / winery-support.js
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *    Oliver Kopp - initial API and implementation and/or initial documentation
11  *******************************************************************************/
12
13 /* no name given as RequireJS' documentatio says that makes modules less portable */
14 define(["datatables"], function() {
15                 /*
16                 * Valid chars: See
17                 * <ul>
18                 * <li>http://www.w3.org/TR/REC-xml-names/#NT-NCName</li>
19                 * <li>http://www.w3.org/TR/REC-xml/#NT-Name</li>
20                 * </ul>
21                 */
22                 // NameCharRange \u10000-\ueffff is not supported by Java
23                 var NCNameStartChar_RegExp = "[A-Z_a-z\u00c0-\u00d6\u00d8\u00f6\u00f8\u02ff\u0370\u037d\u037f-\u1fff\u200c-\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd]";
24                 var NCNameChar_RegExp = NCNameStartChar_RegExp + "|[-\\.0-9\u00B7\u0300-\u036F\u203F-\u2040]";
25                 var NCName_RegExp = NCNameStartChar_RegExp + "(" + NCNameChar_RegExp + ")*";
26                 var QName_RegExp = "(" + NCName_RegExp + ":)?(" + NCName_RegExp + ")";
27
28                 var NCNameStartChar_Pattern = new RegExp(NCNameStartChar_RegExp);
29                 var NCNameChar_Pattern = new RegExp(NCNameChar_RegExp);
30
31                 /**
32                  * Initializes a table (and updates tableInfo)
33                  *  * adds selection capability
34                  *
35                  * @param info.id: id of the table  (with #)
36                  * @param paramsForDataTable: additional parameters for dataTable()
37                  * @param afterInit function to be called after dataTables object initalization happened
38                  *
39                  * @returns initialized info object
40                  *   * info.table is updated with a pointer to the data table
41                  *   * info.selectedTr is set to null
42                  */
43                 function initTable(info, paramsForDataTable, afterInit) {
44                         paramsForDataTable = paramsForDataTable || {};
45
46                         $(info.id).click(function(event) {
47                                 if (info.selectedTr != null) {
48                                         info.selectedTr.removeClass('row_selected');
49                                 }
50                                 var row = $(event.target.parentNode);
51                                 if ((info.selectedTr != null) && (info.selectedTr[0] == row[0])) {
52                                         // row is deselected if selected again
53                                         info.selectedRow = null;
54                                         info.selectedTr = null;
55                                 } else {
56                                         info.selectedTr = row;
57                                         info.selectedTr.addClass('row_selected');
58                                         info.selectedRow = event.target.parentNode;
59                                 }
60                         });
61                         info.selectedTr = null;
62                         info.table = $(info.id).dataTable(paramsForDataTable);
63                         if (afterInit) afterInit();
64                 }
65
66                 /**
67                  * Function to determine whether a data table described with the tableInfo object is empty
68                  *
69                  * @param tableInfo the table info object describing the table
70                  * @returns true if the table is empty, false otherwise
71                  */
72                 function isEmptyTable(tableInfo) {
73                         return tableInfo.table.children("tbody").children("tr").first().children("td").hasClass("dataTables_empty");
74                 }
75
76                 /**
77                  * JavaScript implementation of org.eclipse.winery.common.Util.makeNCName(String)
78                  */
79                 function makeNCName(text) {
80                         if (!text || text == "") {
81                                 return text;
82                         }
83
84                         var res = "";
85
86                         var start = text.substr(0, 1);
87                         if (NCNameStartChar_Pattern.test(start)) {
88                                 res += start;
89                         } else {
90                                 res += "_";
91                         }
92
93                         for (var i=1; i<text.length; i++) {
94                                 var c = (text.substr(i, 1));
95                                 if (NCNameChar_Pattern.test(c)) {
96                                         res += c;
97                                 } else {
98                                         res += "_";
99                                 }
100                         }
101                         return res;
102                 }
103
104                 var module = {
105                         initTable: initTable,
106                         isEmptyTable: isEmptyTable,
107                         makeNCName: makeNCName,
108                         QName_RegExp: QName_RegExp
109                 };
110                 return module;
111         }
112 );