aboutsummaryrefslogtreecommitdiffstats
path: root/demos/autocomplete/custom-data.html
blob: 149d7cbc50b76c7b4e1e5181d6c9a63d478d80bb (plain)
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
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>jQuery UI Autocomplete - Custom data and display</title>
	<link rel="stylesheet" href="../../themes/base/all.css">
	<link rel="stylesheet" href="../demos.css">
	<style>
	#project-label {
		display: block;
		font-weight: bold;
		margin-bottom: 1em;
	}
	#project-icon {
		float: left;
		height: 32px;
		width: 32px;
	}
	#project-description {
		margin: 0;
		padding: 0;
	}
	</style>
	<script src="../../external/requirejs/require.js"></script>
	<script src="../bootstrap.js">
		var projects = [
			{
				value: "jquery",
				label: "jQuery",
				desc: "the write less, do more, JavaScript library",
				icon: "jquery_32x32.png"
			},
			{
				value: "jquery-ui",
				label: "jQuery UI",
				desc: "the official user interface library for jQuery",
				icon: "jqueryui_32x32.png"
			},
			{
				value: "sizzlejs",
				label: "Sizzle JS",
				desc: "a pure-JavaScript CSS selector engine",
				icon: "sizzlejs_32x32.png"
			}
		];

		$( "#project" ).autocomplete({
			minLength: 0,
			source: projects,
			focus: function( event, ui ) {
				$( "#project" ).val( ui.item.label );
				return false;
			},
			select: function( event, ui ) {
				$( "#project" ).val( ui.item.label );
				$( "#project-id" ).val( ui.item.value );
				$( "#project-description" ).html( ui.item.desc );
				$( "#project-icon" ).attr( "src", "images/" + ui.item.icon );

				return false;
			}
		})
		.autocomplete( "instance" )._renderItem = function( ul, item ) {
			return $( "<li>" )
				.append( "<div>" + item.label + "<br>" + item.desc + "</div>" )
				.appendTo( ul );
		};
	</script>
</head>
<body>

<div id="project-label">Select a project (type "j" for a start):</div>
<img id="project-icon" src="images/transparent_1x1.png" class="ui-state-default" alt="">
<input id="project">
<input type="hidden" id="project-id">
<p id="project-description"></p>

