Structure link with json

This commit is contained in:
chris 2013-10-23 18:28:09 +08:00
parent a4d1c68299
commit 8ebfac2b99
13 changed files with 124 additions and 136 deletions

View File

@ -220,36 +220,25 @@ function pageSetting(id, edit) {
};
function linkSetting(id, edit) {
$linkKey = $('#pageslide .link-key');
$linkUrl = $('#pageslide .link-url');
$linkTitle = $('#pageslide .link-title'),
$linkPublishedTrue = $('#link_is_published_true');
_status = edit;
if(_status) {
$.ajax({
url: 'link-setting.json',
type: 'POST',
url: "<%= Rails.application.routes.url_helpers.get_link_setting_json_admin_links_path %>",
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: '{"id": ' + id + '}',
data: {id: id},
cache: false,
})
.done(function(data) {
_linkData = data;
$linkKey.val(data.key);
$.each(data.url, function(index, val) {
$linkUrl.eq(index).val(val)
});
$.each(data.title, function(index, val) {
$linkTitle.eq(index).val(val)
});
data.public ? $linkPublishedTrue.prop('checked', true) : $linkPublishedTrue.prop('checked', false);
$linkPublishedTrue.prop('checked') ? $('.link-options').slideDown(300) : $('.link-options').slideUp(300);
$.each(data.link, function(index, val) {
if(val[0]) {
$('.active-link').eq(index).find('input[type="checkbox"]').prop('checked', true).end().find('.active-mune').slideDown(300);
val[1] == 1 ? $('.active-link').eq(index).find('input[type="radio"]').eq(val[1]).prop('checked', true) : $('.active-link').eq(index).find('input[type="radio"]').eq(val[1]).prop('checked', true);
};
$.each(_linkData.link, function(index, val) {
if(val[1]) {
$('#pageslide .active-link.' + val[0]).find('input[type="checkbox"]').prop('checked', true).end().find('.active-mune').slideDown(300);
$('#pageslide .active-link.' + val[0]).find('input[type="radio"]').eq(val[2]).prop('checked', true);
}
});
$('#pageslide').find('.nano').nanoScroller({ scrollTop: 0, iOSNativeScrolling: true });
})

View File

@ -1,4 +1,5 @@
class Admin::ItemsController < OrbitBackendController
include ActionView::Helpers::DynamicForm
layout "structure"

View File

@ -32,20 +32,22 @@ class Admin::LinksController < Admin::ItemsController
def update
@item = Link.find(params[:id])
if @item.update_attributes(params[:link])
success = true
else
success = check_valid_url
unless @item.update_attributes(params[:link])
@error = error_messages_for(@item)
end
if success
flash.now[:notice] = t('update.success.link')
respond_to do |format|
format.js { render 'admin/items/reload_items' }
end
def get_link_setting_json
begin
link = Link.find(params[:id])
m = {}
m["public"] = link.is_published ? 1 : 0
m["link"] = @site_valid_locales.inject([]) do |result, locale|
result << [locale, (link.enabled_for && link.enabled_for.include?(locale)) ? 1 : 0, (link.menu_enabled_for && link.menu_enabled_for[locale]) ? 1 :0]
end
else
flash.now[:error] = t('update.error.link')
render :action => "edit"
render json: JSON.pretty_generate(m)
rescue
render :json => {error: ''}, status: 422
end
end

View File

@ -2,8 +2,6 @@ class Admin::PagesController < Admin::ItemsController
helper Admin::PagePartsHelper
include Admin::FrontendWidgetInterface
include ActionView::Helpers::DynamicForm
def show
@item = Page.find(params[:id])
@no_orbit_bar = true
@ -158,7 +156,7 @@ class Admin::PagesController < Admin::ItemsController
m["count"] = page.frontend_data_count
render json: JSON.pretty_generate({design: design, module: m})
rescue
render :json => {error: 'hahah'}, status: 422
render :json => {error: ''}, status: 422
end
end

View File

@ -1,8 +1,8 @@
class Link < Item
field :url
field :url, localize: true
validates :url, :presence => true, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|localhost(:[0-9]{1,5})?(\/.*)?/i
# validates :url, :presence => true, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|localhost(:[0-9]{1,5})?(\/.*)?/i
before_validation :add_http
@ -10,12 +10,23 @@ class Link < Item
ApplicationController.helpers.link_to(self.name, self.url)
end
def urls
self.url_translations.inject({}) do |result, (key, value)|
result["url-#{key}"] = value
result
end
end
protected
def add_http
unless self.url[/^(http|https):\/\//] || self.url.blank?
self.url = 'http://' + self.url
end
if self.url_translations.present?
self.url_translations.each_value do |u|
unless u[/^(http|https):\/\//] || u.blank?
u = 'http://' + u
end
end
end
end
end

View File

@ -0,0 +1,57 @@
<div id="link" class="hide">
<%= form_for :link, url: admin_links_path, remote: true do |f| %>
<fieldset>
<div id="page_error"></div>
<%= f.hidden_field :parent, class: "parent" %>
<%= f.label :name, content_tag(:i, nil, :class => "icons-star") + t(:name) %>
<%= f.text_field :name, class: 'input-xlarge', placeholder: t(:name), id: 'name' %>
<span class="help-block"><%= t("front_page.name_field_helper") %></span>
<%= f.fields_for :title_translations do |f| %>
<% @site_valid_locales.each do |locale| %>
<%= f.label :locale, "#{t(:title)} #{I18nVariable.from_locale(locale)}" %>
<%= f.text_field locale, class: 'input-xlarge', placeholder: "#{t(:title)} #{I18nVariable.from_locale(locale)}", id: locale %>
<%= f.label :url, "#{t(:url)} #{I18nVariable.from_locale(locale)}" %>
<%= f.text_field :url, :class => 'input-xlarge', placeholder: "#{t(:url)} #{I18nVariable.from_locale(locale)}", id: "url-#{locale}" %>
<% end %>
<% end %>
<div class="checkbox-groups" id="link-is-published">
<%= f.label :is_published, t(:is_published) %>
<p>
<label class="radio inline">
<%= f.radio_button :is_published, true %> <%= t(:yes_) %>
</label>
<label class="radio inline">
<%= f.radio_button :is_published, false, checked: "checked" %> <%= t(:no_) %>
</label>
</p>
</div>
<div class="link-options checkbox-groups hide">
<label><%= t(:activation_link) %></label>
<% @site_valid_locales.each do |valid_locale| %>
<div class="active-link <%= valid_locale %>">
<label class="checkbox">
<%= check_box_tag 'link[enabled_for][]', valid_locale, false, class: "lang-enable" %> <%= I18nVariable.from_locale(valid_locale) %>
</label>
<div class="active-mune">
<span><%= t(:menu_enable) %></span>
<label class="radio inline">
<%= radio_button_tag "link[menu_enabled_for][#{valid_locale}]", false, true %> <%= t(:no_) %>
</label>
<label class="radio inline">
<%= radio_button_tag "link[menu_enabled_for][#{valid_locale}]", true, false %> <%= t(:yes_) %>
</label>
</div>
</div>
<% end %>
</div>
<div class="form-actions">
<a href="javascript:$.pageslide.close()" class="btn btn-small"><%= t(:cancel) %></a>
<%= f.submit t(:submit), class: 'btn btn-primary btn-small' %>
</div>
</fieldset>
<% end %>
</div>

View File

@ -63,7 +63,7 @@
</p>
</div>
<div class="link-options checkbox-groups">
<div class="link-options checkbox-groups hide">
<label><%= t(:activation_link) %></label>
<% @site_valid_locales.each do |valid_locale| %>
<div class="active-link <%= valid_locale %>">

View File

@ -18,9 +18,13 @@
<% end %>
<div class="item-menu">
<%= link_to content_tag(:i, nil, class: "icon-eye-open"), "/#{node.path}?edit=true", class: "view-page open-slide tip", title: t(:view), data: {title: node.title} if node.class.to_s.eql?('Page') %>
<%= link_to content_tag(:i, nil, class: "icon-edit"), "#page", class: "open-slide tip #{node.class.to_s.downcase} edit", title: t(:edit), data: {title: t(:edit), id: node.id.to_s, parent: node.parent_id.to_s, form: {name: node.name}.merge(node.title_translations)} %>
<% if node.class.to_s.downcase.eql?("page") %>
<%= link_to content_tag(:i, nil, class: "icon-edit"), "#page", class: "open-slide tip page edit", title: t('editing.page'), data: {title: t('editing.page'), id: node.id.to_s, parent: node.parent_id.to_s, form: {name: node.name}.merge(node.title_translations)} %>
<% elsif node.class.to_s.downcase.eql?("link") %>
<%= link_to content_tag(:i, nil, class: "icon-edit"), "#link", class: "open-slide tip link edit", title: t('editing.link'), data: {title: t('editing.link'), id: node.id.to_s, parent: node.parent_id.to_s, form: {name: node.name}.merge(node.title_translations).merge(node.urls)} %>
<% end %>
<%= link_to content_tag(:i, nil, class: "icons-newspaper"), "#page", class: "open-slide tip page", title: t(:add_page), data: {title: t(:add_page), id: 'new', parent: node.parent_id.to_s} if node.class.to_s.eql?('Page') %>
<%= link_to content_tag(:i, nil, class: "icon-link"), new_admin_link_path(:parent_id => node.id), class: "open-slide tip link", title: t(:add_link), data: {title: t(:add_link)} if node.class.to_s.eql?('Page') %>
<%= link_to content_tag(:i, nil, class: "icon-link"), "#link", class: "open-slide tip link", title: t(:add_link), data: {title: t(:add_link)} if node.class.to_s.eql?('Page') %>
<%= link_to content_tag(:i, nil, class: "icon-trash"), nil, rel: eval("admin_#{node.class.to_s.downcase}_path(node)"), class: "delete tip", title: t(:delete_), data: {title: t(:delete_)} unless node.root? %>
</div>
</div>

View File

@ -6,4 +6,5 @@
<% end %>
<%= render 'layouts/delete_modal', delete_options: {remote: true} %>
<%= render 'form_page' %>
<%= render 'form_link' %>
<%= javascript_include_tag "lib/pageslide.js" %>

View File

@ -1,95 +0,0 @@
<%= flash_messages %>
<%= f.error_messages %>
<%= f.hidden_field :parent, :value => (@item.parent.id rescue nil) %>
<%= f.hidden_field :id, :value => (@item.id), :id => "object_id" %>
<%= f.label :name, content_tag(:i, nil, :class => "icons-star") + t(:name) %>
<%= f.text_field :name, class: 'input-xlarge', placeholder: t(:name) %>
<span class="help-block"><%= I18n.t("front_page.name_field_helper") %></span>
<%= f.fields_for :title_translations do |f| %>
<% @site_valid_locales.each do |locale| %>
<%= f.label :locale, "#{t(:title)} #{I18nVariable.from_locale(locale)}" %>
<%= f.text_field locale, class: 'input-xlarge', placeholder: "#{t(:title)} #{I18nVariable.from_locale(locale)}", value: (@item.title_translations[locale] rescue nil) %>
<% end %>
<% end %>
<%= f.label :design, t(:template_name) %>
<%= f.collection_select :design, @designs, :id, :title, {selected: (@selected[:design].id rescue nil) }, {rel: reload_themes_admin_pages_path, class: "input-xlarge"} %>
<%= f.label :theme, t(:theme) %>
<%= f.select :theme_id, @themes.collect { |t| [t.name.capitalize, t.id] }, {include_blank: true, selected: (@selected[:theme].id rescue nil) }, { class: "input-xlarge" } %>
<div id="module_app_list">
<label for="select_module_app">
<%= t("default_widget.select_module_app") %>
</label>
<%= f.select :module_app_id, options_from_collection_for_select(@module_apps, :id, :module_name, selected: (@selected[:module_app].id rescue nil)), {include_blank: true }, {rel: reload_after_module_changed_admin_pages_path, id: "page_module_app_id", class: "input-xlarge"} %>
</div>
<div id="frontend_list">
<label for="select_app_url">
<%= t("default_widget.select_app_url") %>
</label>
<div id="app_page_url">
<%= select('page','app_frontend_url', @app_frontend_urls || [], {selected: @selected[:app_frontend_url], rel: reload_after_list_changed_admin_pages_path}, class: "input-xlarge") rescue '' %>
</div>
</div>
<div id="app_page_category">
<%= render :partial=>"admin/page_parts/widget_data_source_category",:locals=>{:object=>@item} %>
</div>
<div id="app_page_tag">
<%= render :partial=>"admin/page_parts/widget_data_source_tag" ,:locals=>{:object=>@item}%>
</div>
<div id="frontend_setting">
<% if show_default_widget_setting_panel %>
<%= render :partial=>'admin/page_parts/widget_setting',:locals=>{:object=>@item,:f=>f} %>
<% end %>
</div>
<div id='data_count'>
<%= render :partial=>'data_count_field',:locals=>{:field_name=>"page[frontend_data_count]",:field_value=>@selected[:page_frontend_data_count]} %>
</div>
<%= f.label :is_published, "#{t('front_page.is_published')} ?" %>
<p>
<label class="radio inline">
<%= f.radio_button :is_published, true %>
<%= t(:yes_) %>
</label>
<label class="radio inline">
<%= f.radio_button :is_published, false %>
<%= t(:no_) %>
</label>
</p>
<%= f.label :menu_enabled_for, "#{t('front_page.menu_enable_lang')}:" %>
<p>
<% @site_valid_locales.each do |valid_locale| %>
<label class="checkbox inline">
<%= check_box_tag 'page[menu_enabled_for][]', valid_locale, (@item.menu_enabled_for.nil? ? true : @item.menu_enabled_for.include?(valid_locale)) %>
<%= I18nVariable.from_locale(valid_locale) %>
</label>
<% end %>
<%= hidden_field_tag 'page[menu_enabled_for][]', '' %>
</p>
<%= f.label :enabled_for, "#{t('front_page.link_enable_lang')}:" %>
<p>
<% @site_valid_locales.each do |valid_locale| %>
<label class="checkbox inline">
<%= check_box_tag 'page[enabled_for][]', valid_locale, (@item.enabled_for.nil? ? true : @item.enabled_for.include?(valid_locale)) %>
<%= I18nVariable.from_locale(valid_locale) %>
</label>
<% end %>
<%= hidden_field_tag 'page[enabled_for][]', '' %>
<!-- <p class="help-block">In addition to freeform text, any HTML5 text-based input appears like so.</p> -->
</div>
</div>
<%=javascript_include_tag "lib/items/update_cates_and_tags.js" %>
<%= javascript_include_tag "lib/items/page_widget_edit_interface.js" %>

View File

@ -94,6 +94,9 @@ Orbit::Application.routes.draw do
end
resources :links do
collection do
get 'get_link_setting_json'
end
member do
get 'delete'
end

View File

@ -58,6 +58,10 @@ namespace :new_ui do
menu_enabled_for_to_hash
end
task :link_url_to_hash => :environment do
link_url_to_hash
end
def migrate_categories(args = nil)
if args && args[:app_key] && args[:model_name]
migrate_category(args[:app_key], args[:model_name], args[:category_name])
@ -270,4 +274,17 @@ namespace :new_ui do
end
end
def link_url_to_hash
@db ||= Mongoid.database
collection_link = @db['items']
links = collection_link.find({_type: 'Link'})
links.each do |link|
link['url'] = VALID_LOCALES.inject({}) do |enable, locale|
enable[locale] = link['url']
enable
end
collection_link.save(link)
end
end
end