Passed
Pull Request — master (#77)
by Daniel
24:04
created

js/admin.js   A

Complexity

Total Complexity 39
Complexity/F 1.39

Size

Lines of Code 295
Function Count 28

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 39
eloc 129
c 0
b 0
f 0
dl 0
loc 295
rs 9.28
mnd 11
bc 11
fnc 28
bpm 0.3928
cpm 1.3928
noi 0
1
/**
2
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
3
 *
4
 * @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>)
5
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
/** global: OC */
24
/** global: OCA */
25
/** global: jQuery */
26
27
(function (document, $, OC, OCA) {
28
	'use strict';
29
30
	/**
31
	 * @class
32
	 * @extends OCA.CMSPico.List
33
	 *
34
	 * @param {jQuery}        $element
35
	 * @param {Object}        [options]
36
	 * @param {string}        [options.route]
37
	 * @param {jQuery|string} [options.template]
38
	 * @param {jQuery|string} [options.systemTemplate]
39
	 * @param {jQuery|string} [options.customTemplate]
40
	 * @param {jQuery|string} [options.newTemplate]
41
	 * @param {jQuery|string} [options.loadingTemplate]
42
	 * @param {jQuery|string} [options.errorTemplate]
43
	 */
44
	OCA.CMSPico.AdminList = function ($element, options) {
45
		this.initialize($element, options);
46
	};
47
48
	/**
49
	 * @lends OCA.CMSPico.AdminList.prototype
50
	 */
51
	OCA.CMSPico.AdminList.prototype = $.extend({}, OCA.CMSPico.List.prototype, {
52
		/** @member {jQuery} */
53
		$systemTemplate: $(),
54
55
		/** @member {jQuery} */
56
		$customTemplate: $(),
57
58
		/** @member {jQuery} */
59
		$newTemplate: $(),
60
61
		/**
62
		 * @constructs
63
		 *
64
		 * @param {jQuery}        $element
65
		 * @param {Object}        [options]
66
		 * @param {string}        [options.route]
67
		 * @param {jQuery|string} [options.template]
68
		 * @param {jQuery|string} [options.systemTemplate]
69
		 * @param {jQuery|string} [options.customTemplate]
70
		 * @param {jQuery|string} [options.newTemplate]
71
		 * @param {jQuery|string} [options.loadingTemplate]
72
		 * @param {jQuery|string} [options.errorTemplate]
73
		 */
74
		initialize: function ($element, options) {
75
			OCA.CMSPico.List.prototype.initialize.apply(this, arguments);
76
77
			options = $.extend({
78
				systemTemplate: $element.data('systemTemplate'),
79
				customTemplate: $element.data('customTemplate'),
80
				newTemplate: $element.data('newTemplate')
81
			}, options);
82
83
			this.$systemTemplate = $(options.systemTemplate);
84
			this.$customTemplate = $(options.customTemplate);
85
			this.$newTemplate = $(options.newTemplate);
86
87
			var signature = 'OCA.CMSPico.AdminList.initialize()';
88
			if (!this.$systemTemplate.length) throw signature + ': No valid system item template given';
89
			if (!this.$customTemplate.length) throw signature + ': No valid custom item template given';
90
			if (!this.$newTemplate.length) throw signature + ': No valid new item template given';
91
		},
92
93
		/**
94
		 * @public
95
		 *
96
		 * @param {Object}            data
97
		 * @param {Object[]|string[]} data.systemItems
98
		 * @param {Object[]|string[]} data.customItems
99
		 * @param {Object[]|string[]} data.newItems
100
		 */
101
		update: function (data) {
102
			var that = this;
103
104
			this._content(this.$template);
105
106
			$.each(data.systemItems, function (_, value) {
107
				var itemData = (typeof value === 'object') ? value : { name: value },
108
					$item = that._content(that.$systemTemplate, itemData);
109
				that._setupItem($item, itemData);
110
			});
111
112
			$.each(data.customItems, function (_, value) {
113
				var itemData = (typeof value === 'object') ? value : { name: value },
114
					$item = that._content(that.$customTemplate, itemData);
115
				that._setupItem($item, itemData);
116
			});
117
118
			$.each(data.newItems, function (_, value) {
119
				var itemData = (typeof value === 'object') ? value : { name: value },
120
					$item = that._content(that.$newTemplate, itemData);
121
				that._setupItem($item, itemData);
122
			});
123
124
			this._setup();
125
		},
126
127
		/**
128
		 * @protected
129
		 */
130
		_setup: function () {
131
			var $newItem = this.$element.find('.action-new-item'),
132
				$newItemButton = this.$element.find('.action-new'),
133
				that = this;
134
135
			if ($newItem.val()) {
136
				$newItemButton.on('click.CMSPicoAdminList', function (event) {
137
					event.preventDefault();
138
					that._api('POST', '', {item: $newItem.val()});
139
				});
140
			} else {
141
				$newItemButton.add($newItem).prop('disabled', true);
142
			}
143
144
			this.$element.find('.action-reload').on('click.CMSPicoAdminList', function (event) {
145
				event.preventDefault();
146
				that.reload();
147
			});
148
		},
149
150
		/**
151
		 * @protected
152
		 *
153
		 * @param {jQuery}  $item
154
		 * @param {Object}  itemData
155
		 * @param {string}  itemData.name
156
		 * @param {boolean} [itemData.compat]
157
		 * @param {string}  [itemData.compatReason]
158
		 * @param {Object}  [itemData.compatReasonData]
159
		 */
160
		_setupItem: function ($item, itemData) {
161
			var that = this;
162
163
			$item.find('.info-compat').each(function () {
164
				var $this = $(this),
165
					$icon = $this.find('[class^="icon-"], [class*=" icon-"]'),
166
					compat = (itemData.compat === undefined) || !!itemData.compat;
167
168
				$this.data('value', compat);
169
170
				$icon
171
					.addClass(compat ? 'icon-checkmark' : 'icon-error-color')
172
					.removeClass(compat ? 'icon-error-color' : 'icon-checkmark');
173
174
				if ($icon.hasClass('has-tooltip')) {
175
					var compatReason = $icon.prop('title') || '';
176
					if (itemData.compatReason) {
177
						var rawCompatReason = OCA.CMSPico.Util.unescape(itemData.compatReason);
178
						compatReason = t('cms_pico', rawCompatReason, itemData.compatReasonData);
179
					}
180
181
					$icon
182
						.prop('title', compatReason)
183
						.tooltip();
184
				}
185
			});
186
187
			$item.find('.action-sync').on('click.CMSPicoAdminList', function (event) {
188
				event.preventDefault();
189
				that._api('POST', itemData.name);
190
			});
191
192
			$item.find('.action-delete').on('click.CMSPicoAdminList', function (event) {
193
				event.preventDefault();
194
				that._api('DELETE', itemData.name);
195
			});
196
		}
197
	});
198
199
	$('.picocms-admin-list').each(function () {
200
		var $this = $(this),
201
			adminList = new OCA.CMSPico.AdminList($this);
202
203
		$this.data('CMSPicoAdminList', adminList);
204
		adminList.reload();
205
	});
206
207
	/**
208
	 * @class
209
	 * @extends OCA.CMSPico.Form
210
	 *
211
	 * @param {jQuery} $element
212
	 * @param {Object} [options]
213
	 * @param {string} [options.route]
214
	 */
215
	OCA.CMSPico.LimitGroupsForm = function ($element, options) {
216
		this.initialize($element, options);
217
	};
218
219
	/**
220
	 * @lends OCA.CMSPico.LimitGroupsForm.prototype
221
	 */
222
	OCA.CMSPico.LimitGroupsForm.prototype = $.extend({}, OCA.CMSPico.Form.prototype, {
223
		/**
224
		 * @public
225
		 */
226
		prepare: function () {
227
			var that = this,
228
				$input = this.$element.find('input');
229
230
			// loading order is crucial - and Nextcloud loads its own JS settings files last... m(
231
			$(function () {
232
				OC.Settings.setupGroupsSelect($input);
233
234
				$input.on('change.CMSPicoLimitGroupsForm', function (event) {
235
					that.submit();
236
				});
237
			});
238
		},
239
240
		/**
241
		 * @public
242
		 */
243
		submit: function () {
244
			var $input = this.$element.find(':input'),
245
				data = this.$element.serialize();
246
247
			$input.prop('disabled', true);
248
249
			$.ajax({
250
				method: 'POST',
251
				url: OC.generateUrl(this.route),
252
				data: data
253
			}).done(function (data, textStatus, jqXHR) {
254
				$input.prop('disabled', false);
255
			});
256
		}
257
	});
258
259
	$('.picocms-limit_groups-form').each(function () {
260
		var $this = $(this),
261
			limitGroupsForm = new OCA.CMSPico.LimitGroupsForm($this);
262
263
		$this.data('CMSPicoLimitGroupsForm', limitGroupsForm);
264
		limitGroupsForm.prepare();
265
	});
266
267
	/**
268
	 * @class
269
	 * @extends OCA.CMSPico.Form
270
	 *
271
	 * @param {jQuery} $element
272
	 * @param {Object} [options]
273
	 * @param {string} [options.route]
274
	 */
275
	OCA.CMSPico.LinkModeForm = function ($element, options) {
276
		this.initialize($element, options);
277
	};
278
279
	/**
280
	 * @lends OCA.CMSPico.LinkModeForm.prototype
281
	 */
282
	OCA.CMSPico.LinkModeForm.prototype = $.extend({}, OCA.CMSPico.Form.prototype, {
283
		/**
284
		 * @public
285
		 */
286
		prepare: function () {
287
			var that = this,
288
				$input = this.$element.find('input[type="radio"]');
289
290
			$input.on('change.CMSPicoLinkModeForm', function (event) {
291
				that.submit();
292
			});
293
		},
294
295
		/**
296
		 * @public
297
		 */
298
		submit: function () {
299
			var $input = this.$element.find(':input'),
300
				data = this.$element.serialize();
301
302
			$input.prop('disabled', true);
303
304
			$.ajax({
305
				method: 'POST',
306
				url: OC.generateUrl(this.route),
307
				data: data
308
			}).done(function (data, textStatus, jqXHR) {
309
				$input.prop('disabled', false);
310
			});
311
		}
312
	});
313
314
	$('.picocms-link_mode-form').each(function () {
315
		var $this = $(this),
316
			linkModeForm = new OCA.CMSPico.LinkModeForm($this);
317
318
		$this.data('CMSPicoLinkModeForm', linkModeForm);
319
		linkModeForm.prepare();
320
	});
321
})(document, jQuery, OC, OCA);
322