<div class="demo-description">
<p>You can use your own custom data formats and displays by simply overriding the default focus and select actions.</p>
<p>Try typing "j" to get a list of projects or just press the down arrow.</p>
</div>
</body>
</html>
015/stable29'>backport/48015/stable29 Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_versions/tests/js/versionstabviewSpec.js
blob: 40fd887964502fd43eb2e904be0eaaf467b42b58 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (c) 2015
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */
describe('OCA.Versions.VersionsTabView', function() {
	var VersionCollection = OCA.Versions.VersionCollection;
	var VersionModel = OCA.Versions.VersionModel;
	var VersionsTabView = OCA.Versions.VersionsTabView;

	var fetchStub, fileInfoModel, tabView, testVersions, clock;

	beforeEach(function() {
		clock = sinon.useFakeTimers(Date.UTC(2015, 6, 17, 1, 2, 0, 3));
		var time1 = Date.UTC(2015, 6, 17, 1, 2, 0, 3) / 1000;
		var time2 = Date.UTC(2015, 6, 15, 1, 2, 0, 3) / 1000;

		var version1 = new VersionModel({
			id: time1,
			timestamp: time1,
			name: 'some file.txt',
			size: 140,
			fullPath: '/subdir/some file.txt',
			mimetype: 'text/plain'
		});
		var version2 = new VersionModel({
			id: time2,
			timestamp: time2,
			name: 'some file.txt',
			size: 150,
			fullPath: '/subdir/some file.txt',
			mimetype: 'text/plain'
		});

		testVersions = [version1, version2];

		fetchStub = sinon.stub(VersionCollection.prototype, 'fetch');
		fileInfoModel = new OCA.Files.FileInfoModel({
			id: 123,
			name: 'test.txt',
			permissions: OC.PERMISSION_READ | OC.PERMISSION_UPDATE
		});
		tabView = new VersionsTabView();
		tabView.render();
	});

	afterEach(function() {
		fetchStub.restore();
		tabView.remove();
		clock.restore();
	});

	describe('rendering', function() {
		it('reloads matching versions when setting file info model', function() {
			tabView.setFileInfo(fileInfoModel);
			expect(fetchStub.calledOnce).toEqual(true);
		});

		it('renders loading icon while fetching versions', function() {
			tabView.setFileInfo(fileInfoModel);
			tabView.collection.trigger('request');

			expect(tabView.$el.find('.loading').length).toEqual(1);
			expect(tabView.$el.find('.versions li').length).toEqual(0);
		});

		it('renders versions', function() {

			tabView.setFileInfo(fileInfoModel);
			tabView.collection.set(testVersions);

			var version1 = testVersions[0];
			var version2 = testVersions[1];
			var $versions = tabView.$el.find('.versions>li');
			expect($versions.length).toEqual(2);
			var $item = $versions.eq(0);
			expect($item.find('.downloadVersion').attr('href')).toEqual(version1.getDownloadUrl());
			expect($item.find('.versiondate').text()).toEqual('seconds ago');
			expect($item.find('.size').text()).toEqual('< 1 KB');
			expect($item.find('.revertVersion').length).toEqual(1);

			$item = $versions.eq(1);
			expect($item.find('.downloadVersion').attr('href')).toEqual(version2.getDownloadUrl());
			expect($item.find('.versiondate').text()).toEqual('2 days ago');
			expect($item.find('.size').text()).toEqual('< 1 KB');
			expect($item.find('.revertVersion').length).toEqual(1);
		});

		it('does not render revert button when no update permissions', function() {

			fileInfoModel.set('permissions', OC.PERMISSION_READ);
			tabView.setFileInfo(fileInfoModel);
			tabView.collection.set(testVersions);

			var version1 = testVersions[0];
			var version2 = testVersions[1];
			var $versions = tabView.$el.find('.versions>li');
			expect($versions.length).toEqual(2);
			var $item = $versions.eq(0);
			expect($item.find('.downloadVersion').attr('href')).toEqual(version1.getDownloadUrl());
			expect($item.find('.versiondate').text()).toEqual('seconds ago');
			expect($item.find('.revertVersion').length).toEqual(0);

			$item = $versions.eq(1);
			expect($item.find('.downloadVersion').attr('href')).toEqual(version2.getDownloadUrl());
			expect($item.find('.versiondate').text()).toEqual('2 days ago');
			expect($item.find('.revertVersion').length).toEqual(0);
		});
	});

	describe('Reverting', function() {
		var revertStub;

		beforeEach(function() {
			revertStub = sinon.stub(VersionModel.prototype, 'revert');
			tabView.setFileInfo(fileInfoModel);
			tabView.collection.set(testVersions);
		});
		
		afterEach(function() {
			revertStub.restore();
		});

		it('tells the model to revert when clicking "Revert"', function() {
			tabView.$el.find('.revertVersion').eq(1).click();

			expect(revertStub.calledOnce).toEqual(true);
		});
		it('triggers busy state during revert', function() {
			var busyStub = sinon.stub();
			fileInfoModel.on('busy', busyStub);

			tabView.$el.find('.revertVersion').eq(1).click();

			expect(busyStub.calledOnce).toEqual(true);
			expect(busyStub.calledWith(fileInfoModel, true)).toEqual(true);

			busyStub.reset();
			revertStub.getCall(0).args[0].success();

			expect(busyStub.calledOnce).toEqual(true);
			expect(busyStub.calledWith(fileInfoModel, false)).toEqual(true);
		});
		it('updates the file info model with the information from the reverted revision', function() {
			var changeStub = sinon.stub();
			fileInfoModel.on('change', changeStub);

			tabView.$el.find('.revertVersion').eq(1).click();

			expect(changeStub.notCalled).toEqual(true);

			revertStub.getCall(0).args[0].success();

			expect(changeStub.calledOnce).toEqual(true);
			var changes = changeStub.getCall(0).args[0].changed;
			expect(changes.size).toEqual(150);
			expect(changes.mtime).toEqual(testVersions[1].get('timestamp') * 1000);
			expect(changes.etag).toBeDefined();
		});
		it('shows notification on revert error', function() {
			var notificationStub = sinon.stub(OC.Notification, 'show');

			tabView.$el.find('.revertVersion').eq(1).click();

			revertStub.getCall(0).args[0].error();

			expect(notificationStub.calledOnce).toEqual(true);

			notificationStub.restore();
		});
	});
});