Skip to content

Commit

Permalink
Merge pull request #2260 from isenseDev/revert-2233-editFields
Browse files Browse the repository at this point in the history
Revert "Re-order Fields"
  • Loading branch information
bdonald25 committed Sep 25, 2015
2 parents 77dafe9 + 19fcc83 commit 952cd48
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 129 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
//= require jquery.caret.js
//= require jquery.form.js
//= require jquery.qrcode.min.js
//= require jquery-ui/sortable
//= require titlebar.js.coffee
//= require bootstrap
//= require jstz/jstz-1.0.4.min.js
Expand Down
60 changes: 13 additions & 47 deletions app/assets/javascripts/projects/edit_fields.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ $ ->
$('#number').click ->
numCount = numCount + 1
displayNumCount = displayNumCount + 1
addRow(["""<i class="sort-hamburger glyphicon glyphicon-menu-hamburger"></i>""",
"""<input class="input-small form-control" type="text"
addRow(["""<input class="input-small form-control" type="text"
name="number_#{numCount}" value="Number_#{displayNumCount}">""", "Number",
"""<input class="input-small form-control" type="text" class="units"
name="units_#{numCount}">""", "", """<a href="#" fid="0"
Expand All @@ -38,44 +37,37 @@ $ ->
$('#text').click ->
textCount = textCount + 1
displayTextCount = displayTextCount + 1
addRow(["""<i class="sort-hamburger glyphicon glyphicon-menu-hamburger"></i>""",
"""<input class="input-small form-control" type="text"
addRow(["""<input class="input-small form-control" type="text"
name="text_#{textCount}" value="Text_#{displayTextCount}">""", "Text", "",
"""<input class="input-small form-control" type="text" class="restrictions"
"""<input class="input-small form-control" type="text" class="restrictions"
name="restrictions_#{textCount}">""", """<a href="#" fid="0"
class="field_delete"><i class="fa fa-close slick-delete"></i></a>"""])

$('#timestamp').click ->
timestampCount = timestampCount + 1
addRow(["""<i class="sort-hamburger glyphicon glyphicon-menu-hamburger"></i>""",
"""<input class="input-small form-control" type="text" name="timestamp"
addRow(["""<input class="input-small form-control" type="text" name="timestamp"
value="Timestamp">""", "Timestamp", "", "", """<a href="#" fid="0"
class="field_delete"><i class="fa fa-close slick-delete"></i></a>"""])
document.getElementById('timestamp').disabled = true

$('#location').click ->
locationCount = locationCount + 1
addRow(["""<i class="sort-hamburger glyphicon glyphicon-menu-hamburger"></i>""",
"""<input class="input-small form-control" type="text" name="longitude"
addRow(["""<input class="input-small form-control" type="text" name="longitude"
value="Longitude">""", "Longitude", "deg", "", """<a href="#" fid="0"
class="field_delete"><i class="fa fa-close slick-delete"></i></a>"""])
addRow(["""<i class="sort-hamburger glyphicon glyphicon-menu-hamburger"></i>""",
"""<input class="input-small form-control" type="text" name="latitude"
addRow(["""<input class="input-small form-control" type="text" name="latitude"
value="Latitude">""", "Latitude", "deg", "", """<a href="#" fid="0"
class="field_delete"><i class="fa fa-close slick-delete"></i></a>"""])
document.getElementById('location').disabled = true

# Make table sortable
$( "tbody" ).sortable();

# Delete field, enable timestamp/location buttons (NOTE: fid is 0 when the field
# hasn't yet been added to project in database)
$('#fields_table').on 'click', '.field_delete', ->
# fid of row being deleted
fid = $(@).closest('a').attr('fid')

# Row index of row being deleted
rowIndex = $(@).closest('tr').index() + 1
rowIndex = $(@).closest('tr').index()

# Row name of row being deleted
rowName = $(@).closest('tr').attr('name')
Expand Down Expand Up @@ -110,32 +102,15 @@ $ ->
for i in [0...4]
setValue(inputBoxes[i], values[i])

# add hidden input for each field with it's position
t = document.getElementById('fields_table')
for i in [1...t.rows.length]
field_id = t.rows[i].cells[5].getElementsByTagName('a')[0].getAttribute('fid')
# This is for new fields that do not have an id yet
if field_id == '0'
field_id = t.rows[1].cells[1].getElementsByTagName('input')[0].getAttribute('name')
input = document.createElement('input')
input.setAttribute('type', 'text')
input.setAttribute('text', 'form-control')
input.setAttribute('style', 'visibility:collapse;')
input.setAttribute('name', "#{field_id}_index")
input.setAttribute('id', "#{field_id}_index")
input.setAttribute('value', i - 1)
document.getElementById('hidden_index_inputs').appendChild(input)


# Adds row to table, highlight new row
addRow = (content) ->
row = document.getElementById('sortable').insertRow(0)
$(row).attr('name', content[2].toLowerCase())
row = document.getElementById('fields_table').insertRow(1)
$(row).attr('name', content[1].toLowerCase())

cells = for i in [1...7]
cells = for i in [1...6]
row.insertCell(i - 1)

for i in [0...6]
for i in [0...5]
cells[i].innerHTML = content[i]

$(row).highlight(500)
Expand All @@ -144,21 +119,12 @@ addRow = (content) ->
callDeleteRow = (rowIndex, rowName, fid) ->
if rowName == 'timestamp'
deleteRow(rowIndex, true, 'timestamp')

else if rowName == 'latitude'
t = document.getElementById('fields_table')
deleteRow(rowIndex, true, 'location')
for i in [1...t.rows.length]
if t.rows[i].cells[2].innerHTML == 'Longitude'
deleteRow(i, true, 'location')

deleteRow(rowIndex, true, 'location')
else if rowName == 'longitude'
t = document.getElementById('fields_table')
deleteRow(rowIndex, true, 'location')
for i in [1...t.rows.length]
if t.rows[i].cells[2].innerHTML == 'Latitude'
deleteRow(i, true, 'location')

deleteRow(rowIndex - 1, true, 'location')
else
deleteRow(rowIndex, false, '')

Expand Down
8 changes: 1 addition & 7 deletions app/assets/stylesheets/projects.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,4 @@
.wide-input {
width: 100% !important;
}

.sort-hamburger {
padding-top:8px;
padding-left: 12px;
}

}
}
2 changes: 1 addition & 1 deletion app/controllers/fields_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ def destroy
private

def field_params
params[:field].permit(:project_id, :field_type, :name, :unit, :restrictions, :index)
params[:field].permit(:project_id, :field_type, :name, :unit, :restrictions)
end
end
23 changes: 8 additions & 15 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ def save_fields

unless field.update_attributes(name: params["#{field.id}_name"],
unit: params["#{field.id}_unit"],
index: params["#{field.id}_index"],
restrictions: restrictions)
respond_to do |format|
flash[:error] = 'Field names must be unique.'
Expand All @@ -279,47 +278,43 @@ def save_fields

# Add fields based on type
if params[:hidden_location_count] == '1'
if addField('Latitude', 'Latitude', 'deg', [], params['latitude_index']) == -1 and return
if addField('Latitude', 'Latitude', 'deg', []) == -1 and return
end
if addField('Longitude', 'Longitude', 'deg', [], params['longitude_index']) == -1 and return
if addField('Longitude', 'Longitude', 'deg', []) == -1 and return
end
end

if params[:hidden_timestamp_count] == '1'
if addField('Timestamp', 'Timestamp', '', [], params['timestamp_index']) == -1 and return
if addField('Timestamp', 'Timestamp', '', []) == -1 and return
end
end

(params[:hidden_num_count].to_i).times do |i|
if addField('Number', params[('number_' + (i + 1).to_s).to_sym], params[('units_' + (i + 1).to_s).to_sym], [], params['number_' + i.to_s + '_index']) == -1 and return
if addField('Number', params[('number_' + (i + 1).to_s).to_sym], params[('units_' + (i + 1).to_s).to_sym], []) == -1 and return
end
end

(params[:hidden_text_count].to_i).times do |i|
# Need to explicitly check if restrictions are nil because empty restrictions should be []
restrictions = params[('restrictions_' + (i + 1).to_s).to_sym].nil? ? [] : params[('restrictions_' + (i + 1).to_s).to_sym].split(',')

if addField('Text', params[('text_' + (i + 1).to_s).to_sym], '', restrictions, params['text_' + i.to_s + '_index']) == -1 and return
if addField('Text', params[('text_' + (i + 1).to_s).to_sym], '', restrictions) == -1 and return
end
end

redirect_to "/projects/#{@project.id}", notice: 'Fields were successfully updated.' and return
end

# Helper function to add field to database
def addField(fieldType, fieldName, unit, restrictions, index)
def addField(fieldType, fieldName, unit, restrictions)
if fieldName.nil?
return
else
if index.nil?
index = @project.fields.size
end
field = Field.new(project_id: @project.id,
field_type: get_field_type(fieldType),
name: fieldName,
unit: unit,
restrictions: restrictions,
index: index)
restrictions: restrictions)
end

unless field.save
Expand Down Expand Up @@ -359,9 +354,7 @@ def finishTemplateUpload
@project = Project.find(params[:id])
@matches.each do |header|
field = Field.new(project_id: @project.id,
field_type: header[1].to_i,
name: header[0],
index: @project.fields.size)
field_type: header[1].to_i, name: header[0])

unless field.save
respond_to do |format|
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/visualizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def displayVis
data_fields.push(typeID: TEXT_TYPE, unitName: 'String', fieldID: -1, fieldName: 'Combined Data Sets')

# push real fields to temp variable
@project.fields.sort_by(&:index).each do |field|
@project.fields.each do |field|
data_fields.push(typeID: field.field_type, unitName: field.unit, fieldID: field.id, fieldName: field.name)
end

Expand Down
3 changes: 2 additions & 1 deletion app/helpers/data_sets_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ def data_set_edit_helper(field, can_edit = false, make_link = true, url = '#')
end

def format_slickgrid(fields, data_set)
cols, data = [fields.sort_by(&:index), data_set]
cols = fields
data = data_set
cols, data = format_slickgrid_merge cols, data
cols, data = format_slickgrid_units cols, data
cols, data = format_slickgrid_populate cols, data
Expand Down
2 changes: 1 addition & 1 deletion app/views/projects/_field_edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% @project.fields.sort_by(&:index).each do |field| %>
<% @project.fields.each do |field| %>
<tr class="<%= cycle('feed-even', 'feed-odd') %> fields" >
<td style="max-width:95px" class="field_name"
data-field_id="<%= field.id %>">
Expand Down
2 changes: 1 addition & 1 deletion app/views/projects/_print.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<table class="table table-striped table-bordered" id="entry">
<thead>
<tr>
<% @project.fields.sort_by(&:index).each do |field|%>
<% @project.fields.each do |field|%>
<td class="center"><%=field.name%></td>
<% end %>
</tr>
Expand Down
66 changes: 29 additions & 37 deletions app/views/projects/edit_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,40 @@
<% end %>
</div>
</div>

<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<%= form_tag "/projects/#{@project.id}/save_fields", name: "fields_form" do %>
<table class="fields_table table table-bordered" id="fields_table" name="fields_table">
<thead>
<tr style="background: #DDD;">
<th></th>
<th>Name</th>
<th>Type</th>
<th class="units">Units</th>
<th class="restrictions">Restrictions</th>
<th></th>
<tr style="background: #DDD;">
<th>Name</th>
<th>Type</th>
<th class="units">Units</th>
<th class="restrictions">Restrictions</th>
<th></th>
</tr>
<% @project.fields.order(:created_at).each do |field| %>
<tr name="<%= get_field_name(field.field_type).downcase %>">
<td><input class="form-control" type="text" name="<%= field.id %>_name" value="<%= (defined?(@tmp_save) && @tmp_save.has_key?("#{field.id}_name")) ? @tmp_save["#{field.id}_name"] : field.name %>"></td>
<td><%= get_field_name(field.field_type) %></td>
<td class="units">
<% if (field.field_type == get_field_type("Number")) %>
<input class="input-small form-control" type="text" name="<%= field.id %>_unit" value="<%= field.unit %>">
<% elsif field.field_type == get_field_type("Latitude") || field.field_type == get_field_type("Longitude") %>
<%= field.unit %>
<% end %>
</td>
<td class="restrictions">
<% if field.field_type == get_field_type("Text") %>
<input class="input-small form-control" type="text" name="<%= field.id %>_restrictions" value="<%= field.restrictions.nil? ? "" : field.restrictions.join(',') %>">
<% end %>
</td>
<td>
<a class="field_delete <%= @project.data_sets.count > 0 ? "not-active" : "" %>" href="#" fid="<%= field.id %>">
<i class="fa fa-close slick-delete"></i>
</a>
</td>
</tr>
</thead>
<tbody id="sortable">
<% (@project.fields.sort_by &:index).each do |field| %>
<tr class="ui-state-default" name="<%= get_field_name(field.field_type).downcase %>">
<td><i class="sort-hamburger glyphicon glyphicon-menu-hamburger"></i></td>
<td><input class="form-control" type="text" name="<%= field.id %>_name" value="<%= (defined?(@tmp_save) && @tmp_save.has_key?("#{field.id}_name")) ? @tmp_save["#{field.id}_name"] : field.name %>"></td>
<td><%= get_field_name(field.field_type) %></td>
<td class="units">
<% if (field.field_type == get_field_type("Number")) %>
<input class="input-small form-control" type="text" name="<%= field.id %>_unit" value="<%= field.unit %>">
<% elsif field.field_type == get_field_type("Latitude") || field.field_type == get_field_type("Longitude") %>
<%= field.unit %>
<% end %>
</td>
<td class="restrictions">
<% if field.field_type == get_field_type("Text") %>
<input class="input-small form-control" type="text" name="<%= field.id %>_restrictions" value="<%= field.restrictions.nil? ? "" : field.restrictions.join(',') %>">
<% end %>
</td>
<td>
<a class="field_delete <%= @project.data_sets.count > 0 ? "not-active" : "" %>" href="#" fid="<%= field.id %>">
<i class="fa fa-close slick-delete"></i>
</a>
</td>
</tr>
<% end %>
<div id='hidden_index_inputs' style='display:none' ></div>
</tbody>
<% end %>
</table>
</div>
</div>
Expand Down
14 changes: 0 additions & 14 deletions db/migrate/20150805145056_add_index_to_fields.rb

This file was deleted.

Loading

0 comments on commit 952cd48

Please sign in to comment.