From 24e7beed5bf247199b0f6b382dd0d63eb68d8dc2 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Mon, 8 Sep 2014 16:23:15 +0300 Subject: [PATCH] New domain form --- app/assets/javascripts/app.js.coffee | 10 ++++++++++ app/assets/javascripts/application.js.coffee | 1 + app/controllers/admin/contacts_controller.rb | 7 +++++++ app/controllers/admin/domains_controller.rb | 4 ++++ app/controllers/admin/registrars_controller.rb | 7 +++++++ app/helpers/application_helper.rb | 3 +++ app/views/admin/domains/new.haml | 18 ++++++++++++++++++ app/views/layouts/application.html.haml | 2 +- config/locales/en.yml | 3 +++ config/routes.rb | 11 +++++++++++ .../javascripts/bootstrap3-typeahead.min.js | 1 + 11 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/app.js.coffee create mode 100644 app/controllers/admin/contacts_controller.rb create mode 100644 app/controllers/admin/registrars_controller.rb create mode 100644 app/views/admin/domains/new.haml create mode 100644 vendor/assets/javascripts/bootstrap3-typeahead.min.js diff --git a/app/assets/javascripts/app.js.coffee b/app/assets/javascripts/app.js.coffee new file mode 100644 index 000000000..a10de5a92 --- /dev/null +++ b/app/assets/javascripts/app.js.coffee @@ -0,0 +1,10 @@ +$(".js-registrars-typeahead").typeahead + source: (query, process) -> + $.get "/admin/registrars/search", {query: query}, (data) -> + process data + +$(".js-contacts-typeahead").typeahead + source: (query, process) -> + $.get "/admin/contacts/search", {query: query}, (data) -> + process data + diff --git a/app/assets/javascripts/application.js.coffee b/app/assets/javascripts/application.js.coffee index 46e077da0..acb907093 100644 --- a/app/assets/javascripts/application.js.coffee +++ b/app/assets/javascripts/application.js.coffee @@ -4,6 +4,7 @@ #= require bootstrap-sprockets #= require nprogress #= require nprogress-turbolinks +#= require bootstrap3-typeahead.min #= require_tree . NProgress.configure diff --git a/app/controllers/admin/contacts_controller.rb b/app/controllers/admin/contacts_controller.rb new file mode 100644 index 000000000..8df0bd5b4 --- /dev/null +++ b/app/controllers/admin/contacts_controller.rb @@ -0,0 +1,7 @@ +class Admin::ContactsController < ApplicationController + def search + c = Contact.arel_table + query_string = "%#{params[:query]}%" + render json: Contact.where(c[:code].matches(query_string)).pluck(:code) + end +end diff --git a/app/controllers/admin/domains_controller.rb b/app/controllers/admin/domains_controller.rb index 682c3d7ea..922ba8bc4 100644 --- a/app/controllers/admin/domains_controller.rb +++ b/app/controllers/admin/domains_controller.rb @@ -1,6 +1,10 @@ class Admin::DomainsController < ApplicationController before_action :set_domain, only: [:show] + def new + @domain = Domain.new + end + def index @q = Domain.search(params[:q]) @domains = @q.result.page(params[:page]) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb new file mode 100644 index 000000000..aa91d0951 --- /dev/null +++ b/app/controllers/admin/registrars_controller.rb @@ -0,0 +1,7 @@ +class Admin::RegistrarsController < ApplicationController + def search + r = Registrar.arel_table + query_string = "%#{params[:query]}%" + render json: Registrar.where(r[:name].matches(query_string)).pluck(:name) + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be7945..7e225ced6 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,5 @@ module ApplicationHelper + def coffee_script_tag(&block) + content_tag(:script, CoffeeScript.compile(capture(&block)).html_safe, :type => 'text/javascript') + end end diff --git a/app/views/admin/domains/new.haml b/app/views/admin/domains/new.haml new file mode 100644 index 000000000..4e3250d8c --- /dev/null +++ b/app/views/admin/domains/new.haml @@ -0,0 +1,18 @@ +%h2= t('shared.new_domain') +%hr += form_for([:admin, @domain]) do |f| + .row + .col-md-6 + .form-group + = f.label :name + = f.text_field(:name, class: 'form-control') + .form-group + = f.label :period + = f.text_field(:period, class: 'form-control') + .col-md-6 + .form-group + = f.label :registrar + = f.text_field(:registrar, class: 'form-control js-registrars-typeahead', placeholder: t('shared.registrar_name')) + .form-group + = f.label :owner_contact + = f.text_field(:owner_contact, class: 'form-control js-contacts-typeahead', placeholder: t('shared.contact_code')) diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 608d2c81e..560c39e0a 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -7,7 +7,6 @@ %meta{content: "Gitlab LTD", name: "author"}/ = csrf_meta_tags = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true - = javascript_include_tag 'application', 'data-turbolinks-track' => true %link{href: "../../favicon.ico", rel: "icon"}/ %title Eesti Interneti SA %body @@ -51,4 +50,5 @@ .container = yield + = javascript_include_tag 'application', 'data-turbolinks-track' => true diff --git a/config/locales/en.yml b/config/locales/en.yml index 73f9d0896..c6ac4bc94 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -181,3 +181,6 @@ en: delete: 'Delete' are_you_sure: 'Are you sure?' back: 'Back' + new_domain: 'New domain' + registrar_name: 'Registrar name' + contact_code: 'Contact code' diff --git a/config/routes.rb b/config/routes.rb index 1afe04a4b..35a8c392f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,17 @@ Rails.application.routes.draw do namespace(:admin) do resources :domains resources :setting_groups + resources :registrars do + collection do + get :search + end + end + + resources :contacts do + collection do + get 'search' + end + end end # The priority is based upon order of creation: first created -> highest priority. diff --git a/vendor/assets/javascripts/bootstrap3-typeahead.min.js b/vendor/assets/javascripts/bootstrap3-typeahead.min.js new file mode 100644 index 000000000..79b3d79e8 --- /dev/null +++ b/vendor/assets/javascripts/bootstrap3-typeahead.min.js @@ -0,0 +1 @@ +!function($){"use strict";var t=function(t,e){this.$element=$(t),this.options=$.extend({},$.fn.typeahead.defaults,e),this.matcher=this.options.matcher||this.matcher,this.sorter=this.options.sorter||this.sorter,this.select=this.options.select||this.select,this.autoSelect="boolean"==typeof this.options.autoSelect?this.options.autoSelect:!0,this.highlighter=this.options.highlighter||this.highlighter,this.render=this.options.render||this.render,this.updater=this.options.updater||this.updater,this.source=this.options.source,this.delay="number"==typeof this.options.delay?this.options.delay:250,this.$menu=$(this.options.menu),this.shown=!1,this.listen(),this.showHintOnFocus="boolean"==typeof this.options.showHintOnFocus?this.options.showHintOnFocus:!1};t.prototype={constructor:t,select:function(){var t=this.$menu.find(".active").data("value");return(this.autoSelect||t)&&this.$element.val(this.updater(t)).change(),this.hide()},updater:function(t){return t},setSource:function(t){this.source=t},show:function(){var t=$.extend({},this.$element.position(),{height:this.$element[0].offsetHeight}),e;return e="function"==typeof this.options.scrollHeight?this.options.scrollHeight.call():this.options.scrollHeight,this.$menu.insertAfter(this.$element).css({top:t.top+t.height+e,left:t.left}).show(),this.shown=!0,this},hide:function(){return this.$menu.hide(),this.shown=!1,this},lookup:function(t){var e;if(this.query="undefined"!=typeof t&&null!==t?t:this.$element.val()||"",this.query.length"+e+""})},render:function(t){var e=this;return t=$(t).map(function(t,s){return t=$(e.options.item).data("value",s),t.find("a").html(e.highlighter(s)),t[0]}),this.autoSelect&&t.first().addClass("active"),this.$menu.html(t),this},next:function(t){var e=this.$menu.find(".active").removeClass("active"),s=e.next();s.length||(s=$(this.$menu.find("li")[0])),s.addClass("active")},prev:function(t){var e=this.$menu.find(".active").removeClass("active"),s=e.prev();s.length||(s=this.$menu.find("li").last()),s.addClass("active")},listen:function(){this.$element.on("focus",$.proxy(this.focus,this)).on("blur",$.proxy(this.blur,this)).on("keypress",$.proxy(this.keypress,this)).on("keyup",$.proxy(this.keyup,this)),this.eventSupported("keydown")&&this.$element.on("keydown",$.proxy(this.keydown,this)),this.$menu.on("click",$.proxy(this.click,this)).on("mouseenter","li",$.proxy(this.mouseenter,this)).on("mouseleave","li",$.proxy(this.mouseleave,this))},destroy:function(){this.$element.data("typeahead",null),this.$element.off("focus").off("blur").off("keypress").off("keyup"),this.eventSupported("keydown")&&this.$element.off("keydown"),this.$menu.remove()},eventSupported:function(t){var e=t in this.$element;return e||(this.$element.setAttribute(t,"return;"),e="function"==typeof this.$element[t]),e},move:function(t){if(this.shown){switch(t.keyCode){case 9:case 13:case 27:t.preventDefault();break;case 38:t.preventDefault(),this.prev();break;case 40:t.preventDefault(),this.next()}t.stopPropagation()}},keydown:function(t){this.suppressKeyPressRepeat=~$.inArray(t.keyCode,[40,38,9,13,27]),this.shown||40!=t.keyCode?this.move(t):this.lookup("")},keypress:function(t){this.suppressKeyPressRepeat||this.move(t)},keyup:function(t){switch(t.keyCode){case 40:case 38:case 16:case 17:case 18:break;case 9:case 13:if(!this.shown)return;this.select();break;case 27:if(!this.shown)return;this.hide();break;default:this.lookup()}t.stopPropagation(),t.preventDefault()},focus:function(t){this.focused||(this.focused=!0,(0===this.options.minLength&&!this.$element.val()||this.options.showHintOnFocus)&&this.lookup())},blur:function(t){this.focused=!1,!this.mousedover&&this.shown&&this.hide()},click:function(t){t.stopPropagation(),t.preventDefault(),this.select(),this.$element.focus()},mouseenter:function(t){this.mousedover=!0,this.$menu.find(".active").removeClass("active"),$(t.currentTarget).addClass("active")},mouseleave:function(t){this.mousedover=!1,!this.focused&&this.shown&&this.hide()}};var e=$.fn.typeahead;$.fn.typeahead=function(e){var s=arguments;return this.each(function(){var i=$(this),o=i.data("typeahead"),n="object"==typeof e&&e;o||i.data("typeahead",o=new t(this,n)),"string"==typeof e&&(s.length>1?o[e].apply(o,Array.prototype.slice.call(s,1)):o[e]())})},$.fn.typeahead.defaults={source:[],items:8,menu:'',item:'
  • ',minLength:1,scrollHeight:0,autoSelect:!0},$.fn.typeahead.Constructor=t,$.fn.typeahead.noConflict=function(){return $.fn.typeahead=e,this},$(document).on("focus.typeahead.data-api",'[data-provide="typeahead"]',function(t){var e=$(this);e.data("typeahead")||e.typeahead(e.data())})}(window.jQuery); \ No newline at end of file