diff options
author | Anton Davydov <antondavydov.o@gmail.com> | 2016-03-01 15:36:50 +0300 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2016-04-18 14:47:50 +0200 |
commit | 073c3d15c71a0f877b62c7d3d7417a9721da1dba (patch) | |
tree | 83dd1a990857e54ebd91f6ef3f6494e51cdd0f47 /app | |
parent | 06952aaf24633550f57fd54b70d27732509935c2 (diff) | |
download | gitlab-ce-073c3d15c71a0f877b62c7d3d7417a9721da1dba.tar.gz |
Initial implementation if license template selector and /licenses/:key API endpoint
Diffstat (limited to 'app')
-rw-r--r-- | app/assets/javascripts/api.js.coffee | 13 | ||||
-rw-r--r-- | app/assets/javascripts/blob/blob_license_selector.js.coffee | 24 | ||||
-rw-r--r-- | app/assets/stylesheets/pages/editor.scss | 9 | ||||
-rw-r--r-- | app/controllers/projects/blob_controller.rb | 9 | ||||
-rw-r--r-- | app/helpers/projects_helper.rb | 7 | ||||
-rw-r--r-- | app/views/projects/blob/_editor.html.haml | 11 | ||||
-rw-r--r-- | app/views/projects/empty.html.haml | 6 |
7 files changed, 76 insertions, 3 deletions
diff --git a/app/assets/javascripts/api.js.coffee b/app/assets/javascripts/api.js.coffee index f3ed9a66715..f4757658d54 100644 --- a/app/assets/javascripts/api.js.coffee +++ b/app/assets/javascripts/api.js.coffee @@ -5,6 +5,7 @@ group_projects_path: "/api/:version/groups/:id/projects.json" projects_path: "/api/:version/projects.json" labels_path: "/api/:version/projects/:id/labels" + license_path: "/api/:version/licenses/:key" group: (group_id, callback) -> url = Api.buildUrl(Api.group_path) @@ -92,6 +93,18 @@ ).done (projects) -> callback(projects) + # Return text for specific license + licenseText: (key, fullname, callback) -> + url = Api.buildUrl(Api.license_path) + url = url.replace(':key', key) + + $.ajax( + url: url + data: + fullname: fullname + ).done (projects) -> + callback(projects) + buildUrl: (url) -> url = gon.relative_url_root + url if gon.relative_url_root? return url.replace(':version', gon.api_version) diff --git a/app/assets/javascripts/blob/blob_license_selector.js.coffee b/app/assets/javascripts/blob/blob_license_selector.js.coffee new file mode 100644 index 00000000000..b43c1ec2bb5 --- /dev/null +++ b/app/assets/javascripts/blob/blob_license_selector.js.coffee @@ -0,0 +1,24 @@ +class @BlobLicenseSelector + licenseRegex: /^(.+\/)?(licen[sc]e|copying)($|\.)/i + + constructor: (editor)-> + self = this + @licenseSelector = $('.js-license-selector') + @toggleLicenseSelector($('#file_name').val()) + + $('#file_name').on 'input', -> + self.toggleLicenseSelector($(this).val()) + + $('select.license-select').select2( + width: 'resolve' + dropdownAutoWidth: true + placeholder: 'Choose a license template' + ).on 'change', (e) -> + Api.licenseText $(this).val(), $(this).data('fullname'), (data) -> + editor.setValue(data, -1) + + toggleLicenseSelector: (fileName) => + if @licenseRegex.test(fileName) + @licenseSelector.show() + else + @licenseSelector.hide() diff --git a/app/assets/stylesheets/pages/editor.scss b/app/assets/stylesheets/pages/editor.scss index 0f0592a0ab8..8981f070a20 100644 --- a/app/assets/stylesheets/pages/editor.scss +++ b/app/assets/stylesheets/pages/editor.scss @@ -26,6 +26,10 @@ line-height: 42px; padding-top: 7px; padding-bottom: 7px; + + .pull-right { + height: 20px; + } } .editor-ref { @@ -53,4 +57,9 @@ .select2 { float: right; } + + .encoding-selector, + .license-selector { + display: inline-block; + } } diff --git a/app/controllers/projects/blob_controller.rb b/app/controllers/projects/blob_controller.rb index cd8b2911674..6960562d15f 100644 --- a/app/controllers/projects/blob_controller.rb +++ b/app/controllers/projects/blob_controller.rb @@ -122,6 +122,15 @@ class Projects::BlobController < Projects::ApplicationController end def editor_variables + @licenses = { + 'Popular' => Licensee::License.all(featured: true).map!{ |license| [license.name, license.key] }, + 'Other' => Licensee::License.all(featured: false).map!{ |license| [license.name, license.key] } + } + + unless @repository.empty? + @current_license_key = Licensee.license(@repository.path).try(:key) + end + @target_branch = params[:target_branch] @file_path = diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 7e00aacceaa..0b0cb658b17 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -306,6 +306,13 @@ module ProjectsHelper namespace_project_new_blob_path(@project.namespace, @project, tree_join(ref), file_name: 'README.md') end + def new_license_path + ref = @repository.root_ref if @repository + ref ||= 'master' + + namespace_project_new_blob_path(@project.namespace, @project, tree_join(ref), file_name: 'LICENSE') + end + def last_push_event if current_user current_user.recent_push(@project.id) diff --git a/app/views/projects/blob/_editor.html.haml b/app/views/projects/blob/_editor.html.haml index f8b6fa253c4..8ebbc91f81d 100644 --- a/app/views/projects/blob/_editor.html.haml +++ b/app/views/projects/blob/_editor.html.haml @@ -13,7 +13,11 @@ required: true, class: 'form-control new-file-name' .pull-right - = select_tag :encoding, options_for_select([ "base64", "text" ], "text"), class: 'select2' + .license-selector.js-license-selector.hide + = select_tag :license_type, grouped_options_for_select(@licenses, @current_license_key), include_blank: true, class: 'select2 license-select', data: { fullname: @repository.project.creator.name } + + .encoding-selector + = select_tag :encoding, options_for_select([ "base64", "text" ], "text"), class: 'select2' .file-content.code %pre.js-edit-mode-pane#editor #{params[:content] || local_assigns[:blob_data]} @@ -22,3 +26,8 @@ .center %h2 %i.icon-spinner.icon-spin + +:javascript + window.onload = function() { + new BlobLicenseSelector(blob.editor) + } diff --git a/app/views/projects/empty.html.haml b/app/views/projects/empty.html.haml index 6ad7b05155a..2eb79703304 100644 --- a/app/views/projects/empty.html.haml +++ b/app/views/projects/empty.html.haml @@ -14,8 +14,10 @@ %p If you already have files you can push them using command line instructions below. %p - Otherwise you can start with - = link_to "adding README", new_readme_path, class: 'underlined-link' + Otherwise you can start with adding + = link_to "README", new_readme_path, class: 'underlined-link' + or + = link_to "LICENSE", new_license_path, class: 'underlined-link' file to this project. - if can?(current_user, :push_code, @project) |