class TableBuilder
@render_tbody:(obj) ->
TableBuilder.obj = obj
TableBuilder.html = ""
TableBuilder.make_table()
return TableBuilder.html
@make_table: ->
for row in TableBuilder.obj.data
do (row) ->
TableBuilder.html += '
'
TableBuilder.add_row_functions(row)
TableBuilder.add_row_columns(row)
TableBuilder.html += '
'
@make_row: ->
@add_row_functions:(row) ->
if TableBuilder.obj.row? && TableBuilder.obj.row.functions?
TableBuilder.html += ''
for function_name, settings of TableBuilder.obj.row.functions
TableBuilder.make_row_function_button(settings, row)
TableBuilder.html += ' | '
@add_row_columns:(row) ->
for col in TableBuilder.obj.columns
do (col) ->
TableBuilder.html += ''
if (is_hash(row[col.table + '_' + col.name]))
# hash span or href (styled as button)
button_settings = row[col.table + '_' + col.name]
button_settings = {} if !button_settings?
TableBuilder.make_column_from_hash(button_settings, row, col)
else if (is_array(row[col.table + '_' + col.name]))
# array of hashes (probably buttons)
one_cell_buttons = row[col.table + '_' + col.name]
for one_cell_button in one_cell_buttons
do (one_cell_button) ->
one_cell_button = {} if !one_cell_buttons?
TableBuilder.make_column_from_hash(one_cell_button, row, col)
else if (is_string(row[col.table + '_' + col.name]))
# its just string
text = ""
text = row[col.table + '_' + col.name] if row[col.table + '_' + col.name]?
text = row[col.name] if row[col.name]? && text? && text.length <= 0
sliced_text = text
if (col.max_text_length)
max = col.max_text_length - 3
sliced_text = sliced_text.slice(0, col.max_text_length) + "..." if ( max > 0 && sliced_text.length > max)
TableBuilder.html += '' + sliced_text + ''
else
# its something else eg. number cant be sliced, or its probably aliens Kveigars
text = ""
text = row[col.table + '_' + col.name] if row[col.table + '_' + col.name]?
text = row[col.name] if row[col.name]? && text? && text.length <= 0
console.log[text]
TableBuilder.html += '' + text + ''
TableBuilder.html += ' | '
@make_href_button:(settings, row, col) ->
sliced_text = settings['name']
if col?
if (col.max_text_length)
max = col.max_text_length - 3
sliced_text = sliced_text.slice(0, col.max_text_length) + "..." if ( max > 0 && sliced_text.length > max)
it_is_link = false
it_is_link = true if (settings.url || settings.symlink_controller || settings.symlink_action || settings.symlink_id || settings.symlink_outer_controller || settings.symlink_outer_id)
if it_is_link
stringified_settings = JSON.stringify(settings)
stringified_settings = stringified_settings.replace(/"/g,'"') # this is crutial unless the double quotes will fuck up html
non_ajax_url = build_get_url(settings)
TableBuilder.html += ' 0
if it_is_link
if (settings.confirm)
TableBuilder.html += ' onclick="if (confirm(\'' + settings.confirm + '\')){ load_page(' + stringified_settings + ',this); }; return false;"'
else
TableBuilder.html += ' onclick="load_page(' + stringified_settings + ',this); return false;"'
TableBuilder.html += '>' + sliced_text
if it_is_link
TableBuilder.html +=''
else
TableBuilder.html += ''
@make_row_function_button:(button_settings, row, col) ->
button_settings.symlink_id = row.row_id if row? # only for generic row functions, they are defined without the id
TableBuilder.make_href_button(button_settings, row, col)
@make_column_from_hash:(button_settings, row, col) ->
button_settings['origin'] = 'table'
TableBuilder.make_href_button(button_settings, row, col)
window.TableBuilder = TableBuilder