thingspeak/app/controllers/windows_controller.rb

221 lines
8.5 KiB
Ruby
Raw Normal View History

class WindowsController < ApplicationController
before_filter :require_user, :except => [:index, :html, :iframe]
def hide
window = Window.find(params[:id])
window.show_flag = false
if window.save
render :text => window.id.to_s
else
render :text => '-1'
end
end
# Call WindowsController.display when we want to display a window on the dashboard
# params[:visibility_flag] is whether it is the private or public dashboard
# params[:plugin] is for displaying a plugin, instead of a window
# params[:id] is the window ID for conventional windows, but the plugin_id for plugins
# params[:channel_id] is the channel_id
def display
@visibility = params[:visibility_flag]
window = Window.find(params[:id])
window = Window.new if window.nil?
window.show_flag = true
#Just save this change, then modify the object before rendering the JSON
savedWindow = window.save
config_window window
@mychannel = current_user && current_user.id == window.channel.user_id
if savedWindow
render :json => window.to_json
else
render :json => 'An error occurred'.to_json
end
end
def config_window(window)
2014-07-23 01:13:11 +02:00
if window.window_type == "plugin"
pluginName = Plugin.find(window.content_id).name
window.title = t(window.title, {:name => pluginName})
2014-07-23 01:13:11 +02:00
elsif window.window_type == "chart"
window.title = t(window.title, {:field_number => window.content_id})
options = window.options if !window.nil?
options ||= ""
window.html["::OPTIONS::"] = options unless window.html.nil? || window.html.index("::OPTIONS::").nil?
else
window.title = t(window.title)
end
end
2014-07-23 01:13:11 +02:00
def html
window = Window.find(params[:id])
2014-07-23 01:13:11 +02:00
options = window.options unless window.nil? || window.window_type != "chart"
window.html["::OPTIONS::"] = options unless window.html.nil? || window.html.index("::OPTIONS::").nil?
html = window.html
render :text => html
end
def iframe
window = Window.find(params[:id])
2014-07-23 01:13:11 +02:00
options = window.options unless window.nil? || window.window_type != "chart"
window.html["::OPTIONS::"] = options unless window.html.nil? || window.html.index("::OPTIONS::").nil?
iframe_html = window.html
iframe_html = iframe_html.gsub(/src=\"[\/.]/, 'src="' + api_domain);
render :text => iframe_html
end
def index
channel = Channel.find(params[:channel_id])
2014-07-23 01:13:11 +02:00
#channel.update_status_portlet false if (channel.windows.select { |w| w.window_type == 'status' && w.private_flag == false } )
#channel.update_status_portlet true if (channel.windows.select { |w| w.window_type == 'status' && w.private_flag == true } )
#channel.update_video_portlet false if (channel.windows.select { |w| w.window_type == 'video' && w.private_flag == false } )
#channel.update_video_portlet true if (channel.windows.select { |w| w.window_type == 'video' && w.private_flag == true } )
#channel.update_location_portlet false if (channel.windows.select { |w| w.window_type == 'location' && w.private_flag == false } )
#channel.update_location_portlet true if (channel.windows.select { |w| w.window_type == 'location' && w.private_flag == true } )
#channel.update_chart_portlets if (channel.windows.select { |w| w.window_type == 'chart' } )
windows = channel.public_windows(true).order(:position) unless params[:channel_id].nil?
if channel.recent_statuses.nil? || channel.recent_statuses.size <= 0
2014-07-23 01:13:11 +02:00
@windows = windows.delete_if { |w| w.window_type == "status" }
else
@windows = windows
end
@windows.each do |window|
2014-07-23 01:13:11 +02:00
if window.window_type == "plugin"
pluginName = Plugin.find(window.content_id).name
window.title = t(window.title, {:name => pluginName})
2014-07-23 01:13:11 +02:00
elsif window.window_type == "chart"
window.title = t(window.title, {:field_number => window.content_id})
options = window.options if !window.nil?
options ||= ""
window.html["::OPTIONS::"] = options unless window.html.nil? || window.html.index("::OPTIONS::").nil?
else
window.title = t(window.title)
end
end
respond_to do |format|
format.html
2014-07-23 01:13:11 +02:00
format.json { render :json => @windows.as_json }
end
end
# This is going to display windows that are hidden (show_flag = false)
# The "visibility_flag" param indicates whether it's public or private visibility
def hidden_windows
@visibility = params[:visibility_flag]
channel = Channel.find(params[:channel_id])
if @visibility == "private"
@windows = channel.private_windows(false) unless channel.nil?
else
@windows = channel.public_windows(false) unless channel.nil?
end
2014-07-23 01:13:11 +02:00
@windows.reject! { |window| window.window_type == "plugin" }
@windows.each do |window|
2014-07-23 01:13:11 +02:00
if window.window_type == "plugin"
elsif window.window_type == "chart"
window.title = t(window.title, {:field_number => window.content_id})
options = window.options unless window.nil?
options ||= ""
window.html["::OPTIONS::"] = options unless window.html.nil? || window.html.index("::OPTIONS::").nil?
else
window.title = t(window.title)
end
end
respond_to do |format|
format.html { render :partial => "hidden_windows" }
2014-07-23 01:13:11 +02:00
format.json { render :json => @windows.as_json }
end
end
def private_windows
channel = Channel.find(params[:channel_id])
2014-07-23 01:13:11 +02:00
#channel.update_status_portlet false if (channel.windows.select { |w| w.window_type == 'status' && w.private_flag == false } )
#channel.update_status_portlet true if (channel.windows.select { |w| w.window_type == 'status' && w.private_flag == true } )
#channel.update_video_portlet false if (channel.windows.select { |w| w.window_type == 'video' && w.private_flag == false } )
#channel.update_video_portlet true if (channel.windows.select { |w| w.window_type == 'video' && w.private_flag == true } )
#channel.update_location_portlet false if (channel.windows.select { |w| w.window_type == 'location' && w.private_flag == false } )
#channel.update_location_portlet true if (channel.windows.select { |w| w.window_type == 'location' && w.private_flag == true } )
#channel.update_chart_portlets if (channel.windows.select { |w| w.window_type == 'chart' } )
windows = channel.private_windows(true).order(:position) unless params[:channel_id].nil?
if channel.recent_statuses.nil? || channel.recent_statuses.size <= 0
2014-07-23 01:13:11 +02:00
@windows = windows.delete_if { |w| w.window_type == "status" }
else
@windows = windows
end
@windows.each do |window|
2014-07-23 01:13:11 +02:00
if window.window_type == "plugin"
pluginName = Plugin.find(window.content_id).name
window.title = t(window.title, {:name => pluginName})
2014-07-23 01:13:11 +02:00
elsif window.window_type == "chart"
window.title = t(window.title, {:field_number => window.content_id})
options = window.options unless window.nil?
options ||= ""
window.html["::OPTIONS::"] = options unless window.html.nil? || window.html.index("::OPTIONS::").nil?
else
window.title = t(window.title)
end
end
respond_to do |format|
format.html
2014-07-23 01:13:11 +02:00
format.json { render :json => @windows.as_json }
end
end
def update
logger.info "We're trying to update the windows with " + params.to_s
#params for this put are going to look like
# page"=>"{\"col\":0,\"positions\":[1,2,3]}"
#So.. the position values are Windows.id They should get updated with the ordinal value based
# on their array position and the column should get updated according to col value.
# Since the windows are order by position, when a window record changes from
# col1,position0 -> col0,position0 the entire new column is reordered.
# The old column is missing a position, but the remaining are just left to their order
# (ie., 0,1,2 become 1,2) Unless they are also changed
# First parse the JSON in params["page"] ...
values = JSON(params[:page])
# .. then find each window and update with new ordinal position and col.
logger.info "Channel id = " + params[:channel_id].to_s
@channel = current_user.channels.find(params[:channel_id])
col = values["col"]
saved = true
values["positions"].each_with_index do |p,i|
windows = @channel.windows.where({:id => p}) unless p.nil?
unless windows.nil? || windows.empty?
w = windows[0]
w.position = i
w.col = col
if !w.save
saved = false
end
end
end
if saved
render :text => '0'
else
render :text => '-1'
end
end
end