summaryrefslogtreecommitdiffstats
path: root/modules-available/usblockoff/templates/usb-edit-rule.html
blob: e2854f506f32dd0d99af254e792c08813fce3425 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<form method="post" action="?do=usblockoff" id="editRuleForm">
	<input type="hidden" name="token" value="{{token}}">
	<input type="hidden" name="action" value="editRule">
	<input type="hidden" name="configid" value="{{configid}}" id="configid">
	<input type="hidden" name="ruleid" value="{{ruleid}}" id="ruleid">
	<input type="hidden" name="attributes" value="" id="attributes">

	<div class="panel panel-default">
		<div class="panel-heading">{{lang_editRule}}</div>
		<div class="panel-body">
			<div class="list-group">

				{{#attributes}}
				{{{attributesHtml}}}
				{{/attributes}}

			</div>
		</div>
	</div>

	<div class="pull-right">
		<a href="?do=usblockoff&show=edit-config&configid={{configid}}" class="btn btn-default">Cancel</a>

		<button id="addButton" class="btn btn-primary" type="submit">
			<span class="glyphicon glyphicon-floppy-disk"></span> {{lang_save}}
		</button>
	</div>

</form>

<script>
	var idArray = {{{usbJson}}};

	document.addEventListener("DOMContentLoaded", function() {
		$('a.btn[title]').tooltip({placement: "auto", html: true});

		$('.switch-input').click(function() {
			//var $op = $(this).parent().find('select');
			var $op = $(this).parent().find(':input').not('button');
			var val = $op.val();
			console.log(val);
			if ($op[0].localName == 'select') {
				$op.replaceWith($('<input class="form-control" type="text">').attr('name', $op.attr('name')).attr('id', $op.attr('id')).val(val));

				if ($op.attr('id') === 'prop-vid') {
					remakeChangeEvent(val);
				}

				$(this).parent().find('button span').removeClass('glyphicon-pencil').addClass('glyphicon glyphicon-list-alt');

			} else if ($op[0].localName == 'input') {
				$op.replaceWith($('<select class="form-control">').attr('name', $op.attr('name')).attr('id', $op.attr('id')).val(val));

				if ($op.attr('id') === 'prop-vid') {
					var vendorid = $('#prop-vid');
					$.each(idArray, function (key, value) {
						vendorid.append($('<option></option>').val(key).html(value['name']));
					});

					vendorid.val(val);
					remakeChangeEvent(val);
				} else if ($op.attr('id') === 'prop-pid') {
					var productid = $('#prop-pid');
					$.each(idArray[$('#prop-vid').val()]['products'], function(key, value) {
						productid.append($('<option></option>').val(key).html(value));
					});
					productid.val(val);
				}
				$(this).parent().find('button span').removeClass('glyphicon-list-alt').addClass('glyphicon-pencil');
			}
			remakeUpdateEvent();
		});
		remakeUpdateEvent();

		var vendorid = $('#prop-vid');
		var productid = $('#prop-pid');

		var vid = vendorid.val();
		vendorid.empty();

		vendorid.change(function () {
			var $this = $(this);
			var vid = $this.val();
			var pid = productid.val();
			productid.empty();
			$.each(idArray[vid]['products'], function(key, value) {
				productid.append($('<option></option>').val(key).html(value));
			});
			productid.val(pid);
		});

		$.each(idArray, function(key, value) {
			vendorid.append($('<option></option>').val(key).html(value['name']));
		});

		vendorid.val(vid);
		vendorid.change();
	});

	function updateAttributes() {
		var attributes = [];

		$.each($('[id^="prop-"]'), function () {
			var attr = {};
			attr['prop'] = $(this).attr('id');
			attr['value'] = $(this).val();

			attributes.push(attr);
		});
		$('#attributes').val(JSON.stringify(attributes));
	}

	function remakeUpdateEvent() {
		$('[id^="prop-"]').change(function () {
			updateAttributes();
		});
	}

	function remakeChangeEvent(val) {
		var vendorid = $('#prop-vid');

		vendorid.change(function () {
			var productid = $('#prop-pid');

			var $this = $(this);
			var vid = $this.val();
			var pid = productid.val();
			productid.empty();
			$.each(idArray[vid]['products'], function(key, value) {
				productid.append($('<option></option>').val(key).html(value));
			});
			productid.val(pid);
		});

		vendorid.val(val);
		vendorid.change();
	}

</script>