Debian Codegraph @

manuals/en/js/index.js

raw
1 var Docs = {
2 docs_deps: null,
3 docs_types: [ 'main', 'console', 'operator' ],
4 current_doc_type: null,
5 current_key: null,
6 patterns: {
7 version: /^\d+\.\d+$/,
8 key: /^[\w\d\:\.\-]{1,127}$/
9 },
10 urls: {
11 base: document.location.href.replace(/\/index.html.*$/, ''),
12 docs_content: 'html',
13 docs_def: 'html/main/docs.json'
14 },
15 init: function() {
16 // version is currently not used (only validation for version works)
17 var version = this.get_url_param('version');
18 if (this.validate_product_version(version) == false) {
19 this.go_to_default_page();
20 }
21 var doc_type = this.get_url_param('manual') || 'main';
22 if (this.validate_doc_type(doc_type) == false) {
23 this.go_to_default_page();
24 }
25 var key = this.get_url_param('key');
26 if (this.validate_doc_key(key) == false) {
27 this.go_to_default_page();
28 }
29 this.current_doc_type = doc_type;
30 this.current_key = key;
31 this.load_docs();
32 },
33 validate_product_version: function(version) {
34 return this.patterns.version.test(version);
35 },
36 validate_doc_key: function(key) {
37 return this.patterns.key.test(key);
38 },
39 validate_doc_type: function(doc_type) {
40 return this.docs_types.indexOf(doc_type) !== -1;
41 },
42 load_docs: function() {
43 var url = this.urls.base + '/' + this.urls.docs_def;
44 this.send_request(url);
45 },
46 set_doc_content: function(value) {
47 this.docs_deps = value;
48 },
49 get_doc_content: function() {
50 return (this.docs_deps.hasOwnProperty(this.current_key)) ? this.docs_deps[this.current_key] : "";
51 },
52 get_doc_by_params: function(doc_type, key) {
53 var doc_url = [ this.urls.base, this.urls.docs_content, this.current_doc_type, this.get_doc_content() ].join('/');
54 doc_url += '#' + this.current_key;
55 window.location.replace(doc_url);
56 },
57 go_to_default_page: function() {
58 var default_url = [ this.urls.base, this.urls.docs_content, 'main', 'index.html' ].join('/');
59 document.location.replace(default_url);
60 },
61 get_xhr: function() {
62 var xhr;
63 if (window.XMLHttpRequest) {
64 xhr = new XMLHttpRequest();
65 } else {
66 xhr = new ActiveXObject("Microsoft.XMLHTTP");
67 }
68 return xhr;
69 },
70 send_request: function(url) {
71 var xhr = this.get_xhr();
72 var result;
73 xhr.open('GET', url, true);
74 xhr.onreadystatechange = function () {
75 if (xhr.readyState == 4 && xhr.status == 200) {
76 var result = eval('(' + xhr.responseText + ')');
77 this.set_doc_content(result);
78 this.get_doc_by_params();
79 }
80 }.bind(this);
81 xhr.send();
82 },
83 get_url_param: function(name) {
84 name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
85 var regex = new RegExp("[\\?&;]" + name + "=([^&;#]*)"),
86 results = regex.exec(location.search);
87 var ret = (results != null) ? decodeURIComponent(results[1].replace(/\+/g, " ")) : "";
88 return ret.trim();
89 }
90 }
91
92 Docs.init();