require 'active_support/core_ext/module/delegation'
require 'flavour_saver'
require 'hashie/mash'
FS.register_helper :adjust_iframe_height_script do
<<-HTML.strip_heredoc.html_safe
HTML
end
FS.register_helper :download_csv do
<<-HTML.strip_heredoc.html_safe
HTML
end
FS.register_helper :download_json do
json = except(:css_uri).to_json
<<-HTML.strip_heredoc.html_safe
HTML
end
FS.register_helper :download_svg do
<<-HTML.strip_heredoc.html_safe
HTML
end
FS.register_helper :download_image do
<<-HTML.strip_heredoc.html_safe
HTML
end
def init_download_script
<<-HTML.strip_heredoc.html_safe
if (!$("div#stanza_buttons")[0]) {
$('body').append("
");
}
HTML
end
module TogoStanza::Stanza
autoload :ExpressionMap, 'togostanza/stanza/expression_map'
autoload :Grouping, 'togostanza/stanza/grouping'
autoload :Querying, 'togostanza/stanza/querying'
class Context < Hashie::Mash
def respond_to_missing?(*)
# XXX It looks ugly, but we need use not pre-defined properties
true
end
end
class Base
extend ExpressionMap::Macro
include Querying
include Grouping
define_expression_map :properties
define_expression_map :resources
property :css_uri do |css_uri|
if css_uri
css_uri.split(',')
else
%w(
//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/css/bootstrap.min.css
//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css
/stanza/assets/stanza.css
)
end
end
class_attribute :root
def self.id
to_s.underscore.sub(/_stanza$/, '')
end
delegate :id, to: 'self.class'
def initialize(params = {})
@params = params
end
attr_reader :params
def context
Context.new(properties.resolve_all_in_parallel(self, params))
end
def resource(name)
resources.resolve(self, name, params)
end
def render
path = File.join(root, 'template.hbs')
Tilt.new(path).render(context)
end
def metadata(server_url)
path = File.join(root, 'metadata.json')
if File.exist?(path)
orig = JSON.load(open(path))
stanza_uri = "#{server_url}/#{orig['id']}"
usage_attrs = orig['parameter'].map {|hash|
unless hash['key'].start_with?("data-stanza-") then
hash['key'] = "data-stanza-" << hash['key']
end
"#{hash['key']}=\"#{hash['example']}\""
}.push("data-stanza=\"#{stanza_uri}\"").join(' ')
append_prefix_to_hash_keys(orig.merge(usage: ""), 'stanza').merge('@id' => stanza_uri)
else
nil
end
end
private
def append_prefix_to_hash_keys(hash, prefix)
hash.each_with_object({}) do |(key, value), new_hash|
new_hash["#{prefix}:#{key}"] = expand_values(value, prefix)
end
end
def expand_values(value, prefix)
case value
when Hash
append_prefix_to_hash_keys(value, prefix)
when Array
value.map {|v| expand_values(v, prefix) }
else
value
end
end
end
end