Skip to content

Commit

Permalink
Rename to discourse-annotator #96
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel authored and Daniel committed Dec 27, 2021
1 parent 22cd333 commit e97d157
Show file tree
Hide file tree
Showing 85 changed files with 421 additions and 523 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -921,16 +921,16 @@
if (annotation.ranges === void 0) {

// START damingo (Github ID), 2020-08-09, Set up one annotation for each tag. Required to display
// the created tags in the popover.
var firstTag = annotation.tags.shift();
if (annotation.tags.length >= 1) {
for (var i = 0, len = annotation.tags.length; i < len; i++) {
// the created codes in the popover.
var firstTag = annotation.codes.shift();
if (annotation.codes.length >= 1) {
for (var i = 0, len = annotation.codes.length; i < len; i++) {
var clonedAnnotation = Object.assign({}, annotation);
clonedAnnotation.tags = [annotation.tags[i]]
clonedAnnotation.codes = [annotation.codes[i]]
this.setupAnnotation(clonedAnnotation);
}
}
annotation.tags = [firstTag];
annotation.codes = [firstTag];
// END

return this.setupAnnotation(annotation);
Expand Down Expand Up @@ -2626,7 +2626,7 @@
if (!Annotator.supported()) return;
this.field = this.annotator.editor.addField({
// 2020-08-05 / damingo / Adapt text.
label: Annotator._t('Add tag here') + '\u2026',
label: Annotator._t('Add code here') + '\u2026',
load: this.updateField,
submit: this.setAnnotationTags
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ Annotator.Plugin.MyTags = function (element, message) {
this.annotator.viewer.addField({
load: function (field, annotation) {
field = $(field);
// console.log(annotation.tags);
if (annotation.tags && $.isArray(annotation.tags) && annotation.tags.length) {
// console.log(annotation.codes);
if (annotation.codes && $.isArray(annotation.codes) && annotation.codes.length) {
return field.addClass('annotator-tags').html(function () {
var string;
return string = $.map(annotation.tags, function (tag) {
return string = $.map(annotation.codes, function (tag) {
return '<span class="annotator-tag">' + tag + '</span>';
}).join(' ');
});
Expand All @@ -42,7 +42,7 @@ Annotator.Plugin.MyTags = function (element, message) {
this.field = this.annotator.editor.addField({
label: Annotator._t('Add some codes here') + '\u2026',
submit: function (field, annotation) {
return annotation.tags = parseTags(plugin.input.val());
return annotation.codes = parseTags(plugin.input.val());
}
});

Expand All @@ -61,8 +61,8 @@ Annotator.Plugin.MyTags = function (element, message) {

this.annotator.subscribe("annotationEditorShown", function (editor, annotation) {
tagify.removeAllTags();
if (annotation.tags) {
tagify.addTags(annotation.tags);
if (annotation.codes) {
tagify.addTags(annotation.codes);
}
if (Object.keys(annotation).length === 0) { // create annotation
tagify.settings.maxTags = Infinity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def index

# Only annotations that are tagged with the given Open Ethnographer code(s).
if params[:code_id].present?
tag_ids = params[:include_sub_codes].present? ? DiscourseAnnotator::Tag.find(params[:code_id]).subtree_ids : params[:code_id]
scope = scope.where(tag_id: tag_ids)
code_ids = params[:include_sub_codes].present? ? DiscourseAnnotator::Code.find(params[:code_id]).subtree_ids : params[:code_id]
scope = scope.where(code_id: code_ids)
end

search_term = params[:search].to_s.strip
Expand All @@ -47,8 +47,6 @@ def index
# See: https://github.com/edgeryders/discourse-annotator/issues/201
resources = resources.left_joins(:post).select('discourse_annotator_annotations.*, posts.user_id as post_creator_id')

# Rename tag_id to code_id
r = resources.to_a.map(&:attributes).each { |a| a['code_id'] = a.delete('tag_id') }
render json: JSON.pretty_generate(JSON.parse(r.to_json))
}
end
Expand All @@ -64,7 +62,7 @@ def records_per_page
def create
success = true
# Create one annotation for each provided code-name.
params[:tags].each do |path|
params[:codes].each do |path|
code = get_code(path)
format_client_input_to_rails_convention_for_create(code)

Expand Down Expand Up @@ -119,12 +117,12 @@ def update
end
end

def update_tag
def update_code
fallback_path = annotator_discourse_annotator_annotations_path
ids = params[:selected_ids].split(',')
redirect_back fallback_location: fallback_path, notice: 'No annotations were selected.' and return if ids.blank?
status = []
ids.each { |id| status << DiscourseAnnotator::Annotation.find(id).update(tag_id: params[:tag_id]) }
ids.each { |id| status << DiscourseAnnotator::Annotation.find(id).update(code_id: params[:code_id]) }
msg = []
msg << "#{status.count(true)} annotations were successfully moved." if status.count(true) > 0
msg << "#{status.count(false)} annotations could not be moved." if status.count(false) > 0
Expand Down Expand Up @@ -181,15 +179,15 @@ def set_current_user
# "start"=>11.560403,
# "end"=>16.2217363
# },
# "tags"=>["test", "tag", "→", "testtag", "deutsch"],
# "codes"=>["test", "tag", "→", "testtag", "deutsch"],
# "updated"=>"2019-06-19T19:39:59.349Z",
# "created"=>"2019-06-19T19:39:59.349Z",
# "text"=>"",
# "media"=>"video",
# "ranges"=>[],
# "quote"=>"",
# "annotator_schema_version"=>"v1.0",
# video_annotation"=>{"uri"=>"/post/1"}
# "video_annotation"=>{"uri"=>"/post/1"}
# }
#
# IMAGE annotation input example:
Expand All @@ -213,7 +211,7 @@ def set_current_user
# }
def format_client_input_to_rails_convention_for_create(code)
params[:annotation] = {}
params[:annotation][:tag_id] = code.id
params[:annotation][:code_id] = code.id

# VideoAnnotation
params[:annotation][:uri] = params[:uri] unless params[:uri].blank?
Expand Down Expand Up @@ -256,7 +254,7 @@ def format_client_input_to_rails_convention_for_create(code)
# Convert the data sent by AnnotatorJS to the format that Rails expects so
# that we are able to create a proper params object
def format_client_input_to_rails_convention_for_update
code = get_code(params[:tags].first)
code = get_code(params[:codes].first)
format_client_input_to_rails_convention_for_create(code)
# Annotator sends duplicate ranges when an annotation is updated which would then be saved.
# As ranges are not allowed to be changed we can simply remove the provided attributes.
Expand All @@ -266,7 +264,7 @@ def format_client_input_to_rails_convention_for_update
# Only allow a trusted parameter 'white list' through.
def annotation_params
params.require(:annotation).permit(
:tag_id, :uri, :post_id, :topic_id,
:code_id, :uri, :post_id, :topic_id,
# VideoAnnotation
:container, :src, :ext, :start, :end,
# Image Annotation
Expand All @@ -279,15 +277,15 @@ def annotation_params
# codes in the path can be separated by ` -> ` or ` → `
def get_code(path)
return if path.blank?
normalized_path = path.gsub('->', DiscourseAnnotator::LocalizedTag.path_separator)
normalized_path = path.gsub('->', DiscourseAnnotator::LocalizedCode.path_separator)
language = DiscourseAnnotator::UserSetting.language_for_user(current_user)
path_items = []
code = nil
normalized_path.split(DiscourseAnnotator::LocalizedTag.path_separator).map(&:strip).each do |code_name|
normalized_path.split(DiscourseAnnotator::LocalizedCode.path_separator).map(&:strip).each do |code_name|
path_items << code_name
code = DiscourseAnnotator::Tag.
joins(:localized_tags).
where("lower(discourse_annotator_localized_tags.path) = ?", path_items.join(DiscourseAnnotator::LocalizedTag.path_separator).downcase)&.first ||
code = DiscourseAnnotator::Code.
joins(:localized_codes).
where("lower(discourse_annotator_localized_codes.path) = ?", path_items.join(DiscourseAnnotator::LocalizedCode.path_separator).downcase)&.first ||
create_code!(parent: code, name: code_name, language: language)
end
code
Expand All @@ -306,7 +304,7 @@ def valid_action?(name, resource = resource_class)
# language:
# parent:
def create_code!(args = {})
code = DiscourseAnnotator::Tag.new(parent: args[:parent], creator: current_user)
code = DiscourseAnnotator::Code.new(parent: args[:parent], creator: current_user)
code.names.build(name: args[:name], language: args[:language])
code.save!
code
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require_dependency 'annotator/application_controller'

class Annotator::DiscourseAnnotator::CodeNamesController < Annotator::ApplicationController


end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# https://github.com/thoughtbot/administrate/blob/master/app/controllers/administrate/application_controller.rb
#
class Annotator::DiscourseAnnotator::TagsController < Annotator::ApplicationController
class Annotator::DiscourseAnnotator::CodesController < Annotator::ApplicationController

include Annotator::ApplicationHelper

Expand All @@ -13,11 +13,11 @@ def index
resources = scoped_resource
# 1. Apply filter
resources = resources.where(creator_id: params[:creator_id]) if params[:creator_id].present?
if params[:discourse_tag].present? && (tag = ::Tag.find_by(name: params[:discourse_tag]))
resources = resources.where(id: DiscourseAnnotator::Annotation.where(topic_id: tag.topic_ids).select(:tag_id))
if params[:discourse_tag].present? && (discourse_tag = ::Tag.find_by(name: params[:discourse_tag]))
resources = resources.where(id: DiscourseAnnotator::Annotation.where(topic_id: discourse_tag.topic_ids).select(:code_id))
end
if params[:search].present?
resources = resources.where("discourse_annotator_localized_tags.path ILIKE ?", "%#{params[:search].split.join('%')}%")
resources = resources.where("discourse_annotator_localized_codes.path ILIKE ?", "%#{params[:search].split.join('%')}%")
end
# 2. For the tree-view get and limit the results to the root codes of matching codes.
# Note: Must be done before pagination is applied.
Expand All @@ -27,14 +27,14 @@ def index
resources = scoped_resource.where(id: root_ids)
end
# 3. Sorting
resources = order_tags(tags: resources, order: params[:order])
resources = order_codes(codes: resources, order: params[:order])
# 4. Select & Joins
if params[:search].present?
# See: https://github.com/edgeryders/discourse-annotator/issues/200#issuecomment-728110668
resources = resources.with_localized_path
else
language = (current_user.present? && api_request?) ? DiscourseAnnotator::UserSetting.language_for_user(current_user) : DiscourseAnnotator::Language.english
resources = resources.with_localized_tags(language: language)
resources = resources.with_localized_codes(language: language)
end
# 5. Pagination
resources = resources.page(params[:page]).per(records_per_page)
Expand Down Expand Up @@ -115,11 +115,11 @@ def update
end

def update_parent
fallback_path = annotator_discourse_annotator_tags_path(creator_id: current_user.id)
fallback_path = annotator_discourse_annotator_codes_path(creator_id: current_user.id)
ids = params[:selected_ids].split(',')
redirect_back fallback_location: fallback_path, notice: 'No codes were selected.' and return if ids.blank?
status = []
ids.each { |id| status << DiscourseAnnotator::Tag.find(id).update(parent_id: params[:parent_id]) }
ids.each { |id| status << DiscourseAnnotator::Code.find(id).update(parent_id: params[:parent_id]) }
msg = []
msg << "#{status.count(true)} codes were successfully updated." if status.count(true) > 0
msg << "#{status.count(false)} codes could not be updated." if status.count(false) > 0
Expand All @@ -128,7 +128,7 @@ def update_parent

def copy
msg = requested_resource.copy ? 'Code was successfully copied.' : 'An error occurred while coping the code!'
redirect_back fallback_location: annotator_discourse_annotator_tags_path(creator_id: current_user.id), notice: msg
redirect_back fallback_location: annotator_discourse_annotator_codes_path(creator_id: current_user.id), notice: msg
end

def merge
Expand All @@ -138,8 +138,8 @@ def merge
end

def merge_into
if requested_resource.merge_into(DiscourseAnnotator::Tag.find(params[:tag][:merge_into_tag_id]))
redirect_to annotator_discourse_annotator_tags_path(creator_id: current_user.id), notice: 'Codes were successfully merged.'
if requested_resource.merge_into(DiscourseAnnotator::Code.find(params[:code][:merge_into_code_id]))
redirect_to annotator_discourse_annotator_codes_path(creator_id: current_user.id), notice: 'Codes were successfully merged.'
else
render :merge, locals: { page: Administrate::Page::Form.new(dashboard, requested_resource) }
end
Expand Down Expand Up @@ -185,7 +185,7 @@ def api_request?
# Foo.find_by!(slug: param)
# end

# scope = scope.joins(:names).where(discourse_annotator_tag_names: {name: params[:search] }) if params[:search].present?
# scope = scope.joins(:names).where(discourse_annotator_code_names: {name: params[:search] }) if params[:search].present?
# resources = Administrate::Search.new(scope, dashboard_class, search_term).run
# resources = apply_collection_includes(resources)
# resources = params[:search].present? ? order.apply(resources) : resources.order(updated_at: :desc)
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class Annotator::DiscourseAnnotator::LocalizedCodesController < Annotator::ApplicationController


def index
settings = current_user.discourse_annotator_settings

codes = DiscourseAnnotator::Code
.select('discourse_annotator_codes.id, discourse_annotator_localized_codes.path localized_path')
.order("LOWER(discourse_annotator_localized_codes.path) ASC")
.joins(:localized_codes)
.where(discourse_annotator_localized_codes: {language_id: DiscourseAnnotator::UserSetting.language_for_user(current_user).id})

if settings&.discourse_tag.present?
code_ids = DiscourseAnnotator::Annotation.select('DISTINCT code_id')
.where(topic_id: current_user.discourse_annotator_settings.discourse_tag.topics)
codes = codes.where(id: code_ids)
end

if settings&.propose_codes_from_users.present?
user_ids = settings&.propose_codes_from_users.split.map { |n| User.find_by(username: n)&.id }.compact
user_ids << current_user.id
codes = codes.where(creator_id: user_ids)
elsif settings&.discourse_tag.blank?
codes = codes.where(creator_id: current_user.id)
end

# codes = codes.where("' ' || discourse_annotator_localized_codes.path ILIKE ?", "% #{params[:q].split.join('%')}%") if params[:q].present?
codes = codes.where("discourse_annotator_localized_codes.path ILIKE ?", "%#{params[:q].split.join('%')}%") if params[:q].present?

respond_to do |format|
format.json { render json: codes.to_json(fields: %i[code_id localized_path]) }
end
end


def mergeable
list
end


def autosuggest_codes
list
end


private

def list
codes = DiscourseAnnotator::Code
.joins(:creator)
.joins(:localized_codes)
.select('discourse_annotator_codes.id, users.username username, discourse_annotator_localized_codes.path localized_path')
.order("LOWER(discourse_annotator_localized_codes.path) ASC")
.group(%w[discourse_annotator_codes.id users.username discourse_annotator_localized_codes.path])

# Removed. See: https://github.com/edgeryders/discourse-annotator/issues/200
# .where(discourse_annotator_localized_codes: {language_id: DiscourseAnnotator::UserSetting.language_for_user(current_user).id})

codes = codes.where.not(id: params[:code_id]) if params[:code_id].present?
# codes = codes.where("' ' || discourse_annotator_localized_codes.path ILIKE ?", "% #{params[:q].split.join('%')}%") if params[:q].present?
codes = codes.where("discourse_annotator_localized_codes.path ILIKE ?", "%#{params[:q].split.join('%')}%") if params[:q].present?
respond_to do |format|
format.json {
render json: codes.map { |c|
{id: c.id, localized_path: "#{c.localized_path} (by #{c.username})"}
}.to_json
}
end
end


end

Loading

0 comments on commit e97d157

Please sign in to comment.