From 8ebfac2b990a616ee56a6df6c7c810c50a95e893 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 23 Oct 2013 18:28:09 +0800 Subject: [PATCH] Structure link with json --- app/assets/javascripts/lib/items/items.js.erb | 25 ++--- app/controllers/admin/items_controller.rb | 1 + app/controllers/admin/links_controller.rb | 26 ++--- app/controllers/admin/pages_controller.rb | 4 +- app/models/link.rb | 21 +++- app/views/admin/items/_form_link.html.erb | 57 +++++++++++ app/views/admin/items/_form_page.html.erb | 2 +- app/views/admin/items/_node.html.erb | 8 +- app/views/admin/items/index.html.erb | 1 + .../admin/{pages => items}/update.js.erb | 0 app/views/admin/pages/_form.html.erb | 95 ------------------- config/routes.rb | 3 + lib/tasks/new_ui.rake | 17 ++++ 13 files changed, 124 insertions(+), 136 deletions(-) create mode 100644 app/views/admin/items/_form_link.html.erb rename app/views/admin/{pages => items}/update.js.erb (100%) delete mode 100644 app/views/admin/pages/_form.html.erb diff --git a/app/assets/javascripts/lib/items/items.js.erb b/app/assets/javascripts/lib/items/items.js.erb index 09e3d85fb..8296503e4 100644 --- a/app/assets/javascripts/lib/items/items.js.erb +++ b/app/assets/javascripts/lib/items/items.js.erb @@ -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 }); }) diff --git a/app/controllers/admin/items_controller.rb b/app/controllers/admin/items_controller.rb index f3cb3325c..875d2abe7 100644 --- a/app/controllers/admin/items_controller.rb +++ b/app/controllers/admin/items_controller.rb @@ -1,4 +1,5 @@ class Admin::ItemsController < OrbitBackendController + include ActionView::Helpers::DynamicForm layout "structure" diff --git a/app/controllers/admin/links_controller.rb b/app/controllers/admin/links_controller.rb index 9813c560b..923435cad 100644 --- a/app/controllers/admin/links_controller.rb +++ b/app/controllers/admin/links_controller.rb @@ -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 diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index 14b059cab..763cbe9f0 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -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 diff --git a/app/models/link.rb b/app/models/link.rb index a7aa7cf6b..5f2f28080 100644 --- a/app/models/link.rb +++ b/app/models/link.rb @@ -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 diff --git a/app/views/admin/items/_form_link.html.erb b/app/views/admin/items/_form_link.html.erb new file mode 100644 index 000000000..7a448095d --- /dev/null +++ b/app/views/admin/items/_form_link.html.erb @@ -0,0 +1,57 @@ + \ No newline at end of file diff --git a/app/views/admin/items/_form_page.html.erb b/app/views/admin/items/_form_page.html.erb index ed91e217b..e89b277ae 100644 --- a/app/views/admin/items/_form_page.html.erb +++ b/app/views/admin/items/_form_page.html.erb @@ -63,7 +63,7 @@

- - -<%=javascript_include_tag "lib/items/update_cates_and_tags.js" %> -<%= javascript_include_tag "lib/items/page_widget_edit_interface.js" %> diff --git a/config/routes.rb b/config/routes.rb index afa9414be..4cc0bc199 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/lib/tasks/new_ui.rake b/lib/tasks/new_ui.rake index 6f8c16514..73ca653bd 100644 --- a/lib/tasks/new_ui.rake +++ b/lib/tasks/new_ui.rake @@ -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