sortablelabel-js 1.3 → 1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f370c6a7e8cd062bacb56116186ad205d0850e7
4
- data.tar.gz: c8fd7f3a6d6b2be042f51225d6dd4ccba57a3d48
3
+ metadata.gz: 133834335558558247a311078d3b4b37fefa539b
4
+ data.tar.gz: dc23c8b48047fcefb1cd6e1e4dae9e314221e752
5
5
  SHA512:
6
- metadata.gz: 491aa937af69e1268b1e5b68aab8a6637ecc4f5070e0e6a64c92db4ec1451a22c36c3e502261ad5a32490f8e3c6ff74ad74189c7f63a0a88ee87866160f7cdc6
7
- data.tar.gz: 2f6d18d4c164cdfa675f49f48f617323e48e1fb2387a5e9bb85d2f74b1b81f538f724ed4e54be6f64f8ced0bb16fd5e33c3f9e2e4c56d53fd9619beefdfbf2a2
6
+ metadata.gz: 6115e5e420c318138ec88640596e3944e954cb4c0e7e6f9d179069b0695cb9a3fc8f93835b70d486df775822ce7d8963ca0ac79fa90d423235cc028bc7ad7fa6
7
+ data.tar.gz: 70a4c4f4fd9360c0dd173efe9616be2a3601ebb0de8041437b6357c50a543d6e024d1488a7c28264c75ef9b363abc20e0911d7e0b0f85bfaca55bd32eecc35dd
@@ -1,5 +1,5 @@
1
1
  module Sortablelabel
2
2
  module Js
3
- VERSION = "1.3"
3
+ VERSION = "1.4"
4
4
  end
5
5
  end
@@ -39,7 +39,7 @@ class @SortableLabel
39
39
  items: @options['sortableItem']
40
40
  placeholder: "ui-state-highlight"
41
41
  start: (event, ui)->
42
- $(this).find('.ui-state-highlight').css('height', $(ui.item).css('height'))
42
+ $(this).find('.ui-state-highlight').css('height', $(ui.item).css('height'))
43
43
  stop: =>
44
44
  @calStepLable()
45
45
  if typeof(@stop_callback) == 'function'
@@ -57,10 +57,13 @@ class @SortableLabel
57
57
  @target.each((i, sub_target) ->
58
58
  all_fields = $(sub_target).find(_option['sortableItem'])
59
59
  _positionTarget = _option['positionTarget']
60
- all_fields_info = []
60
+ all_remain_fields_info = []
61
+ all_removed_fields_info = []
61
62
  maxium_position = 1
62
63
 
63
64
  if all_fields.length > 0
65
+
66
+ # get the maxinum_position
64
67
  all_fields.each((index, field_item) ->
65
68
  _position = $(field_item).find(_positionTarget).val()
66
69
  if _position && _position != '' && typeof _position != 'undefined'
@@ -68,23 +71,65 @@ class @SortableLabel
68
71
  if _position >= maxium_position
69
72
  maxium_position = _position
70
73
  )
74
+
75
+ # in case that new added field position value is initialized as null or '' or undefined
71
76
  all_fields.each((index, field_item) ->
72
- _position = parseInt($(field_item).find(_positionTarget).val(), 10)
73
- if typeof(_position) == 'undefined'
74
- _position = maxium_position
77
+ _position = $(field_item).find(_positionTarget).val()
78
+ if !_position || _position == '' || typeof _position != 'undefined'
75
79
  maxium_position += 1
76
- all_fields_info.push({
77
- position: _position,
78
- j_element: field_item
79
- })
80
+ $(field_item).find(_positionTarget).val(maxium_position)
81
+ )
82
+
83
+ # filter removed fields and remain fields
84
+ all_fields.each((index, field_item) ->
85
+ _position = $(field_item).find(_positionTarget).val()
86
+ _is_removed = $(field_item).find(_option['removeField']).val()
87
+ if _is_removed == "false" || _is_removed == false
88
+ _is_removed = "false"
89
+ else
90
+ _is_removed = "true"
91
+
92
+ # removed fields should be filtered
93
+ # only unremoved fields will be sorted
94
+ if _is_removed == 'true'
95
+ all_removed_fields_info.push({
96
+ position: _position,
97
+ j_element: field_item
98
+ })
99
+ else
100
+ all_remain_fields_info.push({
101
+ position: _position,
102
+ j_element: field_item
103
+ })
80
104
  )
81
105
 
82
- if all_fields_info.length > 0
83
- all_fields_info.sort((a, b) ->
106
+ # 1. go through sorted array to check if a field is of same position as previous fields
107
+ # 2. if so, update the position of later one as max position + 1
108
+ if all_remain_fields_info.length > 0
109
+
110
+ _tmp_remain_fields_info = all_remain_fields_info.slice()
111
+ for field, key in _tmp_remain_fields_info
112
+ current_position = field.position
113
+ for previous_field, previous_key in _tmp_remain_fields_info
114
+ tmp_position = previous_field.position
115
+ if tmp_position == current_position && previous_key < key
116
+ maxium_position += 1
117
+ all_remain_fields_info[key].position = maxium_position
118
+
119
+ all_remain_fields_info.sort((a, b) ->
84
120
  return a.position - b.position
85
121
  )
122
+
123
+ # set remain fields position value as the acutal position in html page
124
+ for field, key in all_remain_fields_info
125
+ all_remain_fields_info[key].position = key + 1
126
+
86
127
  _target = $(sub_target)
87
- all_fields_info.forEach((item, index) ->
128
+ all_remain_fields_info.forEach((item, index) ->
129
+ _target.append(item.j_element)
130
+ )
131
+
132
+ all_removed_fields_info.forEach((item, index) ->
88
133
  _target.append(item.j_element)
89
134
  )
90
135
  )
@@ -111,8 +156,8 @@ class @SortableLabel
111
156
  $(this).closest('.fields').find('.remove_nested_fields').show();
112
157
  $(this).closest('.fields').find(_this.options['positionTarget']).val(stepCount)
113
158
  if typeof(_this.options['label']) == 'string'
114
- if _this.options['label'].trim() == 'Day'
115
- $(this).closest('.fields').find(_this.options['labelTarget']).html(_this.dayOfWeek(stepCount - 1))
159
+ if _this.options['label'].trim() == 'Day'
160
+ $(this).closest('.fields').find(_this.options['labelTarget']).html(_this.dayOfWeek(stepCount - 1))
116
161
  else
117
162
  if _this.options['fieldName']
118
163
  if $(this).parent().find('.remove_nested_fields').data('association') == _this.options['fieldName']
@@ -122,7 +167,7 @@ class @SortableLabel
122
167
  else if typeof(_this.options['label']) == 'function'
123
168
  $(this).closest('.fields').find(_this.options['labelTarget']).html(_this.options['label'].call($(this).closest('.fields').find(_this.options['labelTarget']), stepCount))
124
169
  stepCount += 1
125
- )
170
+ )
126
171
  )
127
172
  if typeof(@stop_callback) == 'function'
128
173
  @stop_callback();
@@ -173,4 +218,4 @@ class @SortableLabel
173
218
  (($) ->
174
219
  $.fn.sortableLabel = (options) ->
175
220
  new SortableLabel(this, options)
176
- )(jQuery)
221
+ )(jQuery)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sortablelabel-js
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.3'
4
+ version: '1.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-20 00:00:00.000000000 Z
11
+ date: 2018-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler