1 | // https://gist.github.com/iperelivskiy/4110988#gistcomment-2697447 |
||
2 | // used only to hash strings to get unique IDs for href targets |
||
3 | function funhash (s) { |
||
4 | let h = 0xdeadbeef |
||
5 | for (let i = 0; i < s.length; i++) { |
||
6 | h = Math.imul(h ^ s.charCodeAt(i), 2654435761) |
||
7 | } |
||
8 | return (h ^ h >>> 16) >>> 0 |
||
9 | } |
||
10 | |||
11 | function displayProperties (objectId, objectAttrName, viewMethod, removeMethod) { |
||
12 | const container = $('#properties-accordion') |
||
13 | const propertyTemplate = $('#property-fragment')[0].content |
||
14 | const valueTemplate = $(propertyTemplate).find('template')[0].content |
||
15 | const shownProperties = [] |
||
16 | let property = null |
||
17 | |||
18 | const query = {} |
||
19 | query[objectAttrName] = objectId |
||
20 | |||
21 | jsonRPC(viewMethod, query, data => { |
||
22 | data.forEach(element => { |
||
23 | if (!shownProperties.includes(element.name)) { |
||
24 | property = $(propertyTemplate.cloneNode(true)) |
||
25 | property.find('.js-property-name').html(element.name) |
||
26 | |||
27 | const collapseId = 'collapse' + funhash(element.name) |
||
28 | property.find('.js-property-name').attr('href', `#${collapseId}`) |
||
29 | property.find('.js-panel-collapse').attr('id', collapseId) |
||
30 | property.find('.js-remove-property').attr(`data-${objectAttrName}_id`, element[objectAttrName]) |
||
31 | property.find('.js-remove-property').attr('data-property-name', element.name) |
||
32 | property.find('template').remove() |
||
33 | |||
34 | container.find('.js-insert-here').append(property) |
||
35 | shownProperties.push(element.name) |
||
36 | } |
||
37 | |||
38 | const value = $(valueTemplate.cloneNode(true)) |
||
39 | value.find('.js-property-value').text(element.value) |
||
40 | value.find('.js-remove-value').attr('data-id', element.id) |
||
41 | container.find('.js-panel-body').last().append(value) |
||
42 | }) |
||
43 | |||
44 | $('.js-remove-property').click(function () { |
||
45 | const sender = $(this) |
||
46 | const query = { name: sender.data('property-name') } |
||
47 | query[objectAttrName] = sender.data(`${objectAttrName}_id`) |
||
48 | |||
49 | jsonRPC(removeMethod, query, function (data) { |
||
0 ignored issues
–
show
|
|||
50 | sender.parents('.panel').first().fadeOut(500) |
||
51 | } |
||
52 | ) |
||
53 | return false |
||
54 | }) |
||
55 | |||
56 | $('.js-remove-value').click(function () { |
||
57 | const sender = $(this) |
||
58 | jsonRPC(removeMethod, { pk: sender.data('id') }, function (data) { |
||
0 ignored issues
–
show
|
|||
59 | sender.parent().fadeOut(500) |
||
60 | }) |
||
61 | return false |
||
62 | }) |
||
63 | }) |
||
64 | } |
||
65 | |||
66 | function addPropertyValue (objectId, objectAttrName, viewMethod, addMethod, removeMethod) { |
||
67 | const nameValue = $('#property-value-input').val().split('=') |
||
68 | |||
69 | jsonRPC( |
||
70 | addMethod, |
||
71 | [objectId, nameValue[0], nameValue[1]], |
||
72 | function (data) { |
||
0 ignored issues
–
show
|
|||
73 | animate($('.js-insert-here'), function () { |
||
74 | $('#property-value-input').val('') |
||
75 | $('.js-insert-here').empty() |
||
76 | |||
77 | displayProperties(objectId, objectAttrName, viewMethod, removeMethod) |
||
78 | }) |
||
79 | } |
||
80 | ) |
||
81 | } |
||
82 | |||
83 | // binds everything in this card |
||
84 | function propertiesCard (objectId, objectAttrName, viewMethod, addMethod, removeMethod) { |
||
85 | displayProperties(objectId, objectAttrName, viewMethod, removeMethod) |
||
86 | |||
87 | $('.js-add-property-value').click(function () { |
||
88 | addPropertyValue(objectId, objectAttrName, viewMethod, addMethod, removeMethod) |
||
89 | return false |
||
90 | }) |
||
91 | |||
92 | $('#property-value-input').keyup(function (event) { |
||
93 | if (event.keyCode === 13) { |
||
94 | addPropertyValue(objectId, objectAttrName, viewMethod, addMethod, removeMethod) |
||
95 | } |
||
96 | }) |
||
97 | } |
||
98 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.