├── test ├── dummy │ ├── public │ │ ├── favicon.ico │ │ ├── stylesheets │ │ │ ├── .gitkeep │ │ │ └── scaffold.css │ │ ├── javascripts │ │ │ ├── application.js │ │ │ ├── rails.js │ │ │ ├── dragdrop.js │ │ │ └── controls.js │ │ ├── 422.html │ │ ├── 404.html │ │ └── 500.html │ ├── app │ │ ├── helpers │ │ │ ├── posts_helper.rb │ │ │ └── application_helper.rb │ │ ├── views │ │ │ ├── posts │ │ │ │ ├── new.html.erb │ │ │ │ ├── edit.html.erb │ │ │ │ ├── show.html.erb │ │ │ │ ├── index.html.erb │ │ │ │ └── _form.html.erb │ │ │ └── layouts │ │ │ │ └── application.html.erb │ │ ├── controllers │ │ │ ├── application_controller.rb │ │ │ └── posts_controller.rb │ │ └── models │ │ │ └── post.rb │ ├── config │ │ ├── mongoid.yml │ │ ├── routes.rb │ │ ├── environment.rb │ │ ├── locales │ │ │ └── en.yml │ │ ├── initializers │ │ │ ├── mime_types.rb │ │ │ ├── inflections.rb │ │ │ ├── backtrace_silencers.rb │ │ │ ├── session_store.rb │ │ │ ├── secret_token.rb │ │ │ └── ckeditor.rb │ │ ├── boot.rb │ │ ├── database.yml │ │ ├── environments │ │ │ ├── development.rb │ │ │ ├── test.rb │ │ │ └── production.rb │ │ └── application.rb │ ├── test │ │ └── fixtures │ │ │ └── files │ │ │ ├── rails.png │ │ │ └── rails.tar.gz │ ├── config.ru │ ├── Rakefile │ ├── script │ │ └── rails │ └── db │ │ ├── migrate │ │ ├── 20110623120047_create_posts.rb │ │ └── 20110705195648_create_ckeditor_assets.rb │ │ └── schema.rb ├── orm │ ├── active_record.rb │ └── mongoid.rb ├── integration │ ├── navigation_test.rb │ └── posts_test.rb ├── support │ └── integration_case.rb ├── ckeditor_test.rb ├── generators │ ├── install_generator_test.rb │ └── models_generator_test.rb ├── test_helper.rb ├── controllers │ ├── pictures_controller_test.rb │ └── attachment_files_controller_test.rb └── routes_test.rb ├── lib ├── ckeditor │ ├── version.rb │ ├── helpers │ │ ├── form_builder.rb │ │ ├── view_helper.rb │ │ ├── controllers.rb │ │ └── form_helper.rb │ ├── hooks │ │ ├── formtastic.rb │ │ └── simple_form.rb │ ├── engine.rb │ ├── http.rb │ ├── utils.rb │ └── orm │ │ ├── mongoid.rb │ │ └── active_record.rb ├── generators │ └── ckeditor │ │ ├── templates │ │ ├── ckeditor │ │ │ ├── filebrowser │ │ │ │ ├── images │ │ │ │ │ ├── gal_add.jpg │ │ │ │ │ ├── gal_add.png │ │ │ │ │ ├── gal_del.png │ │ │ │ │ ├── gal_more.gif │ │ │ │ │ ├── preloader.gif │ │ │ │ │ └── thumbs │ │ │ │ │ │ ├── mp3.gif │ │ │ │ │ │ ├── pdf.gif │ │ │ │ │ │ ├── rar.gif │ │ │ │ │ │ ├── swf.gif │ │ │ │ │ │ ├── xls.gif │ │ │ │ │ │ └── ckfnothumb.gif │ │ │ │ ├── stylesheets │ │ │ │ │ └── uploader.css │ │ │ │ └── javascripts │ │ │ │ │ └── application.js │ │ │ ├── plugins │ │ │ │ ├── embed │ │ │ │ │ ├── images │ │ │ │ │ │ └── embed.png │ │ │ │ │ ├── lang │ │ │ │ │ │ ├── en.js │ │ │ │ │ │ ├── uk.js │ │ │ │ │ │ └── ru.js │ │ │ │ │ ├── plugin.js │ │ │ │ │ └── dialogs │ │ │ │ │ │ └── embed.js │ │ │ │ └── attachment │ │ │ │ │ ├── images │ │ │ │ │ └── attachment.png │ │ │ │ │ ├── lang │ │ │ │ │ ├── uk.js │ │ │ │ │ ├── ru.js │ │ │ │ │ └── en.js │ │ │ │ │ ├── plugin.js │ │ │ │ │ └── dialogs │ │ │ │ │ └── attachment.js │ │ │ └── config.js │ │ ├── models │ │ │ ├── mongoid │ │ │ │ └── ckeditor │ │ │ │ │ ├── asset.rb │ │ │ │ │ ├── picture.rb │ │ │ │ │ └── attachment_file.rb │ │ │ └── active_record │ │ │ │ ├── ckeditor │ │ │ │ ├── asset.rb │ │ │ │ ├── picture.rb │ │ │ │ └── attachment_file.rb │ │ │ │ └── migration.rb │ │ └── ckeditor.rb │ │ ├── models_generator.rb │ │ └── install_generator.rb └── ckeditor.rb ├── config ├── routes.rb └── locales │ ├── en.ckeditor.yml │ ├── ru.ckeditor.yml │ └── uk.ckeditor.yml ├── .gitignore ├── app ├── views │ ├── ckeditor │ │ ├── pictures │ │ │ └── index.html.erb │ │ ├── attachment_files │ │ │ └── index.html.erb │ │ └── shared │ │ │ ├── _asset_tmpl.html.erb │ │ │ └── _asset.html.erb │ └── layouts │ │ └── ckeditor.html.erb └── controllers │ └── ckeditor │ ├── pictures_controller.rb │ ├── attachment_files_controller.rb │ └── base_controller.rb ├── Gemfile ├── Rakefile ├── ckeditor.gemspec ├── MIT-LICENSE ├── Gemfile.lock └── README.rdoc /test/dummy/public/favicon.ico: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/dummy/public/stylesheets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/dummy/app/helpers/posts_helper.rb: -------------------------------------------------------------------------------- 1 | module PostsHelper 2 | end 3 | -------------------------------------------------------------------------------- /test/dummy/config/mongoid.yml: -------------------------------------------------------------------------------- 1 | test: 2 | database: ckeditor-test-suite 3 | -------------------------------------------------------------------------------- /test/dummy/app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /test/orm/active_record.rb: -------------------------------------------------------------------------------- 1 | ActiveRecord::Migrator.migrate(File.expand_path("../../dummy/db/migrate/", __FILE__)) 2 | -------------------------------------------------------------------------------- /test/dummy/config/routes.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.routes.draw do 2 | resources :posts 3 | root :to => "posts#index" 4 | end 5 | -------------------------------------------------------------------------------- /test/dummy/app/views/posts/new.html.erb: -------------------------------------------------------------------------------- 1 |

New post

2 | 3 | <%= render 'form' %> 4 | 5 | <%= link_to 'Back', posts_path %> 6 | -------------------------------------------------------------------------------- /test/dummy/test/fixtures/files/rails.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/test/dummy/test/fixtures/files/rails.png -------------------------------------------------------------------------------- /lib/ckeditor/version.rb: -------------------------------------------------------------------------------- 1 | module Ckeditor 2 | module Version 3 | GEM = "3.6.0".freeze 4 | EDITOR = "3.6.1".freeze 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /test/dummy/test/fixtures/files/rails.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/test/dummy/test/fixtures/files/rails.tar.gz -------------------------------------------------------------------------------- /test/orm/mongoid.rb: -------------------------------------------------------------------------------- 1 | class ActiveSupport::TestCase 2 | setup do 3 | Post.delete_all 4 | Ckeditor::Asset.delete_all 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | protect_from_forgery 3 | respond_to :html, :xml 4 | end 5 | -------------------------------------------------------------------------------- /test/dummy/app/views/posts/edit.html.erb: -------------------------------------------------------------------------------- 1 |

Editing post

2 | 3 | <%= render 'form' %> 4 | 5 | <%= link_to 'Show', @post %> | 6 | <%= link_to 'Back', posts_path %> 7 | -------------------------------------------------------------------------------- /test/dummy/app/views/posts/show.html.erb: -------------------------------------------------------------------------------- 1 |

<%= notice %>

2 | 3 | 4 | <%= link_to 'Edit', edit_post_path(@post) %> | 5 | <%= link_to 'Back', posts_path %> 6 | -------------------------------------------------------------------------------- /test/dummy/config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require ::File.expand_path('../config/environment', __FILE__) 4 | run Dummy::Application 5 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | namespace :ckeditor, :only => [:index, :create, :destroy] do 3 | resources :pictures 4 | resources :attachment_files 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/gal_add.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/gal_add.jpg -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/gal_add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/gal_add.png -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/gal_del.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/gal_del.png -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/gal_more.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/gal_more.gif -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/embed/images/embed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/plugins/embed/images/embed.png -------------------------------------------------------------------------------- /test/dummy/config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the rails application 2 | require File.expand_path('../application', __FILE__) 3 | 4 | # Initialize the rails application 5 | Dummy::Application.initialize! 6 | -------------------------------------------------------------------------------- /test/dummy/public/javascripts/application.js: -------------------------------------------------------------------------------- 1 | // Place your application-specific JavaScript functions and classes here 2 | // This file is automatically included by javascript_include_tag :defaults 3 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/preloader.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/preloader.gif -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/mp3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/mp3.gif -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/pdf.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/pdf.gif -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/rar.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/rar.gif -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/swf.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/swf.gif -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/xls.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/xls.gif -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/ckfnothumb.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/filebrowser/images/thumbs/ckfnothumb.gif -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/attachment/images/attachment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegram/ckeditor/master/lib/generators/ckeditor/templates/ckeditor/plugins/attachment/images/attachment.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle/ 2 | log/*.log 3 | pkg/ 4 | test/dummy/db/*.sqlite3 5 | test/dummy/db/schema.rb 6 | test/dummy/log/*.log 7 | test/dummy/public/ckeditor_assets/ 8 | test/dummy/tmp/ 9 | test/tmp 10 | 11 | *.swp 12 | -------------------------------------------------------------------------------- /test/integration/navigation_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class NavigationTest < ActiveSupport::IntegrationCase 4 | test "truth" do 5 | assert_kind_of Dummy::Application, Rails.application 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /config/locales/en.ckeditor.yml: -------------------------------------------------------------------------------- 1 | en: 2 | ckeditor: 3 | page_title: "CKEditor Files Manager" 4 | confirm_delete: "Delete file?" 5 | buttons: 6 | cancel: "Cancel" 7 | upload: "Upload" 8 | delete: "Delete" 9 | -------------------------------------------------------------------------------- /test/support/integration_case.rb: -------------------------------------------------------------------------------- 1 | # Define a bare test case to use with Capybara 2 | class ActiveSupport::IntegrationCase < ActiveSupport::TestCase 3 | include Capybara::DSL 4 | include Rails.application.routes.url_helpers 5 | end 6 | -------------------------------------------------------------------------------- /config/locales/ru.ckeditor.yml: -------------------------------------------------------------------------------- 1 | ru: 2 | ckeditor: 3 | page_title: "CKEditor Загрузка файлов" 4 | confirm_delete: "Удалить файл?" 5 | buttons: 6 | cancel: "Отмена" 7 | upload: "Загрузить" 8 | delete: "Удалить" 9 | -------------------------------------------------------------------------------- /config/locales/uk.ckeditor.yml: -------------------------------------------------------------------------------- 1 | uk: 2 | ckeditor: 3 | page_title: "CKEditor Завантаження файлів" 4 | confirm_delete: "Видалити файл?" 5 | buttons: 6 | cancel: "Відміна" 7 | upload: "Завантажити" 8 | delete: "Видалити" 9 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/models/mongoid/ckeditor/asset.rb: -------------------------------------------------------------------------------- 1 | require 'mime/types' 2 | 3 | class Ckeditor::Asset 4 | include Ckeditor::Orm::Mongoid::AssetBase 5 | 6 | attr_accessible :data, :assetable_type, :assetable_id, :assetable 7 | end 8 | -------------------------------------------------------------------------------- /test/dummy/config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Sample localization file for English. Add more files in this directory for other locales. 2 | # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. 3 | 4 | en: 5 | hello: "Hello world" 6 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | # Mime::Type.register_alias "text/html", :iphone 6 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/attachment/lang/uk.js: -------------------------------------------------------------------------------- 1 | CKEDITOR.plugins.setLang('attachment', 'uk', 2 | { 3 | attachment : 4 | { 5 | title : "Вставити файл", 6 | url: "URL", 7 | name: "Назва", 8 | button : "Вставити" 9 | } 10 | }); 11 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/attachment/lang/ru.js: -------------------------------------------------------------------------------- 1 | CKEDITOR.plugins.setLang('attachment', 'ru', 2 | { 3 | attachment : 4 | { 5 | title : "Включить вложения", 6 | url: "URL", 7 | name: "Название", 8 | button : "Вставить" 9 | } 10 | }); 11 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/models/active_record/ckeditor/asset.rb: -------------------------------------------------------------------------------- 1 | require 'mime/types' 2 | 3 | class Ckeditor::Asset < ActiveRecord::Base 4 | include Ckeditor::Orm::ActiveRecord::AssetBase 5 | 6 | attr_accessible :data, :assetable_type, :assetable_id, :assetable 7 | end 8 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/attachment/lang/en.js: -------------------------------------------------------------------------------- 1 | CKEDITOR.plugins.setLang('attachment', 'en', 2 | { 3 | attachment : 4 | { 5 | title : "Insert attachment", 6 | url: "URL", 7 | name: "Title", 8 | button : "Insert attachment" 9 | } 10 | }); 11 | -------------------------------------------------------------------------------- /test/dummy/Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require File.expand_path('../config/application', __FILE__) 5 | require 'rake' 6 | 7 | Dummy::Application.load_tasks 8 | -------------------------------------------------------------------------------- /test/dummy/config/boot.rb: -------------------------------------------------------------------------------- 1 | begin 2 | require File.expand_path("../../../../.bundle/environment", __FILE__) 3 | rescue LoadError 4 | require 'rubygems' 5 | require 'bundler' 6 | Bundler.setup :default, :test, CKEDITOR_ORM 7 | end 8 | 9 | $:.unshift File.expand_path('../../../../lib', __FILE__) 10 | -------------------------------------------------------------------------------- /test/dummy/app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Dummy 5 | <%= stylesheet_link_tag :all %> 6 | <%= javascript_include_tag :defaults, :ckeditor %> 7 | <%= csrf_meta_tag %> 8 | 9 | 10 | 11 | <%= yield %> 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /test/ckeditor_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CkeditorTest < ActiveSupport::TestCase 4 | test "truth" do 5 | assert_kind_of Module, Ckeditor 6 | end 7 | 8 | test 'setup block yields self' do 9 | Ckeditor.setup do |config| 10 | assert_equal Ckeditor, config 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /test/dummy/script/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. 3 | 4 | APP_PATH = File.expand_path('../../config/application', __FILE__) 5 | require File.expand_path('../../config/boot', __FILE__) 6 | require 'rails/commands' 7 | -------------------------------------------------------------------------------- /lib/ckeditor/helpers/form_builder.rb: -------------------------------------------------------------------------------- 1 | module Ckeditor 2 | module Helpers 3 | module FormBuilder 4 | extend ActiveSupport::Concern 5 | 6 | def cktext_area(method, options = {}) 7 | @template.send("cktext_area", @object_name, method, objectify_options(options)) 8 | end 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /test/dummy/db/migrate/20110623120047_create_posts.rb: -------------------------------------------------------------------------------- 1 | class CreatePosts < ActiveRecord::Migration 2 | def self.up 3 | create_table :posts do |t| 4 | t.string :title 5 | t.text :info 6 | t.text :content 7 | t.timestamps 8 | end 9 | end 10 | 11 | def self.down 12 | drop_table :posts 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/embed/lang/en.js: -------------------------------------------------------------------------------- 1 | CKEDITOR.plugins.setLang('embed', 'en', 2 | { 3 | embed : 4 | { 5 | title : "Paste embed", 6 | button : "Paste embed", 7 | pasteMsg : "Please, paste embed-code from Youtube, Myspace, Flickr and others sources into rectangle, using the keyboard (Ctrl + V), and click OK." 8 | } 9 | }); 10 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/embed/lang/uk.js: -------------------------------------------------------------------------------- 1 | CKEDITOR.plugins.setLang('embed', 'uk', 2 | { 3 | embed : 4 | { 5 | title : "Вставити embed", 6 | button : "Вставити embed", 7 | pasteMsg : "Будь ласка, вставте embed-код з Youtube, Myspace, Flickr та інших ресурсів в прямокутник, використовуючи (Ctrl+V), та нажміть OK." 8 | } 9 | }); 10 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/embed/lang/ru.js: -------------------------------------------------------------------------------- 1 | CKEDITOR.plugins.setLang('embed', 'ru', 2 | { 3 | embed : 4 | { 5 | title : "Вставить embed", 6 | button : "Вставить embed", 7 | pasteMsg : "Пожалуйста, вставьте embed-код с Youtube, Myspace, Flickr и других ресурсов в прямоугольник, используя сочетание клавиш (Ctrl+V), и нажмите OK." 8 | } 9 | }); 10 | -------------------------------------------------------------------------------- /test/dummy/app/models/post.rb: -------------------------------------------------------------------------------- 1 | case CKEDITOR_ORM 2 | 3 | when :active_record 4 | class Post < ActiveRecord::Base 5 | validates_presence_of :title, :info, :content 6 | end 7 | 8 | when :mongoid 9 | class Post 10 | include Mongoid::Document 11 | field :title 12 | field :info 13 | field :content 14 | validates_presence_of :title, :info, :content 15 | end 16 | 17 | end 18 | -------------------------------------------------------------------------------- /lib/ckeditor/hooks/formtastic.rb: -------------------------------------------------------------------------------- 1 | module Ckeditor 2 | module Hooks 3 | module FormtasticBuilder 4 | def self.included(base) 5 | base.send(:include, InstanceMethods) 6 | end 7 | 8 | module InstanceMethods 9 | def ckeditor_input(method, options) 10 | basic_input_helper(:cktext_area, :text, method, options) 11 | end 12 | end 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format 4 | # (all these examples are active by default): 5 | # ActiveSupport::Inflector.inflections do |inflect| 6 | # inflect.plural /^(ox)$/i, '\1en' 7 | # inflect.singular /^(ox)en/i, '\1' 8 | # inflect.irregular 'person', 'people' 9 | # inflect.uncountable %w( fish sheep ) 10 | # end 11 | -------------------------------------------------------------------------------- /test/dummy/config/database.yml: -------------------------------------------------------------------------------- 1 | development: 2 | adapter: sqlite3 3 | database: db/development.sqlite3 4 | 5 | # Warning: The database defined as "test" will be erased and 6 | # re-generated from your development database when you run "rake". 7 | # Do not set this db to the same as development or production. 8 | test: 9 | adapter: sqlite3 10 | database: db/test.sqlite3 11 | 12 | production: 13 | adapter: sqlite3 14 | database: db/production.sqlite3 15 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. 4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } 5 | 6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. 7 | # Rails.backtrace_cleaner.remove_silencers! 8 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session' 4 | 5 | # Use the database for sessions instead of the cookie-based default, 6 | # which shouldn't be used to store highly confidential information 7 | # (create the session table with "rails generate session_migration") 8 | # Dummy::Application.config.session_store :active_record_store 9 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/embed/plugin.js: -------------------------------------------------------------------------------- 1 | (function(){var embedCmd={exec:function(editor){editor.openDialog('embed');return}};CKEDITOR.plugins.add('embed',{lang:['en','ru','uk'],requires:['dialog'],init:function(editor){var commandName='embed';editor.addCommand(commandName,embedCmd);editor.ui.addButton('Embed',{label:editor.lang.embed.button,command:commandName,icon:this.path+"images/embed.png"});CKEDITOR.dialog.add(commandName,CKEDITOR.getUrl(this.path+'dialogs/embed.js'))}})})(); 2 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/attachment/plugin.js: -------------------------------------------------------------------------------- 1 | (function(){var attachmentCmd={exec:function(editor){editor.openDialog('attachment');return}};CKEDITOR.plugins.add('attachment',{lang:['en','ru','uk'],requires:['dialog'],init:function(editor){var commandName='attachment';editor.addCommand(commandName,attachmentCmd);editor.ui.addButton('Attachment',{label:editor.lang.attachment.button,command:commandName,icon:this.path+"images/attachment.png"});CKEDITOR.dialog.add(commandName,CKEDITOR.getUrl(this.path+'dialogs/attachment.js'))}})})(); 2 | -------------------------------------------------------------------------------- /app/views/ckeditor/pictures/index.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 | <%= link_to I18n.t(:upload, :scope => [:ckeditor, :buttons]), '#', :class => "add" %> 5 |
6 |
7 | 8 |
9 | <%= render :partial => 'ckeditor/shared/asset', :collection => @pictures %> 10 |
11 |
12 | 13 | 16 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/secret_token.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Your secret key for verifying the integrity of signed cookies. 4 | # If you change this key, all old signed cookies will become invalid! 5 | # Make sure the secret is at least 30 characters and all random, 6 | # no regular words or you'll be exposed to dictionary attacks. 7 | Dummy::Application.config.secret_token = '75e91f008f9d9c3f8d2a4d7d8f6ac3256d6422530e5b70a743a3c0c40e8a2a14b27913cea9b901f807e987d58211b21bf9f8beb0cf4cec2125f21e3b6720d9d1' 8 | -------------------------------------------------------------------------------- /app/views/ckeditor/attachment_files/index.html.erb: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 | <%= link_to I18n.t(:upload, :scope => [:ckeditor, :buttons]), '#', :class => "add" %> 5 |
6 |
7 | 8 |
9 | <%= render :partial => 'ckeditor/shared/asset', :collection => @attachments %> 10 |
11 |
12 | 13 | 16 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/models/active_record/ckeditor/picture.rb: -------------------------------------------------------------------------------- 1 | class Ckeditor::Picture < Ckeditor::Asset 2 | has_attached_file :data, 3 | :url => "/ckeditor_assets/pictures/:id/:style_:basename.:extension", 4 | :path => ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension", 5 | :styles => { :content => '800>', :thumb => '118x100#' } 6 | 7 | validates_attachment_size :data, :less_than => 2.megabytes 8 | validates_attachment_presence :data 9 | 10 | def url_content 11 | url(:content) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/controllers/ckeditor/pictures_controller.rb: -------------------------------------------------------------------------------- 1 | class Ckeditor::PicturesController < Ckeditor::BaseController 2 | 3 | def index 4 | @pictures = Ckeditor.picture_model.find_all(ckeditor_pictures_scope) 5 | respond_with(@pictures) 6 | end 7 | 8 | def create 9 | @picture = Ckeditor::Picture.new 10 | respond_with_asset(@picture) 11 | end 12 | 13 | def destroy 14 | @picture.destroy 15 | respond_with(@picture, :location => ckeditor_pictures_path) 16 | end 17 | 18 | protected 19 | 20 | def find_asset 21 | @picture = Ckeditor.picture_model.get!(params[:id]) 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/models/mongoid/ckeditor/picture.rb: -------------------------------------------------------------------------------- 1 | class Ckeditor::Picture < Ckeditor::Asset 2 | has_mongoid_attached_file :data, 3 | :url => "/ckeditor_assets/pictures/:id/:style_:basename.:extension", 4 | :path => ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension", 5 | :styles => { :content => '800>', :thumb => '118x100#' } 6 | 7 | validates_attachment_size :data, :less_than => 2.megabytes 8 | validates_attachment_presence :data 9 | 10 | def url_content 11 | url(:content) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/models/active_record/ckeditor/attachment_file.rb: -------------------------------------------------------------------------------- 1 | class Ckeditor::AttachmentFile < Ckeditor::Asset 2 | has_attached_file :data, 3 | :url => "/ckeditor_assets/attachments/:id/:filename", 4 | :path => ":rails_root/public/ckeditor_assets/attachments/:id/:filename" 5 | 6 | validates_attachment_size :data, :less_than => 100.megabytes 7 | validates_attachment_presence :data 8 | 9 | def url_thumb 10 | @url_thumb ||= begin 11 | extname = File.extname(filename).gsub(/^\./, '') 12 | "/javascripts/ckeditor/filebrowser/images/thumbs/#{extname}.gif" 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /app/controllers/ckeditor/attachment_files_controller.rb: -------------------------------------------------------------------------------- 1 | class Ckeditor::AttachmentFilesController < Ckeditor::BaseController 2 | 3 | def index 4 | @attachments = Ckeditor.attachment_file_model.find_all(ckeditor_attachment_files_scope) 5 | respond_with(@attachments) 6 | end 7 | 8 | def create 9 | @attachment = Ckeditor::AttachmentFile.new 10 | respond_with_asset(@attachment) 11 | end 12 | 13 | def destroy 14 | @attachment.destroy 15 | respond_with(@attachment, :location => ckeditor_attachment_files_path) 16 | end 17 | 18 | protected 19 | 20 | def find_asset 21 | @attachment = Ckeditor.attachment_file_model.get!(params[:id]) 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gemspec 4 | 5 | gem "rails", "3.0.9" 6 | 7 | platforms :mri_18 do 8 | group :test do 9 | gem 'ruby-debug' 10 | end 11 | end 12 | 13 | platforms :ruby do 14 | gem "sqlite3" 15 | 16 | group :development do 17 | gem "unicorn", "~> 4.0.1" 18 | end 19 | 20 | group :development, :test do 21 | gem "capybara", ">= 0.4.0" 22 | gem "mynyml-redgreen", "~> 0.7.1", :require => 'redgreen' 23 | end 24 | 25 | group :active_record do 26 | gem "paperclip", "~> 2.3.12" 27 | end 28 | 29 | group :mongoid do 30 | gem "mongoid" 31 | gem "bson_ext" 32 | gem 'mongoid-paperclip', :require => 'mongoid_paperclip' 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/ckeditor/helpers/view_helper.rb: -------------------------------------------------------------------------------- 1 | module Ckeditor 2 | module Helpers 3 | module ViewHelper 4 | extend ActiveSupport::Concern 5 | 6 | def cktext_area_tag(name, content = nil, options = {}) 7 | element_id = sanitize_to_id(name) 8 | options = { :language => I18n.locale.to_s }.merge(options) 9 | input_html = { :id => element_id }.merge( options.delete(:input_html) || {} ) 10 | 11 | output_buffer = ActiveSupport::SafeBuffer.new 12 | output_buffer << text_area_tag(name, content, input_html) 13 | output_buffer << javascript_tag(Utils.js_replace(element_id, options)) 14 | 15 | output_buffer 16 | end 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/models/mongoid/ckeditor/attachment_file.rb: -------------------------------------------------------------------------------- 1 | class Ckeditor::AttachmentFile < Ckeditor::Asset 2 | has_mongoid_attached_file :data, 3 | :url => "/ckeditor_assets/attachments/:id/:filename", 4 | :path => ":rails_root/public/ckeditor_assets/attachments/:id/:filename" 5 | 6 | validates_attachment_size :data, :less_than => 100.megabytes 7 | validates_attachment_presence :data 8 | 9 | def url_thumb 10 | @url_thumb ||= begin 11 | extname = File.extname(filename).gsub(/^\./, '') 12 | "/javascripts/ckeditor/filebrowser/images/thumbs/#{extname}.gif" 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # encoding: UTF-8 2 | require 'rubygems' 3 | begin 4 | require 'bundler/setup' 5 | rescue LoadError 6 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks' 7 | end 8 | 9 | require 'rake' 10 | require 'rdoc/task' 11 | 12 | require 'rake/testtask' 13 | 14 | Rake::TestTask.new(:test) do |t| 15 | t.libs << 'lib' 16 | t.libs << 'test' 17 | t.pattern = 'test/**/*_test.rb' 18 | t.verbose = false 19 | end 20 | 21 | task :default => :test 22 | 23 | RDoc::Task.new do |rdoc| 24 | rdoc.rdoc_dir = 'rdoc' 25 | rdoc.title = 'Ckeditor' 26 | rdoc.options << '--line-numbers' << '--inline-source' 27 | rdoc.rdoc_files.include('README.rdoc') 28 | rdoc.rdoc_files.include('lib/**/*.rb') 29 | end 30 | -------------------------------------------------------------------------------- /test/dummy/app/views/posts/index.html.erb: -------------------------------------------------------------------------------- 1 |

Listing posts

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | <% @posts.each do |post| %> 11 | 12 | 13 | 14 | 15 | 16 | <% end %> 17 |
<%= link_to 'Show', post %><%= link_to 'Edit', edit_post_path(post) %><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>
18 | 19 |
20 | 21 | <%= link_to 'New Post', new_post_path %> 22 | 23 |

Text area test

24 | <%= cktext_area_tag("test_area", "Ckeditor") %> 25 | 26 |

Text area test with options

27 | <%= cktext_area_tag("content", "Ckeditor", :input_html => {:cols => 10, :rows => 20}, :toolbar => 'Easy') %> 28 | -------------------------------------------------------------------------------- /test/dummy/config/initializers/ckeditor.rb: -------------------------------------------------------------------------------- 1 | # Use this hook to configure ckeditor 2 | if Object.const_defined?("Ckeditor") 3 | Ckeditor.setup do |config| 4 | # ==> ORM configuration 5 | # Load and configure the ORM. Supports :active_record (default), :mongo_mapper and 6 | # :mongoid (bson_ext recommended) by default. Other ORMs may be 7 | # available as additional gems. 8 | require "ckeditor/orm/#{CKEDITOR_ORM}" 9 | 10 | # Allowed image file types for upload. 11 | # Set to nil or [] (empty array) for all file types 12 | # config.image_file_types = ["jpg", "jpeg", "png", "gif", "tiff"] 13 | 14 | # Allowed attachment file types for upload. 15 | # Set to nil or [] (empty array) for all file types 16 | # config.attachment_file_types = ["doc", "docx", "rar", "zip", "xls", "swf"] 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /test/dummy/public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 17 | 18 | 19 | 20 | 21 |
22 |

The change you wanted was rejected.

23 |

Maybe you tried to change something you didn't have access to.

24 |
25 | 26 | 27 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor.rb: -------------------------------------------------------------------------------- 1 | # Use this hook to configure ckeditor 2 | if Object.const_defined?("Ckeditor") 3 | Ckeditor.setup do |config| 4 | # ==> ORM configuration 5 | # Load and configure the ORM. Supports :active_record (default), :mongo_mapper and 6 | # :mongoid (bson_ext recommended) by default. Other ORMs may be 7 | # available as additional gems. 8 | require "ckeditor/orm/<%= options[:orm] %>" 9 | 10 | 11 | # Allowed image file types for upload. 12 | # Set to nil or [] (empty array) for all file types 13 | # config.image_file_types = ["jpg", "jpeg", "png", "gif", "tiff"] 14 | 15 | # Allowed attachment file types for upload. 16 | # Set to nil or [] (empty array) for all file types 17 | # config.attachment_file_types = ["doc", "docx", "rar", "zip", "xls", "swf"] 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /test/dummy/public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 17 | 18 | 19 | 20 | 21 |
22 |

The page you were looking for doesn't exist.

23 |

You may have mistyped the address or the page may have moved.

24 |
25 | 26 | 27 | -------------------------------------------------------------------------------- /test/dummy/public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 17 | 18 | 19 | 20 | 21 |
22 |

We're sorry, but something went wrong.

23 |

We've been notified about this issue and we'll take a look at it shortly.

24 |
25 | 26 | 27 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/embed/dialogs/embed.js: -------------------------------------------------------------------------------- 1 | (function(){CKEDITOR.dialog.add('embed',function(editor){return{title:editor.lang.embed.title,minWidth:CKEDITOR.env.ie&&CKEDITOR.env.quirks?368:350,minHeight:240,onShow:function(){this.getContentElement('general','content').getInputElement().setValue('')},onOk:function(){var text=this.getContentElement('general','content').getInputElement().getValue();this.getParentEditor().insertHtml(text)},contents:[{label:editor.lang.common.generalTab,id:'general',elements:[{type:'html',id:'pasteMsg',html:'
'+editor.lang.embed.pasteMsg+'
'},{type:'html',id:'content',style:'width:340px;height:170px',html:'',focus:function(){this.getElement().focus()}}]}]}})})(); 2 | -------------------------------------------------------------------------------- /lib/ckeditor/helpers/controllers.rb: -------------------------------------------------------------------------------- 1 | module Ckeditor 2 | module Helpers 3 | module Controllers 4 | extend ActiveSupport::Concern 5 | 6 | protected 7 | 8 | def ckeditor_authenticate 9 | return true 10 | end 11 | 12 | def ckeditor_before_create_asset(asset) 13 | asset.assetable = current_user if respond_to?(:current_user) 14 | return true 15 | end 16 | 17 | def ckeditor_pictures_scope(options = {}) 18 | ckeditor_filebrowser_scope(options) 19 | end 20 | 21 | def ckeditor_attachment_files_scope(options = {}) 22 | ckeditor_filebrowser_scope(options) 23 | end 24 | 25 | def ckeditor_filebrowser_scope(options = {}) 26 | { :order => [:id, :desc] }.merge(options) 27 | end 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /test/dummy/db/migrate/20110705195648_create_ckeditor_assets.rb: -------------------------------------------------------------------------------- 1 | class CreateCkeditorAssets < ActiveRecord::Migration 2 | def self.up 3 | create_table :ckeditor_assets do |t| 4 | t.string :data_file_name, :null => false 5 | t.string :data_content_type 6 | t.integer :data_file_size 7 | 8 | t.integer :assetable_id 9 | t.string :assetable_type, :limit => 30 10 | t.string :type, :limit => 30 11 | 12 | # Uncomment it to save images dimensions, if your need it 13 | # t.integer :width 14 | # t.integer :height 15 | 16 | t.timestamps 17 | end 18 | 19 | add_index "ckeditor_assets", ["assetable_type", "type", "assetable_id"], :name => "idx_ckeditor_assetable_type" 20 | add_index "ckeditor_assets", ["assetable_type", "assetable_id"], :name => "idx_ckeditor_assetable" 21 | end 22 | 23 | def self.down 24 | drop_table :ckeditor_assets 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /app/views/ckeditor/shared/_asset_tmpl.html.erb: -------------------------------------------------------------------------------- 1 | 22 | -------------------------------------------------------------------------------- /ckeditor.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "ckeditor/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "ckeditor" 7 | s.version = Ckeditor::Version::GEM.dup 8 | s.platform = Gem::Platform::RUBY 9 | s.summary = "Rails gem for easy integration ckeditor in your application" 10 | s.description = "CKEditor is a WYSIWYG editor to be used inside web pages" 11 | s.authors = ["Igor Galeta"] 12 | s.email = "galeta.igor@gmail.com" 13 | s.rubyforge_project = "ckeditor" 14 | s.homepage = "https://github.com/galetahub/ckeditor" 15 | 16 | s.files = Dir["{app,lib,config}/**/*"] + ["MIT-LICENSE", "Rakefile", "Gemfile", "README.rdoc"] 17 | s.test_files = Dir["{test}/**/*"] 18 | s.extra_rdoc_files = ["README.rdoc"] 19 | s.require_paths = ["lib"] 20 | 21 | s.add_dependency("mime-types", "~> 1.16") 22 | s.add_dependency("orm_adapter", "~> 0.0.5") 23 | end 24 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/models/active_record/migration.rb: -------------------------------------------------------------------------------- 1 | class CreateCkeditorAssets < ActiveRecord::Migration 2 | def self.up 3 | create_table :ckeditor_assets do |t| 4 | t.string :data_file_name, :null => false 5 | t.string :data_content_type 6 | t.integer :data_file_size 7 | 8 | t.integer :assetable_id 9 | t.string :assetable_type, :limit => 30 10 | t.string :type, :limit => 30 11 | 12 | # Uncomment it to save images dimensions, if your need it 13 | # t.integer :width 14 | # t.integer :height 15 | 16 | t.timestamps 17 | end 18 | 19 | add_index "ckeditor_assets", ["assetable_type", "type", "assetable_id"], :name => "idx_ckeditor_assetable_type" 20 | add_index "ckeditor_assets", ["assetable_type", "assetable_id"], :name => "idx_ckeditor_assetable" 21 | end 22 | 23 | def self.down 24 | drop_table :ckeditor_assets 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /test/dummy/app/views/posts/_form.html.erb: -------------------------------------------------------------------------------- 1 | <%= form_for(@post) do |f| %> 2 | <% if @post.errors.any? %> 3 |
4 |

<%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:

5 | 6 | 11 |
12 | <% end %> 13 | 14 | <%= f.label :title %> 15 | <%= f.text_field :title %> 16 | 17 | <%= f.label :content %> 18 | <%= f.cktext_area :content, :width => 800, :height => 400 %> 19 | 20 | <%= f.label :info %> 21 | <% if @post.new_record? %> 22 | <%= cktext_area :post, :info, :input_html => { :value => "Defaults info content" } %> 23 | <% else %> 24 | <%= cktext_area :post, :info, :input_html => { :cols => 50, :rows => 70 } %> 25 | <% end %> 26 | 27 |
28 | <%= f.submit %> 29 |
30 | <% end %> 31 | -------------------------------------------------------------------------------- /test/generators/install_generator_test.rb: -------------------------------------------------------------------------------- 1 | require "test_helper" 2 | 3 | class InstallGeneratorTest < Rails::Generators::TestCase 4 | tests Ckeditor::Generators::InstallGenerator 5 | destination File.expand_path("../../tmp", __FILE__) 6 | setup :prepare_destination 7 | 8 | test "Assert all files are properly created" do 9 | run_generator %w(--orm=active_record) 10 | 11 | assert_file "config/initializers/ckeditor.rb", /require "ckeditor\/orm\/active_record"/ 12 | 13 | assert_file "tmp/ckeditor_#{Ckeditor::Version::EDITOR}.tar.gz" 14 | 15 | ["rails.js", "jquery.js", "fileuploader.js", "jquery.tmpl.js"].each do |file| 16 | assert_file "public/javascripts/ckeditor/filebrowser/javascripts/#{file}" 17 | end 18 | end 19 | 20 | test "Assert configurator is valid for mongoid" do 21 | run_generator %w(--orm=mongoid) 22 | 23 | assert_file "config/initializers/ckeditor.rb", /require "ckeditor\/orm\/mongoid"/ 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /app/views/ckeditor/shared/_asset.html.erb: -------------------------------------------------------------------------------- 1 | <%= content_tag(:div, :id => dom_id(asset), :class => "gal-item", :"data-url" => asset.url_content) do %> 2 | <%= link_to image_tag("/javascripts/ckeditor/filebrowser/images/gal_del.png", :title => I18n.t('ckeditor.buttons.delete')), 3 | polymorphic_path(asset, :format => :json), 4 | :remote => true, 5 | :method => :delete, 6 | :confirm => t('ckeditor.confirm_delete'), 7 | :class => "fileupload-cancel gal-del" %> 8 | 9 |
10 |
<%= image_tag(asset.url_thumb, :title => asset.filename) %>
11 |
12 |
<%= asset.filename %>
13 |
14 |
<%= asset.format_created_at %>
15 |
<%= number_to_human_size(asset.size) %>
16 |
17 |
18 |
19 | <% end %> 20 | -------------------------------------------------------------------------------- /lib/ckeditor/hooks/simple_form.rb: -------------------------------------------------------------------------------- 1 | module Ckeditor 2 | module Hooks 3 | module SimpleFormBuilder 4 | class CkeditorInput < ::SimpleForm::Inputs::Base 5 | def input 6 | @builder.send(:cktext_area, attribute_name, input_html_options) 7 | end 8 | end 9 | 10 | def self.included(base) 11 | base.send(:include, InstanceMethods) 12 | end 13 | 14 | module InstanceMethods 15 | def ckeditor(attribute_name, options={}, &block) 16 | column = find_attribute_column(attribute_name) 17 | input_type = default_input_type(attribute_name, column, options) 18 | 19 | if block_given? 20 | SimpleForm::Inputs::BlockInput.new(self, attribute_name, column, input_type, options, &block).render 21 | else 22 | CkeditorInput.new(self, attribute_name, column, input_type, options).render 23 | end 24 | end 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/ckeditor/helpers/form_helper.rb: -------------------------------------------------------------------------------- 1 | module Ckeditor 2 | module Helpers 3 | module FormHelper 4 | extend ActiveSupport::Concern 5 | 6 | include ActionView::Helpers::TagHelper 7 | include ActionView::Helpers::JavaScriptHelper 8 | 9 | def cktext_area(object_name, method, options = {}) 10 | options = { :language => I18n.locale.to_s }.merge(options) 11 | input_html = (options.delete(:input_html) || {}) 12 | hash = input_html.stringify_keys 13 | 14 | instance_tag = ActionView::Base::InstanceTag.new(object_name, method, self, options.delete(:object)) 15 | instance_tag.send(:add_default_name_and_id, hash) 16 | 17 | output_buffer = ActiveSupport::SafeBuffer.new 18 | output_buffer << instance_tag.to_text_area_tag(input_html) 19 | output_buffer << javascript_tag(Utils.js_replace(hash['id'], options)) 20 | 21 | output_buffer 22 | end 23 | end 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /lib/ckeditor/engine.rb: -------------------------------------------------------------------------------- 1 | require 'rails' 2 | 3 | module Ckeditor 4 | class Engine < ::Rails::Engine 5 | config.action_view.javascript_expansions[:ckeditor] = "ckeditor/ckeditor" 6 | 7 | initializer "ckeditor.helpers" do 8 | ActiveSupport.on_load(:action_controller) do 9 | ActionController::Base.send :include, Ckeditor::Helpers::Controllers 10 | end 11 | 12 | ActiveSupport.on_load :action_view do 13 | ActionView::Base.send :include, Ckeditor::Helpers::ViewHelper 14 | ActionView::Base.send :include, Ckeditor::Helpers::FormHelper 15 | ActionView::Helpers::FormBuilder.send :include, Ckeditor::Helpers::FormBuilder 16 | end 17 | end 18 | 19 | initializer "ckeditor.hooks" do 20 | if Object.const_defined?("Formtastic") 21 | ::Formtastic::SemanticFormBuilder.send :include, Ckeditor::Hooks::FormtasticBuilder 22 | end 23 | 24 | if Object.const_defined?("SimpleForm") 25 | ::SimpleForm::FormBuilder.send :include, Ckeditor::Hooks::SimpleFormBuilder 26 | end 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /test/generators/models_generator_test.rb: -------------------------------------------------------------------------------- 1 | require "test_helper" 2 | 3 | class ModelsGeneratorTest < Rails::Generators::TestCase 4 | tests Ckeditor::Generators::ModelsGenerator 5 | destination File.expand_path("../../tmp", __FILE__) 6 | setup :prepare_destination 7 | 8 | test "models and migration for active_record orm" do 9 | run_generator %w(--orm=active_record) 10 | 11 | assert_file "app/models/ckeditor/asset.rb" 12 | assert_file "app/models/ckeditor/picture.rb" 13 | assert_file "app/models/ckeditor/attachment_file.rb" 14 | 15 | assert_migration "db/migrate/create_ckeditor_assets.rb" do |migration| 16 | assert_class_method :up, migration do |up| 17 | assert_match /create_table/, up 18 | end 19 | end 20 | end 21 | 22 | test "models for mongoid orm" do 23 | run_generator %w(--orm=mongoid) 24 | 25 | assert_file "app/models/ckeditor/asset.rb" 26 | assert_file "app/models/ckeditor/picture.rb" 27 | assert_file "app/models/ckeditor/attachment_file.rb" 28 | 29 | assert_no_migration "db/migrate/create_ckeditor_assets.rb" 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2011 AIMBULANCE 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /test/dummy/config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb 3 | 4 | # In the development environment your application's code is reloaded on 5 | # every request. This slows down response time but is perfect for development 6 | # since you don't have to restart the webserver when you make code changes. 7 | config.cache_classes = false 8 | 9 | # Log error messages when you accidentally call methods on nil. 10 | config.whiny_nils = true 11 | 12 | # Show full error reports and disable caching 13 | config.consider_all_requests_local = true 14 | config.action_view.debug_rjs = true 15 | config.action_controller.perform_caching = false 16 | 17 | # Don't care if the mailer can't send 18 | config.action_mailer.raise_delivery_errors = false 19 | 20 | # Print deprecation notices to the Rails logger 21 | config.active_support.deprecation = :log 22 | 23 | # Only use best-standards-support built into browsers 24 | config.action_dispatch.best_standards_support = :builtin 25 | end 26 | 27 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # Configure Rails Envinronment 2 | ENV["RAILS_ENV"] = "test" 3 | CKEDITOR_ORM = (ENV["CKEDITOR_ORM"] || :active_record).to_sym 4 | 5 | puts "\n==> Ckeditor.orm = #{CKEDITOR_ORM.inspect}. You can change orm in environment variable CKEDITOR_ORM" 6 | 7 | require File.expand_path("../dummy/config/environment.rb", __FILE__) 8 | require "rails/test_help" 9 | require 'redgreen' 10 | 11 | ActionMailer::Base.delivery_method = :test 12 | ActionMailer::Base.perform_deliveries = true 13 | ActionMailer::Base.default_url_options[:host] = "test.com" 14 | 15 | Rails.backtrace_cleaner.remove_silencers! 16 | 17 | # Configure capybara for integration testing 18 | require "capybara/rails" 19 | Capybara.default_driver = :rack_test 20 | Capybara.default_selector = :css 21 | 22 | # Run specific orm operations 23 | require "orm/#{CKEDITOR_ORM}" 24 | 25 | # Load support files 26 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } 27 | 28 | # For generators 29 | require "rails/generators/test_case" 30 | require "generators/ckeditor/install_generator" 31 | require "generators/ckeditor/models_generator" 32 | -------------------------------------------------------------------------------- /test/dummy/public/stylesheets/scaffold.css: -------------------------------------------------------------------------------- 1 | body { background-color: #fff; color: #333; } 2 | 3 | body, p, ol, ul, td { 4 | font-family: verdana, arial, helvetica, sans-serif; 5 | font-size: 13px; 6 | line-height: 18px; 7 | } 8 | 9 | pre { 10 | background-color: #eee; 11 | padding: 10px; 12 | font-size: 11px; 13 | } 14 | 15 | a { color: #000; } 16 | a:visited { color: #666; } 17 | a:hover { color: #fff; background-color:#000; } 18 | 19 | div.field, div.actions { 20 | margin-bottom: 10px; 21 | } 22 | 23 | #notice { 24 | color: green; 25 | } 26 | 27 | .field_with_errors { 28 | padding: 2px; 29 | background-color: red; 30 | display: table; 31 | } 32 | 33 | #error_explanation { 34 | width: 450px; 35 | border: 2px solid red; 36 | padding: 7px; 37 | padding-bottom: 0; 38 | margin-bottom: 20px; 39 | background-color: #f0f0f0; 40 | } 41 | 42 | #error_explanation h2 { 43 | text-align: left; 44 | font-weight: bold; 45 | padding: 5px 5px 5px 15px; 46 | font-size: 12px; 47 | margin: -7px; 48 | margin-bottom: 0px; 49 | background-color: #c00; 50 | color: #fff; 51 | } 52 | 53 | #error_explanation ul li { 54 | font-size: 12px; 55 | list-style: square; 56 | } 57 | -------------------------------------------------------------------------------- /app/controllers/ckeditor/base_controller.rb: -------------------------------------------------------------------------------- 1 | class Ckeditor::BaseController < ApplicationController 2 | respond_to :html, :json 3 | layout "ckeditor" 4 | 5 | before_filter :set_locale 6 | before_filter :find_asset, :only => [:destroy] 7 | before_filter :ckeditor_authenticate 8 | 9 | if Rails.version.split('.').first.to_i >= 3 10 | skip_before_filter :verify_authenticity_token 11 | end 12 | 13 | protected 14 | 15 | def set_locale 16 | if !params[:langCode].blank? && I18n.available_locales.include?(params[:langCode].to_sym) 17 | I18n.locale = params[:langCode] 18 | end 19 | end 20 | 21 | def respond_with_asset(asset) 22 | file = params[:CKEditor].blank? ? params[:qqfile] : params[:upload] 23 | asset.data = Ckeditor::Http.normalize_param(file, request) 24 | 25 | callback = ckeditor_before_create_asset(asset) 26 | 27 | if callback && asset.save 28 | body = params[:CKEditor].blank? ? asset.to_json(:only=>[:id, :type]) : %Q"" 31 | 32 | render :text => body 33 | else 34 | render :nothing => true 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /app/views/layouts/ckeditor.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | <%= csrf_meta_tag %> 7 | <%= I18n.t('page_title', :scope => [:ckeditor]) %> 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 21 | 22 | <%= yield %> 23 | <%= render :partial => 'ckeditor/shared/asset_tmpl' %> 24 | 25 | 26 | -------------------------------------------------------------------------------- /test/controllers/pictures_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class PicturesControllerTest < ActionController::TestCase 4 | tests Ckeditor::PicturesController 5 | 6 | def setup 7 | @image = fixture_file_upload('files/rails.png', 'image/png') 8 | end 9 | 10 | def teardown 11 | Ckeditor::Picture.destroy_all 12 | end 13 | 14 | test "index action" do 15 | get :index 16 | 17 | assert_equal 200, @response.status 18 | assert_template "ckeditor/pictures/index" 19 | end 20 | 21 | test "create action via filebrowser" do 22 | assert_difference 'Ckeditor::Picture.count' do 23 | post :create, :qqfile => @image 24 | end 25 | 26 | assert_equal 200, @response.status 27 | end 28 | 29 | test "create action via CKEditor upload form" do 30 | assert_difference 'Ckeditor::Picture.count' do 31 | post :create, :upload => @image, :CKEditor => 'ckeditor_field' 32 | end 33 | 34 | assert_equal 200, @response.status 35 | end 36 | 37 | test "invalid params for create action" do 38 | assert_no_difference 'Ckeditor::Picture.count' do 39 | post :create, :qqfile => nil 40 | end 41 | end 42 | 43 | test "destroy action via filebrowser" do 44 | @picture = Ckeditor::Picture.create :data => @image 45 | 46 | assert_difference 'Ckeditor::Picture.count', -1 do 47 | delete :destroy, :id => @picture.id 48 | end 49 | 50 | assert_equal 302, @response.status 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/ckeditor.rb: -------------------------------------------------------------------------------- 1 | require 'orm_adapter' 2 | 3 | module Ckeditor 4 | IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg', 'image/pjpeg', 'image/tiff', 'image/x-png'] 5 | 6 | autoload :Utils, 'ckeditor/utils' 7 | autoload :Http, 'ckeditor/http' 8 | 9 | module Helpers 10 | autoload :ViewHelper, 'ckeditor/helpers/view_helper' 11 | autoload :FormHelper, 'ckeditor/helpers/form_helper' 12 | autoload :FormBuilder, 'ckeditor/helpers/form_builder' 13 | autoload :Controllers, 'ckeditor/helpers/controllers' 14 | end 15 | 16 | module Hooks 17 | autoload :FormtasticBuilder, 'ckeditor/hooks/formtastic' 18 | autoload :SimpleFormBuilder, 'ckeditor/hooks/simple_form' 19 | end 20 | 21 | # Allowed image file types for upload. 22 | # Set to nil or [] (empty array) for all file types 23 | mattr_accessor :image_file_types 24 | @@image_file_types = ["jpg", "jpeg", "png", "gif", "tiff"] 25 | 26 | # Allowed attachment file types for upload. 27 | # Set to nil or [] (empty array) for all file types 28 | mattr_accessor :attachment_file_types 29 | @@attachment_file_types = ["doc", "docx", "rar", "zip", "xls", "swf"] 30 | 31 | # Default way to setup Ckeditor. Run rails generate ckeditor to create 32 | # a fresh initializer with all configuration values. 33 | def self.setup 34 | yield self 35 | end 36 | 37 | def self.picture_model 38 | Ckeditor::Picture.to_adapter 39 | end 40 | 41 | def self.attachment_file_model 42 | Ckeditor::AttachmentFile.to_adapter 43 | end 44 | end 45 | 46 | require 'ckeditor/engine' 47 | require 'ckeditor/version' 48 | -------------------------------------------------------------------------------- /test/controllers/attachment_files_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class AttachmentFilesControllerTest < ActionController::TestCase 4 | tests Ckeditor::AttachmentFilesController 5 | 6 | def setup 7 | @attachment = fixture_file_upload('files/rails.tar.gz', 'application/x-gzip') 8 | end 9 | 10 | def teardown 11 | Ckeditor::AttachmentFile.destroy_all 12 | end 13 | 14 | test "index action" do 15 | get :index 16 | 17 | assert_equal 200, @response.status 18 | assert_template "ckeditor/attachment_files/index" 19 | end 20 | 21 | test "create action via filebrowser" do 22 | assert_difference 'Ckeditor::AttachmentFile.count' do 23 | post :create, :qqfile => @attachment 24 | end 25 | 26 | assert_equal 200, @response.status 27 | end 28 | 29 | test "create action via CKEditor upload form" do 30 | assert_difference 'Ckeditor::AttachmentFile.count' do 31 | post :create, :upload => @attachment, :CKEditor => 'ckeditor_field' 32 | end 33 | 34 | assert_equal 200, @response.status 35 | end 36 | 37 | test "invalid params for create action" do 38 | assert_no_difference 'Ckeditor::AttachmentFile.count' do 39 | post :create, :qqfile => nil 40 | end 41 | end 42 | 43 | test "destroy action via filebrowser" do 44 | @attachment_file = Ckeditor::AttachmentFile.create :data => @attachment 45 | 46 | assert_difference 'Ckeditor::AttachmentFile.count', -1 do 47 | delete :destroy, :id => @attachment_file.id 48 | end 49 | 50 | assert_equal 302, @response.status 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /test/dummy/config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb 3 | 4 | # The test environment is used exclusively to run your application's 5 | # test suite. You never need to work with it otherwise. Remember that 6 | # your test database is "scratch space" for the test suite and is wiped 7 | # and recreated between test runs. Don't rely on the data there! 8 | config.cache_classes = true 9 | 10 | # Log error messages when you accidentally call methods on nil. 11 | config.whiny_nils = true 12 | 13 | # Show full error reports and disable caching 14 | config.consider_all_requests_local = true 15 | config.action_controller.perform_caching = false 16 | 17 | # Raise exceptions instead of rendering exception templates 18 | config.action_dispatch.show_exceptions = false 19 | 20 | # Disable request forgery protection in test environment 21 | config.action_controller.allow_forgery_protection = false 22 | 23 | # Tell Action Mailer not to deliver emails to the real world. 24 | # The :test delivery method accumulates sent emails in the 25 | # ActionMailer::Base.deliveries array. 26 | config.action_mailer.delivery_method = :test 27 | 28 | # Use SQL instead of Active Record's schema dumper when creating the test database. 29 | # This is necessary if your schema can't be completely dumped by the schema dumper, 30 | # like if you have constraints or database-specific column types 31 | # config.active_record.schema_format = :sql 32 | 33 | # Print deprecation notices to the stderr 34 | config.active_support.deprecation = :stderr 35 | end 36 | -------------------------------------------------------------------------------- /test/dummy/db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # Note that this schema.rb definition is the authoritative source for your 6 | # database schema. If you need to create the application database on another 7 | # system, you should be using db:schema:load, not running all the migrations 8 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 9 | # you'll amass, the slower it'll run and the greater likelihood for issues). 10 | # 11 | # It's strongly recommended to check this file into your version control system. 12 | 13 | ActiveRecord::Schema.define(:version => 20110705195648) do 14 | 15 | create_table "ckeditor_assets", :force => true do |t| 16 | t.string "data_file_name", :null => false 17 | t.string "data_content_type" 18 | t.integer "data_file_size" 19 | t.integer "assetable_id" 20 | t.string "assetable_type", :limit => 30 21 | t.string "type", :limit => 30 22 | t.datetime "created_at" 23 | t.datetime "updated_at" 24 | end 25 | 26 | add_index "ckeditor_assets", ["assetable_type", "assetable_id"], :name => "idx_ckeditor_assetable" 27 | add_index "ckeditor_assets", ["assetable_type", "type", "assetable_id"], :name => "idx_ckeditor_assetable_type" 28 | 29 | create_table "posts", :force => true do |t| 30 | t.string "title" 31 | t.text "content" 32 | t.datetime "created_at" 33 | t.datetime "updated_at" 34 | end 35 | 36 | end 37 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/models_generator.rb: -------------------------------------------------------------------------------- 1 | require 'rails/generators' 2 | require 'rails/generators/migration' 3 | 4 | module Ckeditor 5 | module Generators 6 | class ModelsGenerator < Rails::Generators::Base 7 | include Rails::Generators::Migration 8 | 9 | desc "Generates migration for Asset (Picture, AttachmentFile) models" 10 | 11 | # ORM configuration 12 | class_option :orm, :type => :string, :default => "active_record", 13 | :desc => "Backend processor for upload support" 14 | 15 | def self.source_root 16 | @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates', 'models/')) 17 | end 18 | 19 | def self.next_migration_number(dirname) 20 | Time.now.strftime("%Y%m%d%H%M%S") 21 | end 22 | 23 | def create_models 24 | template "#{generator_dir}/ckeditor/asset.rb", 25 | File.join('app/models', ckeditor_dir, "asset.rb") 26 | 27 | template "#{generator_dir}/ckeditor/picture.rb", 28 | File.join('app/models', ckeditor_dir, "picture.rb") 29 | 30 | template "#{generator_dir}/ckeditor/attachment_file.rb", 31 | File.join('app/models', ckeditor_dir, "attachment_file.rb") 32 | end 33 | 34 | def create_migration 35 | if options[:orm] == "active_record" 36 | migration_template "#{generator_dir}/migration.rb", File.join('db/migrate', "create_ckeditor_assets.rb") 37 | end 38 | end 39 | 40 | protected 41 | 42 | def ckeditor_dir 43 | 'ckeditor' 44 | end 45 | 46 | def generator_dir 47 | options[:orm] || "active_record" 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /test/integration/posts_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class PostsTest < ActiveSupport::IntegrationCase 4 | def setup 5 | @post = Post.create!(:title => "test", :content => "content", :info => "info") 6 | end 7 | 8 | def teardown 9 | @post.destroy 10 | end 11 | 12 | test "include javascripts" do 13 | visit(posts_path) 14 | 15 | assert page.body.include?('/javascripts/ckeditor/ckeditor.js') 16 | assert page.body.include?("CKEDITOR.replace('test_area', { language: 'en' });") 17 | end 18 | 19 | test "pass text_area with options" do 20 | visit(posts_path) 21 | 22 | assert page.body.include?('') 23 | assert page.body.include?("CKEDITOR.replace('content', { language: 'en',toolbar: 'Easy' });") 24 | end 25 | 26 | test "form builder helper" do 27 | visit(new_post_path) 28 | 29 | assert page.body.include?('') 30 | assert page.body.include?("CKEDITOR.replace('post_content', { height: 400,language: 'en',width: 800 });") 31 | assert page.body.include?('') 32 | assert page.body.include?("CKEDITOR.replace('post_info', { language: 'en' });") 33 | end 34 | 35 | test "text_area value" do 36 | visit(edit_post_path(@post)) 37 | 38 | assert page.body.include?('') 39 | assert page.body.include?('') 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /test/dummy/config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Dummy::Application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb 3 | 4 | # The production environment is meant for finished, "live" apps. 5 | # Code is not reloaded between requests 6 | config.cache_classes = true 7 | 8 | # Full error reports are disabled and caching is turned on 9 | config.consider_all_requests_local = false 10 | config.action_controller.perform_caching = true 11 | 12 | # Specifies the header that your server uses for sending files 13 | config.action_dispatch.x_sendfile_header = "X-Sendfile" 14 | 15 | # For nginx: 16 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' 17 | 18 | # If you have no front-end server that supports something like X-Sendfile, 19 | # just comment this out and Rails will serve the files 20 | 21 | # See everything in the log (default is :info) 22 | # config.log_level = :debug 23 | 24 | # Use a different logger for distributed setups 25 | # config.logger = SyslogLogger.new 26 | 27 | # Use a different cache store in production 28 | # config.cache_store = :mem_cache_store 29 | 30 | # Disable Rails's static asset server 31 | # In production, Apache or nginx will already do this 32 | config.serve_static_assets = false 33 | 34 | # Enable serving of images, stylesheets, and javascripts from an asset server 35 | # config.action_controller.asset_host = "http://assets.example.com" 36 | 37 | # Disable delivery errors, bad email addresses will be ignored 38 | # config.action_mailer.raise_delivery_errors = false 39 | 40 | # Enable threaded mode 41 | # config.threadsafe! 42 | 43 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 44 | # the I18n.default_locale when a translation can not be found) 45 | config.i18n.fallbacks = true 46 | 47 | # Send deprecation notices to registered listeners 48 | config.active_support.deprecation = :notify 49 | end 50 | -------------------------------------------------------------------------------- /test/dummy/config/application.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../boot', __FILE__) 2 | 3 | require "active_model/railtie" 4 | require "active_record/railtie" 5 | require "action_controller/railtie" 6 | require "action_view/railtie" 7 | require "action_mailer/railtie" 8 | 9 | Bundler.require :default, CKEDITOR_ORM 10 | 11 | require "ckeditor" 12 | 13 | module Dummy 14 | class Application < Rails::Application 15 | # Settings in config/environments/* take precedence over those specified here. 16 | # Application configuration should go into files in config/initializers 17 | # -- all .rb files in that directory are automatically loaded. 18 | 19 | # Custom directories with classes and modules you want to be autoloadable. 20 | config.autoload_paths += %W(#{config.root}/../../lib/generators/ckeditor/templates/models/#{CKEDITOR_ORM}) 21 | 22 | # Only load the plugins named here, in the order given (default is alphabetical). 23 | # :all can be used as a placeholder for all plugins not explicitly named. 24 | # config.plugins = [ :exception_notification, :ssl_requirement, :all ] 25 | 26 | # Activate observers that should always be running. 27 | # config.active_record.observers = :cacher, :garbage_collector, :forum_observer 28 | 29 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 30 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 31 | # config.time_zone = 'Central Time (US & Canada)' 32 | 33 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 34 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 35 | # config.i18n.default_locale = :de 36 | 37 | # JavaScript files you want as :defaults (application.js is always included). 38 | # config.action_view.javascript_expansions[:defaults] = %w(jquery rails) 39 | 40 | # Configure the default encoding used in templates for Ruby 1.9. 41 | config.encoding = "utf-8" 42 | 43 | # Configure sensitive parameters which will be filtered from the log file. 44 | config.filter_parameters += [:password] 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/ckeditor/http.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require 'digest/sha1' 3 | require 'mime/types' 4 | 5 | module Ckeditor 6 | module Http 7 | # Create tempfile from hash 8 | class UploadedFile 9 | attr_accessor :original_filename, :content_type, :tempfile, :headers 10 | 11 | def initialize(hash) 12 | @original_filename = hash[:filename] 13 | @content_type = hash[:type] 14 | @headers = hash[:head] 15 | @tempfile = hash[:tempfile] 16 | raise(ArgumentError, ':tempfile is required') unless @tempfile 17 | end 18 | 19 | def open 20 | @tempfile.open 21 | end 22 | 23 | def path 24 | @tempfile.path 25 | end 26 | 27 | def read(*args) 28 | @tempfile.read(*args) 29 | end 30 | 31 | def rewind 32 | @tempfile.rewind 33 | end 34 | 35 | def size 36 | @tempfile.size 37 | end 38 | end 39 | 40 | # Usage (paperclip example) 41 | # @asset.data = QqFile.new(params[:qqfile], request) 42 | class QqFile < ::Tempfile 43 | 44 | def initialize(filename, request, tmpdir = Dir::tmpdir) 45 | @original_filename = filename 46 | @request = request 47 | 48 | super Digest::SHA1.hexdigest(filename), tmpdir 49 | fetch 50 | end 51 | 52 | def fetch 53 | self.write @request.raw_post 54 | self.rewind 55 | self 56 | end 57 | 58 | def original_filename 59 | @original_filename 60 | end 61 | 62 | def content_type 63 | types = MIME::Types.type_for(@request.content_type) 64 | types.empty? ? @request.content_type : types.first.to_s 65 | end 66 | end 67 | 68 | # Convert nested Hash to HashWithIndifferentAccess and replace 69 | # file upload hash with UploadedFile objects 70 | def self.normalize_param(*args) 71 | value = args.first 72 | if Hash === value && value.has_key?(:tempfile) 73 | UploadedFile.new(value) 74 | elsif value.is_a?(String) 75 | QqFile.new(*args) 76 | else 77 | value 78 | end 79 | end 80 | end 81 | end 82 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/posts_controller.rb: -------------------------------------------------------------------------------- 1 | class PostsController < ApplicationController 2 | # GET /posts 3 | # GET /posts.xml 4 | def index 5 | @posts = Post.all 6 | 7 | respond_to do |format| 8 | format.html # index.html.erb 9 | format.xml { render :xml => @posts } 10 | end 11 | end 12 | 13 | # GET /posts/1 14 | # GET /posts/1.xml 15 | def show 16 | @post = Post.find(params[:id]) 17 | 18 | respond_to do |format| 19 | format.html # show.html.erb 20 | format.xml { render :xml => @post } 21 | end 22 | end 23 | 24 | # GET /posts/new 25 | # GET /posts/new.xml 26 | def new 27 | @post = Post.new 28 | 29 | respond_to do |format| 30 | format.html # new.html.erb 31 | format.xml { render :xml => @post } 32 | end 33 | end 34 | 35 | # GET /posts/1/edit 36 | def edit 37 | @post = Post.find(params[:id]) 38 | end 39 | 40 | # POST /posts 41 | # POST /posts.xml 42 | def create 43 | @post = Post.new(params[:post]) 44 | 45 | respond_to do |format| 46 | if @post.save 47 | format.html { redirect_to(@post, :notice => 'Post was successfully created.') } 48 | format.xml { render :xml => @post, :status => :created, :location => @post } 49 | else 50 | format.html { render :action => "new" } 51 | format.xml { render :xml => @post.errors, :status => :unprocessable_entity } 52 | end 53 | end 54 | end 55 | 56 | # PUT /posts/1 57 | # PUT /posts/1.xml 58 | def update 59 | @post = Post.find(params[:id]) 60 | 61 | respond_to do |format| 62 | if @post.update_attributes(params[:post]) 63 | format.html { redirect_to(@post, :notice => 'Post was successfully updated.') } 64 | format.xml { head :ok } 65 | else 66 | format.html { render :action => "edit" } 67 | format.xml { render :xml => @post.errors, :status => :unprocessable_entity } 68 | end 69 | end 70 | end 71 | 72 | # DELETE /posts/1 73 | # DELETE /posts/1.xml 74 | def destroy 75 | @post = Post.find(params[:id]) 76 | @post.destroy 77 | 78 | respond_to do |format| 79 | format.html { redirect_to(posts_url) } 80 | format.xml { head :ok } 81 | end 82 | end 83 | end 84 | -------------------------------------------------------------------------------- /test/routes_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class CkeditorRoutingTest < ActionController::TestCase 4 | test "should route to pictures" do 5 | assert_generates "/ckeditor/pictures", { :controller => "ckeditor/pictures", :action => "index"} 6 | assert_generates "/ckeditor/pictures/1", { :controller => "ckeditor/pictures", :action => "destroy", :id => 1} 7 | end 8 | 9 | test "should route to attachment_files" do 10 | assert_generates "/ckeditor/attachment_files", { :controller => "ckeditor/attachment_files", :action => "index"} 11 | assert_generates "/ckeditor/attachment_files/1", { :controller => "ckeditor/attachment_files", :action => "destroy", :id => 1} 12 | end 13 | 14 | test 'map index pictures' do 15 | assert_named_route "/ckeditor/pictures", :ckeditor_pictures_path 16 | assert_recognizes({:controller=>"ckeditor/pictures", :action=>"index"}, {:path => '/ckeditor/pictures', :method => :get}) 17 | end 18 | 19 | test 'map create picture' do 20 | assert_recognizes({:controller => 'ckeditor/pictures', :action => 'create'}, {:path => '/ckeditor/pictures', :method => :post}) 21 | end 22 | 23 | test 'map destroy picture' do 24 | assert_named_route "/ckeditor/pictures/1", :ckeditor_picture_path, 1 25 | assert_recognizes({:controller => 'ckeditor/pictures', :action => 'destroy', :id => "1"}, {:path => '/ckeditor/pictures/1', :method => :delete}) 26 | end 27 | 28 | test 'map index attachment_files' do 29 | assert_named_route "/ckeditor/attachment_files", :ckeditor_attachment_files_path 30 | assert_recognizes({:controller => 'ckeditor/attachment_files', :action => 'index'}, {:path => '/ckeditor/attachment_files', :method => :get}) 31 | end 32 | 33 | test 'map create attachment_file' do 34 | assert_recognizes({:controller => 'ckeditor/attachment_files', :action => 'create'}, {:path => '/ckeditor/attachment_files', :method => :post}) 35 | end 36 | 37 | test 'map destroy attachment_file' do 38 | assert_named_route "/ckeditor/attachment_files/1", :ckeditor_attachment_file_path, 1 39 | assert_recognizes({:controller => 'ckeditor/attachment_files', :action => 'destroy', :id => "1"}, {:path => '/ckeditor/attachment_files/1', :method => :delete}) 40 | end 41 | 42 | protected 43 | 44 | def assert_named_route(result, *args) 45 | assert_equal result, @routes.url_helpers.send(*args) 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/install_generator.rb: -------------------------------------------------------------------------------- 1 | require 'rails/generators' 2 | require 'fileutils' 3 | 4 | module Ckeditor 5 | module Generators 6 | class InstallGenerator < Rails::Generators::Base 7 | class_option :version, :type => :string, :default => Ckeditor::Version::EDITOR, 8 | :desc => "Version of ckeditor which be install" 9 | 10 | class_option :orm, :type => :string, :default => 'active_record', 11 | :desc => "Backend processor for upload support" 12 | 13 | desc "Download and install ckeditor" 14 | 15 | def self.source_root 16 | @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates')) 17 | end 18 | 19 | # copy configuration 20 | def copy_initializer 21 | template "ckeditor.rb", "config/initializers/ckeditor.rb" 22 | end 23 | 24 | # copy ckeditor files 25 | def install_ckeditor 26 | say_status("fetching #{filename}", "", :green) 27 | get(download_url, "tmp/#{filename}") 28 | 29 | filepath = Rails.root.join("tmp/#{filename}") 30 | 31 | if File.exist?(filepath) 32 | Ckeditor::Utils.extract(filepath, Rails.root.join('public', 'javascripts')) 33 | directory "ckeditor", "public/javascripts/ckeditor" 34 | FileUtils.rm_rf(filepath) 35 | end 36 | end 37 | 38 | def download_javascripts 39 | js_dir = "public/javascripts/ckeditor/filebrowser/javascripts" 40 | 41 | say_status("fetching rails.js", "", :green) 42 | get "https://github.com/rails/jquery-ujs/raw/master/src/rails.js", "#{js_dir}/rails.js" 43 | 44 | say_status("fetching fileuploader.js", "", :green) 45 | get "https://raw.github.com/galetahub/file-uploader/master/client/fileuploader.js", "#{js_dir}/fileuploader.js" 46 | 47 | say_status("fetching jquery-1.6.1.min.js", "", :green) 48 | get "http://code.jquery.com/jquery-1.6.1.min.js", "#{js_dir}/jquery.js" 49 | 50 | say_status("fetching jquery.tmpl.min.js", "", :green) 51 | get "https://raw.github.com/jquery/jquery-tmpl/master/jquery.tmpl.min.js", "#{js_dir}/jquery.tmpl.js" 52 | end 53 | 54 | protected 55 | 56 | def download_url 57 | "http://download.cksource.com/CKEditor/CKEditor/CKEditor%20#{options[:version]}/#{filename}" 58 | end 59 | 60 | def filename 61 | "ckeditor_#{options[:version]}.tar.gz" 62 | end 63 | end 64 | end 65 | end 66 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/config.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. 3 | For licensing, see LICENSE.html or http://ckeditor.com/license 4 | */ 5 | 6 | CKEDITOR.editorConfig = function( config ) 7 | { 8 | // Define changes to default configuration here. For example: 9 | // config.language = 'fr'; 10 | // config.uiColor = '#AADC6E'; 11 | 12 | /* Filebrowser routes */ 13 | // The location of an external file browser, that should be launched when "Browse Server" button is pressed. 14 | config.filebrowserBrowseUrl = "/ckeditor/attachment_files"; 15 | 16 | // The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Flash dialog. 17 | config.filebrowserFlashBrowseUrl = "/ckeditor/attachment_files"; 18 | 19 | // The location of a script that handles file uploads in the Flash dialog. 20 | config.filebrowserFlashUploadUrl = "/ckeditor/attachment_files"; 21 | 22 | // The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Link tab of Image dialog. 23 | config.filebrowserImageBrowseLinkUrl = "/ckeditor/pictures"; 24 | 25 | // The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Image dialog. 26 | config.filebrowserImageBrowseUrl = "/ckeditor/pictures"; 27 | 28 | // The location of a script that handles file uploads in the Image dialog. 29 | config.filebrowserImageUploadUrl = "/ckeditor/pictures"; 30 | 31 | // The location of a script that handles file uploads. 32 | config.filebrowserUploadUrl = "/ckeditor/attachment_files"; 33 | 34 | /* Extra plugins */ 35 | // works only with en, ru, uk locales 36 | config.extraPlugins = "embed,attachment"; 37 | 38 | /* Toolbars */ 39 | config.toolbar = 'Easy'; 40 | 41 | config.toolbar_Easy = 42 | [ 43 | ['Source','-','Preview'], 44 | ['Cut','Copy','Paste','PasteText','PasteFromWord',], 45 | ['Undo','Redo','-','SelectAll','RemoveFormat'], 46 | ['Styles','Format'], ['Subscript', 'Superscript', 'TextColor'], ['Maximize','-','About'], '/', 47 | ['Bold','Italic','Underline','Strike'], ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'], 48 | ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'], 49 | ['Link','Unlink','Anchor'], ['Image', 'Attachment', 'Flash', 'Embed'], 50 | ['Table','HorizontalRule','Smiley','SpecialChar','PageBreak'] 51 | ]; 52 | }; 53 | -------------------------------------------------------------------------------- /lib/ckeditor/utils.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | module Ckeditor 3 | module Utils 4 | class << self 5 | def escape_single_quotes(str) 6 | str.gsub('\\','\0\0').gsub(' true, :element => "fileupload" }.merge(options) 36 | 37 | case uploader_type.to_s.downcase 38 | when "image" then 39 | options[:action] = "^EDITOR.config.filebrowserImageUploadUrl" 40 | options[:allowedExtensions] = Ckeditor.image_file_types 41 | when "flash" then 42 | options[:action] = "^EDITOR.config.filebrowserFlashUploadUrl" 43 | options[:allowedExtensions] = ["swf"] 44 | else 45 | options[:action] = "^EDITOR.config.filebrowserUploadUrl" 46 | options[:allowedExtensions] = Ckeditor.attachment_file_types 47 | end 48 | 49 | js_options = applay_options(options) 50 | 51 | "$(document).ready(function(){ new qq.FileUploaderInput({ #{js_options} }); });" 52 | end 53 | 54 | def applay_options(options) 55 | str = [] 56 | 57 | options.each do |key, value| 58 | item = case value 59 | when String then 60 | value.split(//).first == '^' ? value.slice(1..-1) : "'#{value}'" 61 | when Hash then 62 | "{ #{applay_options(value)} }" 63 | when Array then 64 | arr = value.collect { |v| "'#{v}'" } 65 | "[ #{arr.join(',')} ]" 66 | else value 67 | end 68 | 69 | str << "#{key}: #{item}" 70 | end 71 | 72 | str.sort.join(',') 73 | end 74 | end 75 | end 76 | end 77 | -------------------------------------------------------------------------------- /lib/ckeditor/orm/mongoid.rb: -------------------------------------------------------------------------------- 1 | require 'orm_adapter/adapters/mongoid' 2 | 3 | module Ckeditor 4 | module Orm 5 | module Mongoid 6 | module AssetBase 7 | def self.included(base) 8 | base.send(:include, InstanceMethods) 9 | base.send(:include, ::Mongoid::Document) 10 | base.send(:include, ::Mongoid::Timestamps) 11 | base.send(:include, ::Mongoid::Paperclip) 12 | base.send(:extend, ClassMethods) 13 | end 14 | 15 | module ClassMethods 16 | def self.extended(base) 17 | base.class_eval do 18 | belongs_to :assetable, :polymorphic => true 19 | 20 | before_validation :extract_content_type 21 | before_create :read_dimensions, :parameterize_filename 22 | 23 | delegate :url, :path, :styles, :size, :content_type, :to => :data 24 | end 25 | end 26 | end 27 | 28 | module InstanceMethods 29 | def filename 30 | data_file_name 31 | end 32 | 33 | def format_created_at 34 | I18n.l(created_at, :format => "%d.%m.%Y") 35 | end 36 | 37 | def has_dimensions? 38 | respond_to?(:width) && respond_to?(:height) 39 | end 40 | 41 | def image? 42 | Ckeditor::IMAGE_TYPES.include?(data_content_type) 43 | end 44 | 45 | def geometry 46 | @geometry ||= Paperclip::Geometry.from_file(data.to_file) 47 | @geometry 48 | end 49 | 50 | def url_content 51 | url 52 | end 53 | 54 | def url_thumb 55 | url(:thumb) 56 | end 57 | 58 | def as_json(options = nil) 59 | options = { 60 | :methods => [:url_content, :url_thumb, :size, :filename, :format_created_at], 61 | :root => "asset" 62 | }.merge(options || {}) 63 | 64 | super options 65 | end 66 | 67 | protected 68 | 69 | def parameterize_filename 70 | unless data_file_name.blank? 71 | filename = Ckeditor::Utils.parameterize_filename(data_file_name) 72 | self.data.instance_write(:file_name, filename) 73 | end 74 | end 75 | 76 | def read_dimensions 77 | if image? && has_dimensions? 78 | self.width = geometry.width 79 | self.height = geometry.height 80 | end 81 | end 82 | 83 | # Extract content_type from filename using mime/types gem 84 | def extract_content_type 85 | if data_content_type == "application/octet-stream" && !data_file_name.blank? 86 | content_types = MIME::Types.type_for(data_file_name) 87 | self.data_content_type = content_types.first.to_s unless content_types.empty? 88 | end 89 | end 90 | end 91 | end 92 | end 93 | end 94 | end 95 | -------------------------------------------------------------------------------- /lib/ckeditor/orm/active_record.rb: -------------------------------------------------------------------------------- 1 | module Ckeditor 2 | module Orm 3 | module ActiveRecord 4 | module AssetBase 5 | def self.included(base) 6 | base.send(:include, InstanceMethods) 7 | base.send(:extend, ClassMethods) 8 | end 9 | 10 | module ClassMethods 11 | def self.extended(base) 12 | base.class_eval do 13 | set_table_name "ckeditor_assets" 14 | 15 | belongs_to :assetable, :polymorphic => true 16 | 17 | before_validation :extract_content_type 18 | before_create :read_dimensions, :parameterize_filename 19 | 20 | delegate :url, :path, :styles, :size, :content_type, :to => :data 21 | end 22 | end 23 | end 24 | 25 | module InstanceMethods 26 | def filename 27 | data_file_name 28 | end 29 | 30 | def format_created_at 31 | I18n.l(created_at, :format => "%d.%m.%Y") 32 | end 33 | 34 | def has_dimensions? 35 | respond_to?(:width) && respond_to?(:height) 36 | end 37 | 38 | def image? 39 | Ckeditor::IMAGE_TYPES.include?(data_content_type) 40 | end 41 | 42 | def geometry 43 | @geometry ||= Paperclip::Geometry.from_file(data.to_file) 44 | @geometry 45 | end 46 | 47 | def url_content 48 | url 49 | end 50 | 51 | def url_thumb 52 | url(:thumb) 53 | end 54 | 55 | def as_json(options = nil) 56 | options = { 57 | :methods => [:url_content, :url_thumb, :size, :filename, :format_created_at], 58 | :root => "asset" 59 | }.merge(options || {}) 60 | 61 | super options 62 | end 63 | 64 | protected 65 | 66 | def parameterize_filename 67 | unless data_file_name.blank? 68 | filename = Ckeditor::Utils.parameterize_filename(data_file_name) 69 | self.data.instance_write(:file_name, filename) 70 | end 71 | end 72 | 73 | def read_dimensions 74 | if image? && has_dimensions? 75 | self.width = geometry.width 76 | self.height = geometry.height 77 | end 78 | end 79 | 80 | # Extract content_type from filename using mime/types gem 81 | def extract_content_type 82 | if data_content_type == "application/octet-stream" && !data_file_name.blank? 83 | content_types = MIME::Types.type_for(data_file_name) 84 | self.data_content_type = content_types.first.to_s unless content_types.empty? 85 | end 86 | end 87 | end 88 | end 89 | end 90 | end 91 | end 92 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/stylesheets/uploader.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 0; 3 | margin: 0; 4 | font-family: Arial, Helvetica, sans-serif; 5 | } 6 | .gal-holder { 7 | display: block; 8 | overflow: hidden; 9 | padding: 20px; 10 | } 11 | .gal-holder .gal-item { 12 | display: block; 13 | float: left; 14 | width: 150px; 15 | overflow: hidden; 16 | position: relative; 17 | margin: 0 12px 12px 0; 18 | } 19 | .gal-holder .gal-item .gal-del { 20 | display: block; 21 | position: absolute; 22 | right: 0; 23 | top: 0; 24 | } 25 | .gal-holder .gal-item .gal-del:hover { 26 | -moz-opacity: 0.7; 27 | -khtml-opacity: 0.7; 28 | opacity: 0.7; 29 | } 30 | .gal-holder .gal-item .gal-upload-holder .add { 31 | display: block; 32 | color: #2e5aff; 33 | background: url(/javascripts/ckeditor/filebrowser/images/gal_add.png) no-repeat left center; 34 | padding-left: 33px; 35 | line-height: 25px; 36 | text-decoration: none; 37 | margin: 50px 0 0 10px; 38 | font-size: 14px; 39 | } 40 | .gal-holder .gal-item .gal-upload-holder .add:hover { 41 | text-decoration: underline; 42 | } 43 | .gal-holder .gal-item .gal-inner-holder, .gal-holder .gal-item .gal-upload-holder { 44 | display: block; 45 | margin: 6px 6px 0 0; 46 | overflow: hidden; 47 | padding: 12px; 48 | background: #fff; 49 | border: solid 1px #D3D3D3; 50 | min-height: 138px; 51 | } 52 | .gal-holder .gal-item .gal-inner-holder.hover { 53 | background: #DFF1FF; 54 | border: solid 1px #99CCFF; 55 | } 56 | .gal-holder .gal-item .gal-inner-holder.selected { 57 | background: #B4D9FF; 58 | border: solid 1px #6565FE; 59 | } 60 | .gal-holder .gal-item .gal-inner-holder .img { 61 | height: 100px; 62 | overflow: hidden; 63 | cursor:pointer; 64 | } 65 | .gal-holder .gal-item .gal-inner-holder .img.preloader { 66 | padding: 27px 0 0 33px; 67 | height: 73px; 68 | } 69 | .gal-holder .gal-item .gal-inner-holder .img-data { 70 | display: block; 71 | overflow: hidden; 72 | padding-top: 5px; 73 | } 74 | .gal-holder .gal-item .gal-inner-holder .img-data .img-name { 75 | display: block; 76 | color: #333; 77 | padding-bottom: 5px; 78 | font-size: 12px; 79 | overflow: hidden; 80 | width: 500px; 81 | } 82 | .gal-holder .gal-item .gal-inner-holder .img-data .time-size { 83 | display: block; 84 | overflow: hidden; 85 | font-size: 10px; 86 | color: #666; 87 | } 88 | .gal-holder .gal-item .gal-inner-holder .img-data .time-size .time { 89 | display: block; 90 | float: left; 91 | } 92 | .gal-holder .gal-item .gal-inner-holder .img-data .time-size .size { 93 | display: block; 94 | float: right; 95 | } 96 | .gal-more { 97 | display: block; 98 | overflow: hidden; 99 | border-top: dashed 1px #d3d3d3; 100 | padding: 20px 0; 101 | text-align: center; 102 | } 103 | .gal-more a { 104 | color: #2e5aff; 105 | background: url(/javascripts/ckeditor/filebrowser/images/gal_more.gif) no-repeat left center; 106 | padding-left: 15px; 107 | font-size: 14px; 108 | text-decoration: none; 109 | } 110 | .gal-more a:hover { 111 | text-decoration: underline; 112 | } 113 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | ckeditor (3.6.0.pre) 5 | mime-types (~> 1.16) 6 | orm_adapter (~> 0.0.5) 7 | 8 | GEM 9 | remote: http://rubygems.org/ 10 | specs: 11 | abstract (1.0.0) 12 | actionmailer (3.0.9) 13 | actionpack (= 3.0.9) 14 | mail (~> 2.2.19) 15 | actionpack (3.0.9) 16 | activemodel (= 3.0.9) 17 | activesupport (= 3.0.9) 18 | builder (~> 2.1.2) 19 | erubis (~> 2.6.6) 20 | i18n (~> 0.5.0) 21 | rack (~> 1.2.1) 22 | rack-mount (~> 0.6.14) 23 | rack-test (~> 0.5.7) 24 | tzinfo (~> 0.3.23) 25 | activemodel (3.0.9) 26 | activesupport (= 3.0.9) 27 | builder (~> 2.1.2) 28 | i18n (~> 0.5.0) 29 | activerecord (3.0.9) 30 | activemodel (= 3.0.9) 31 | activesupport (= 3.0.9) 32 | arel (~> 2.0.10) 33 | tzinfo (~> 0.3.23) 34 | activeresource (3.0.9) 35 | activemodel (= 3.0.9) 36 | activesupport (= 3.0.9) 37 | activesupport (3.0.9) 38 | arel (2.0.10) 39 | bson (1.3.1) 40 | bson_ext (1.3.1) 41 | builder (2.1.2) 42 | capybara (1.0.0) 43 | mime-types (>= 1.16) 44 | nokogiri (>= 1.3.3) 45 | rack (>= 1.0.0) 46 | rack-test (>= 0.5.4) 47 | selenium-webdriver (~> 0.2.0) 48 | xpath (~> 0.1.4) 49 | childprocess (0.1.9) 50 | ffi (~> 1.0.6) 51 | cocaine (0.1.0) 52 | columnize (0.3.4) 53 | erubis (2.6.6) 54 | abstract (>= 1.0.0) 55 | ffi (1.0.9) 56 | i18n (0.5.0) 57 | json_pure (1.5.3) 58 | kgio (2.5.0) 59 | linecache (0.46) 60 | rbx-require-relative (> 0.0.4) 61 | mail (2.2.19) 62 | activesupport (>= 2.3.6) 63 | i18n (>= 0.4.0) 64 | mime-types (~> 1.16) 65 | treetop (~> 1.4.8) 66 | mime-types (1.16) 67 | mongo (1.3.1) 68 | bson (>= 1.3.1) 69 | mongoid (2.0.2) 70 | activemodel (~> 3.0) 71 | mongo (~> 1.3) 72 | tzinfo (~> 0.3.22) 73 | mongoid-paperclip (0.0.5) 74 | paperclip (~> 2.3.6) 75 | mynyml-redgreen (0.7.1) 76 | term-ansicolor (>= 1.0.4) 77 | nokogiri (1.4.6) 78 | orm_adapter (0.0.5) 79 | paperclip (2.3.12) 80 | activerecord (>= 2.3.0) 81 | activesupport (>= 2.3.2) 82 | cocaine (>= 0.0.2) 83 | polyglot (0.3.1) 84 | rack (1.2.3) 85 | rack-mount (0.6.14) 86 | rack (>= 1.0.0) 87 | rack-test (0.5.7) 88 | rack (>= 1.0) 89 | rails (3.0.9) 90 | actionmailer (= 3.0.9) 91 | actionpack (= 3.0.9) 92 | activerecord (= 3.0.9) 93 | activeresource (= 3.0.9) 94 | activesupport (= 3.0.9) 95 | bundler (~> 1.0) 96 | railties (= 3.0.9) 97 | railties (3.0.9) 98 | actionpack (= 3.0.9) 99 | activesupport (= 3.0.9) 100 | rake (>= 0.8.7) 101 | rdoc (~> 3.4) 102 | thor (~> 0.14.4) 103 | raindrops (0.7.0) 104 | rake (0.9.2) 105 | rbx-require-relative (0.0.5) 106 | rdoc (3.6.1) 107 | ruby-debug (0.10.4) 108 | columnize (>= 0.1) 109 | ruby-debug-base (~> 0.10.4.0) 110 | ruby-debug-base (0.10.4) 111 | linecache (>= 0.3) 112 | rubyzip (0.9.4) 113 | selenium-webdriver (0.2.1) 114 | childprocess (>= 0.1.7) 115 | ffi (>= 1.0.7) 116 | json_pure 117 | rubyzip 118 | sqlite3 (1.3.3) 119 | term-ansicolor (1.0.5) 120 | thor (0.14.6) 121 | treetop (1.4.9) 122 | polyglot (>= 0.3.1) 123 | tzinfo (0.3.28) 124 | unicorn (4.0.1) 125 | kgio (~> 2.4) 126 | rack 127 | raindrops (~> 0.6) 128 | xpath (0.1.4) 129 | nokogiri (~> 1.3) 130 | 131 | PLATFORMS 132 | ruby 133 | 134 | DEPENDENCIES 135 | bson_ext 136 | capybara (>= 0.4.0) 137 | ckeditor! 138 | mongoid 139 | mongoid-paperclip 140 | mynyml-redgreen (~> 0.7.1) 141 | paperclip (~> 2.3.12) 142 | rails (= 3.0.9) 143 | ruby-debug 144 | sqlite3 145 | unicorn (~> 4.0.1) 146 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | = Ckeditor 2 | 3 | CKEditor is a text editor to be used inside web pages. It's a WYSIWYG editor, which means that the text being edited on it looks as similar as possible to 4 | the results users have when publishing it. It brings to the web common editing features found on desktop editing applications like Microsoft Word and OpenOffice. 5 | 6 | == Features 7 | 8 | * Rails 3 integration 9 | * Files browser 10 | * HTML5 files uploader 11 | * Hooks for formtastic and simple_form forms generators 12 | * Extra plugins: attachment and embed. 13 | * Assets pagination (under construction) 14 | 15 | == Install 16 | 17 | gem "ckeditor", "~> 3.6.0" 18 | 19 | rails generate ckeditor:install 20 | 21 | You can pass version of ckeditor to download (http://ckeditor.com/download/releases): 22 | 23 | rails generate ckeditor:install --version=3.5.4 24 | 25 | Now generate models for store uploading files: 26 | 27 | rails generate ckeditor:models --orm=active_record 28 | 29 | Available orms: 30 | * active_record 31 | * mongoid (Thanks Dmitry Lihachev https://github.com/lda) 32 | * mongo_mapper (under construction) 33 | 34 | Autoload ckeditor models folder (application.rb): 35 | 36 | config.autoload_paths += %W(#{config.root}/app/models/ckeditor) 37 | 38 | === Dependencies 39 | 40 | For active_record orm is used paperclip gem (it's by default). 41 | 42 | gem "paperclip" 43 | 44 | For gem "carrierwave" shortly I will give an example. 45 | 46 | == Usage 47 | 48 | <%= javascript_include_tag :ckeditor %> 49 | 50 | cktext_area_tag("test_area", "Ckeditor is the best") 51 | 52 | cktext_area_tag("content", "Ckeditor", :input_html => {:cols => 10, :rows => 20}, :toolbar => 'Easy') 53 | 54 | * input_html - Options for text_area tag, others for ckeditor javascript configuration. 55 | * language - appends automatically, by default is I18n.locale 56 | * available ckeditor options - http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html 57 | 58 | For configure ckeditor default options check: 59 | 60 | public/javascripts/ckeditor/config.js 61 | 62 | This stylesheet use editor for displaying edit area: 63 | 64 | public/javascripts/ckeditor/contents.css 65 | 66 | FormBuilder helper for more usefully: 67 | 68 | <%= form_for @page do |form| -%> 69 | ... 70 | <%= form.cktext_area :notes, :toolbar => 'Full', :width => 800, :height => 400 %> 71 | ... 72 | <%= form.cktext_area :content, :input_html => { :value => "Default value" } %> 73 | ... 74 | <%= cktext_area :page, :info %> 75 | <% end -%> 76 | 77 | === AJAX 78 | 79 | Jquery sample: 80 | 81 | 90 | 91 | === Formtastic integration 92 | 93 | <%= form.input :content, :as => :ckeditor %> 94 | <%= form.input :content, :as => :ckeditor, :input_html => { :height => 400 } %> 95 | 96 | === SimpleForm integration 97 | 98 | <%= form.ckeditor :content, :label => false, :input_html => { :toolbar => 'Full' } %> 99 | 100 | === Default scope 101 | 102 | For example, you need split assets collection for each user. 103 | 104 | class ApplicationController < ActionController::Base 105 | 106 | protected 107 | 108 | def ckeditor_filebrowser_scope(options = {}) 109 | super { :assetable_id => current_user.id, :assetable_type => 'User' }.merge(options) 110 | end 111 | end 112 | 113 | If your wont filter only pictures or attachment_files - redefine methods "ckeditor_pictures_scope" or "ckeditor_attachment_files_scope" respectively. 114 | By default, both these methods call "ckeditor_filebrowser_scope" method: 115 | 116 | class ApplicationController < ActionController::Base 117 | 118 | protected 119 | 120 | def ckeditor_pictures_scope(options = {}) 121 | ckeditor_filebrowser_scope(options) 122 | end 123 | 124 | def ckeditor_attachment_files_scope(options = {}) 125 | ckeditor_filebrowser_scope(options) 126 | end 127 | end 128 | 129 | === Callbacks 130 | 131 | class ApplicationController < ActionController::Base 132 | 133 | protected 134 | 135 | # Cancan example 136 | def ckeditor_authenticate 137 | authorize! action_name, @asset 138 | end 139 | 140 | # Set current_user as assetable 141 | def ckeditor_before_create_asset(asset) 142 | asset.assetable = current_user 143 | return true 144 | end 145 | end 146 | 147 | 148 | == I18n 149 | 150 | en: 151 | ckeditor: 152 | page_title: "CKEditor Files Manager" 153 | confirm_delete: "Delete file?" 154 | buttons: 155 | cancel: "Cancel" 156 | upload: "Upload" 157 | delete: "Delete" 158 | 159 | == Tests 160 | 161 | rake test 162 | rake test CKEDITOR_ORM=mongoid 163 | 164 | This project rocks and uses MIT-LICENSE. 165 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/plugins/attachment/dialogs/attachment.js: -------------------------------------------------------------------------------- 1 | (function(){CKEDITOR.dialog.add('attachment',function(editor){var selectableTargets=/^(_(?:self|top|parent|blank))$/;var parseLink=function(editor,element){var href=element?(element.getAttribute('_cke_saved_href')||element.getAttribute('href')):'',emailMatch,anchorMatch,urlMatch,retval={};retval.type='url';retval.url=href;if(element){var target=element.getAttribute('target');retval.target={};if(target){var targetMatch=target.match(selectableTargets);if(targetMatch)retval.target.type=retval.target.name=target;else{retval.target.type='frame';retval.target.name=target}}var me=this;retval.title=element.getAttribute('title')}var elements=editor.document.getElementsByTag('img'),realAnchors=new CKEDITOR.dom.nodeList(editor.document.$.anchors),anchors=retval.anchors=[];for(var i=0;i',editor.document);selection=editor.getSelection();element.moveChildren(newElement);element.copyAttributes(newElement,{name:1});newElement.replace(element);element=newElement;selection.selectElement(element)}element.setAttributes(attributes);element.removeAttributes(removeAttributes);if(element.getAttribute('title'))element.setHtml(element.getAttribute('title'));if(element.getAttribute('name'))element.addClass('cke_anchor');else element.removeClass('cke_anchor');if(this.fakeObj)editor.createFakeElement(element,'cke_anchor','anchor').replace(this.fakeObj);delete this._.selectedElement}},contents:[{label:editor.lang.common.generalTab,id:'general',accessKey:'I',elements:[{type:'vbox',padding:0,children:[{type:'html',html:''+CKEDITOR.tools.htmlEncode(editor.lang.attachment.url)+''},{type:'hbox',widths:['280px','110px'],align:'right',children:[{id:'src',type:'text',label:'',validate:CKEDITOR.dialog.validate.notEmpty(editor.lang.flash.validateSrc),setup:function(data){if(data.url)this.setValue(data.url);this.select()},commit:function(data){data.url=this.getValue()}},{type:'button',id:'browse',filebrowser:'general:src',hidden:true,align:'center',label:editor.lang.common.browseServer}]}]},{type:'vbox',padding:0,children:[{id:'name',type:'text',label:editor.lang.attachment.name,setup:function(data){if(data.title)this.setValue(data.title)},commit:function(data){data.title=this.getValue()}}]},{type:'hbox',widths:['50%','50%'],children:[{type:'select',id:'linkTargetType',label:editor.lang.link.target,'default':'notSet',style:'width : 100%;','items':[[editor.lang.link.targetNotSet,'notSet'],[editor.lang.link.targetFrame,'frame'],[editor.lang.link.targetNew,'_blank'],[editor.lang.link.targetTop,'_top'],[editor.lang.link.targetSelf,'_self'],[editor.lang.link.targetParent,'_parent']],onChange:targetChanged,setup:function(data){if(data.target)this.setValue(data.target.type)},commit:function(data){if(!data.target)data.target={};data.target.type=this.getValue()}},{type:'text',id:'linkTargetName',label:editor.lang.link.targetFrameName,'default':'',setup:function(data){if(data.target)this.setValue(data.target.name)},commit:function(data){if(!data.target)data.target={};data.target.name=this.getValue()}}]}]},{id:'Upload',hidden:true,filebrowser:'uploadButton',label:editor.lang.common.upload,elements:[{type:'file',id:'upload',label:editor.lang.common.upload,size:38},{type:'fileButton',id:'uploadButton',label:editor.lang.common.uploadSubmit,filebrowser:'general:src','for':['Upload','upload']}]},]}})})(); 2 | -------------------------------------------------------------------------------- /test/dummy/public/javascripts/rails.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | // Technique from Juriy Zaytsev 3 | // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/ 4 | function isEventSupported(eventName) { 5 | var el = document.createElement('div'); 6 | eventName = 'on' + eventName; 7 | var isSupported = (eventName in el); 8 | if (!isSupported) { 9 | el.setAttribute(eventName, 'return;'); 10 | isSupported = typeof el[eventName] == 'function'; 11 | } 12 | el = null; 13 | return isSupported; 14 | } 15 | 16 | function isForm(element) { 17 | return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM' 18 | } 19 | 20 | function isInput(element) { 21 | if (Object.isElement(element)) { 22 | var name = element.nodeName.toUpperCase() 23 | return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA' 24 | } 25 | else return false 26 | } 27 | 28 | var submitBubbles = isEventSupported('submit'), 29 | changeBubbles = isEventSupported('change') 30 | 31 | if (!submitBubbles || !changeBubbles) { 32 | // augment the Event.Handler class to observe custom events when needed 33 | Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap( 34 | function(init, element, eventName, selector, callback) { 35 | init(element, eventName, selector, callback) 36 | // is the handler being attached to an element that doesn't support this event? 37 | if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) || 38 | (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) { 39 | // "submit" => "emulated:submit" 40 | this.eventName = 'emulated:' + this.eventName 41 | } 42 | } 43 | ) 44 | } 45 | 46 | if (!submitBubbles) { 47 | // discover forms on the page by observing focus events which always bubble 48 | document.on('focusin', 'form', function(focusEvent, form) { 49 | // special handler for the real "submit" event (one-time operation) 50 | if (!form.retrieve('emulated:submit')) { 51 | form.on('submit', function(submitEvent) { 52 | var emulated = form.fire('emulated:submit', submitEvent, true) 53 | // if custom event received preventDefault, cancel the real one too 54 | if (emulated.returnValue === false) submitEvent.preventDefault() 55 | }) 56 | form.store('emulated:submit', true) 57 | } 58 | }) 59 | } 60 | 61 | if (!changeBubbles) { 62 | // discover form inputs on the page 63 | document.on('focusin', 'input, select, texarea', function(focusEvent, input) { 64 | // special handler for real "change" events 65 | if (!input.retrieve('emulated:change')) { 66 | input.on('change', function(changeEvent) { 67 | input.fire('emulated:change', changeEvent, true) 68 | }) 69 | input.store('emulated:change', true) 70 | } 71 | }) 72 | } 73 | 74 | function handleRemote(element) { 75 | var method, url, params; 76 | 77 | var event = element.fire("ajax:before"); 78 | if (event.stopped) return false; 79 | 80 | if (element.tagName.toLowerCase() === 'form') { 81 | method = element.readAttribute('method') || 'post'; 82 | url = element.readAttribute('action'); 83 | params = element.serialize(); 84 | } else { 85 | method = element.readAttribute('data-method') || 'get'; 86 | url = element.readAttribute('href'); 87 | params = {}; 88 | } 89 | 90 | new Ajax.Request(url, { 91 | method: method, 92 | parameters: params, 93 | evalScripts: true, 94 | 95 | onComplete: function(request) { element.fire("ajax:complete", request); }, 96 | onSuccess: function(request) { element.fire("ajax:success", request); }, 97 | onFailure: function(request) { element.fire("ajax:failure", request); } 98 | }); 99 | 100 | element.fire("ajax:after"); 101 | } 102 | 103 | function handleMethod(element) { 104 | var method = element.readAttribute('data-method'), 105 | url = element.readAttribute('href'), 106 | csrf_param = $$('meta[name=csrf-param]')[0], 107 | csrf_token = $$('meta[name=csrf-token]')[0]; 108 | 109 | var form = new Element('form', { method: "POST", action: url, style: "display: none;" }); 110 | element.parentNode.insert(form); 111 | 112 | if (method !== 'post') { 113 | var field = new Element('input', { type: 'hidden', name: '_method', value: method }); 114 | form.insert(field); 115 | } 116 | 117 | if (csrf_param) { 118 | var param = csrf_param.readAttribute('content'), 119 | token = csrf_token.readAttribute('content'), 120 | field = new Element('input', { type: 'hidden', name: param, value: token }); 121 | form.insert(field); 122 | } 123 | 124 | form.submit(); 125 | } 126 | 127 | 128 | document.on("click", "*[data-confirm]", function(event, element) { 129 | var message = element.readAttribute('data-confirm'); 130 | if (!confirm(message)) event.stop(); 131 | }); 132 | 133 | document.on("click", "a[data-remote]", function(event, element) { 134 | if (event.stopped) return; 135 | handleRemote(element); 136 | event.stop(); 137 | }); 138 | 139 | document.on("click", "a[data-method]", function(event, element) { 140 | if (event.stopped) return; 141 | handleMethod(element); 142 | event.stop(); 143 | }); 144 | 145 | document.on("submit", function(event) { 146 | var element = event.findElement(), 147 | message = element.readAttribute('data-confirm'); 148 | if (message && !confirm(message)) { 149 | event.stop(); 150 | return false; 151 | } 152 | 153 | var inputs = element.select("input[type=submit][data-disable-with]"); 154 | inputs.each(function(input) { 155 | input.disabled = true; 156 | input.writeAttribute('data-original-value', input.value); 157 | input.value = input.readAttribute('data-disable-with'); 158 | }); 159 | 160 | var element = event.findElement("form[data-remote]"); 161 | if (element) { 162 | handleRemote(element); 163 | event.stop(); 164 | } 165 | }); 166 | 167 | document.on("ajax:after", "form", function(event, element) { 168 | var inputs = element.select("input[type=submit][disabled=true][data-disable-with]"); 169 | inputs.each(function(input) { 170 | input.value = input.readAttribute('data-original-value'); 171 | input.removeAttribute('data-original-value'); 172 | input.disabled = false; 173 | }); 174 | }); 175 | 176 | Ajax.Responders.register({ 177 | onCreate: function(request) { 178 | var csrf_meta_tag = $$('meta[name=csrf-token]')[0]; 179 | 180 | if (csrf_meta_tag) { 181 | var header = 'X-CSRF-Token', 182 | token = csrf_meta_tag.readAttribute('content'); 183 | 184 | if (!request.options.requestHeaders) { 185 | request.options.requestHeaders = {}; 186 | } 187 | request.options.requestHeaders[header] = token; 188 | } 189 | } 190 | }); 191 | })(); 192 | -------------------------------------------------------------------------------- /lib/generators/ckeditor/templates/ckeditor/filebrowser/javascripts/application.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | $.QueryString = (function(a) { 3 | if (a == "") return {}; 4 | var b = {}; 5 | for (var i = 0; i < a.length; ++i) 6 | { 7 | var p=a[i].split('='); 8 | if (p.length != 2) continue; 9 | b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " ")); 10 | } 11 | return b; 12 | })(window.location.search.substr(1).split('&')) 13 | })(jQuery); 14 | 15 | $(document).ready(function(){ 16 | $("div.gal-item div.gal-inner-holder") 17 | .live('mouseover', function(e){ 18 | $(this).addClass('hover'); 19 | }) 20 | .live('mouseout', function(e){ 21 | $(this).removeClass('hover'); 22 | }) 23 | .live('click', function(e){ 24 | var url = $(this).parents('div.gal-item').data('url'); 25 | CKEDITOR.tools.callFunction(CKEditorFuncNum, url); 26 | window.close(); 27 | }); 28 | 29 | $("div.gal-item a.gal-del").live('ajax:complete', function(xhr, status){ 30 | $(this).parents('div.gal-item').remove(); 31 | }); 32 | }); 33 | 34 | // Collection of all instances on page 35 | qq.FileUploader.instances = new Object(); 36 | 37 | /** 38 | * Class that creates upload widget with drag-and-drop and file list 39 | * @inherits qq.FileUploaderBasic 40 | */ 41 | qq.FileUploaderInput = function(o){ 42 | // call parent constructor 43 | qq.FileUploaderBasic.apply(this, arguments); 44 | 45 | // additional options 46 | qq.extend(this._options, { 47 | element: null, 48 | // if set, will be used instead of qq-upload-list in template 49 | listElement: null, 50 | 51 | template_id: '#fileupload_tmpl', 52 | 53 | classes: { 54 | // used to get elements from templates 55 | button: 'fileupload-button', 56 | drop: 'fileupload-drop-area', 57 | dropActive: 'fileupload-drop-area-active', 58 | list: 'fileupload-list', 59 | preview: 'fileupload-preview', 60 | 61 | file: 'fileupload-file', 62 | spinner: 'fileupload-spinner', 63 | size: 'fileupload-size', 64 | cancel: 'fileupload-cancel', 65 | 66 | // added to list item when upload completes 67 | // used in css to hide progress spinner 68 | success: 'fileupload-success', 69 | fail: 'fileupload-fail' 70 | } 71 | }); 72 | // overwrite options with user supplied 73 | qq.extend(this._options, o); 74 | 75 | this._element = document.getElementById(this._options.element); 76 | this._listElement = this._options.listElement || this._find(this._element, 'list'); 77 | 78 | this._classes = this._options.classes; 79 | 80 | this._button = this._createUploadButton(this._find(this._element, 'button')); 81 | 82 | //this._setupDragDrop(); 83 | 84 | qq.FileUploader.instances[this._element.id] = this; 85 | }; 86 | 87 | // inherit from Basic Uploader 88 | qq.extend(qq.FileUploaderInput.prototype, qq.FileUploaderBasic.prototype); 89 | 90 | qq.extend(qq.FileUploaderInput.prototype, { 91 | /** 92 | * Gets one of the elements listed in this._options.classes 93 | **/ 94 | _find: function(parent, type){ 95 | var element = qq.getByClass(parent, this._options.classes[type])[0]; 96 | if (!element){ 97 | alert(type); 98 | throw new Error('element not found ' + type); 99 | } 100 | 101 | return element; 102 | }, 103 | _setupDragDrop: function(){ 104 | var self = this, 105 | dropArea = this._find(this._element, 'drop'); 106 | 107 | var dz = new qq.UploadDropZone({ 108 | element: dropArea, 109 | onEnter: function(e){ 110 | qq.addClass(dropArea, self._classes.dropActive); 111 | e.stopPropagation(); 112 | }, 113 | onLeave: function(e){ 114 | e.stopPropagation(); 115 | }, 116 | onLeaveNotDescendants: function(e){ 117 | qq.removeClass(dropArea, self._classes.dropActive); 118 | }, 119 | onDrop: function(e){ 120 | dropArea.style.display = 'none'; 121 | qq.removeClass(dropArea, self._classes.dropActive); 122 | self._uploadFileList(e.dataTransfer.files); 123 | } 124 | }); 125 | 126 | dropArea.style.display = 'none'; 127 | 128 | qq.attach(document, 'dragenter', function(e){ 129 | if (!dz._isValidFileDrag(e)) return; 130 | 131 | dropArea.style.display = 'block'; 132 | }); 133 | qq.attach(document, 'dragleave', function(e){ 134 | if (!dz._isValidFileDrag(e)) return; 135 | 136 | var relatedTarget = document.elementFromPoint(e.clientX, e.clientY); 137 | // only fire when leaving document out 138 | if ( ! relatedTarget || relatedTarget.nodeName == "HTML"){ 139 | dropArea.style.display = 'none'; 140 | } 141 | }); 142 | }, 143 | _onSubmit: function(id, fileName){ 144 | qq.FileUploaderBasic.prototype._onSubmit.apply(this, arguments); 145 | this._addToList(id, fileName); 146 | }, 147 | _onProgress: function(id, fileName, loaded, total){ 148 | qq.FileUploaderBasic.prototype._onProgress.apply(this, arguments); 149 | 150 | var item = this._getItemByFileId(id); 151 | var size = this._find(item, 'size'); 152 | 153 | var text; 154 | if (loaded != total){ 155 | text = Math.round(loaded / total * 100) + '% from ' + this._formatSize(total); 156 | } else { 157 | text = this._formatSize(total); 158 | } 159 | 160 | qq.setText(size, text); 161 | }, 162 | _onComplete: function(id, fileName, result){ 163 | qq.FileUploaderBasic.prototype._onComplete.apply(this, arguments); 164 | 165 | var item = this._getItemByFileId(id); 166 | var asset = result.asset; 167 | 168 | if (asset && asset.id){ 169 | qq.addClass(item, this._classes.success); 170 | 171 | asset.size = this._formatSize(asset.size); 172 | asset.controller = (asset.type.toLowerCase() == "ckeditor::picture" ? "pictures" : "attachment_files"); 173 | 174 | $(item).replaceWith($(this._options.template_id).tmpl(asset)); 175 | } else { 176 | qq.addClass(item, this._classes.fail); 177 | } 178 | }, 179 | _addToList: function(id, fileName){ 180 | if (this._listElement) { 181 | if (this._options.multiple === false) { 182 | $(this._listElement).empty(); 183 | } 184 | 185 | var asset = { 186 | id: 0, 187 | filename: this._formatFileName(fileName), 188 | size: 0, 189 | format_created_at: '', 190 | url_content: "#", 191 | controller: "assets", 192 | url_thumb: "/javascripts/ckeditor/filebrowser/images/preloader.gif" 193 | }; 194 | 195 | var item = $(this._options.template_id) 196 | .tmpl(asset) 197 | .attr('qqfileid', id) 198 | .prependTo( this._listElement ); 199 | 200 | item.find('div.img').addClass('preloader'); 201 | 202 | this._bindCancelEvent(item); 203 | } 204 | }, 205 | _getItemByFileId: function(id){ 206 | return $(this._listElement).find('div[qqfileid=' + id +']').get(0); 207 | }, 208 | /** 209 | * delegate click event for cancel link 210 | **/ 211 | _bindCancelEvent: function(element){ 212 | var self = this, 213 | item = $(element); 214 | 215 | item.find('a.' + this._classes.cancel).bind('click', function(e){ 216 | self._handler.cancel( item.attr('qqfileid') ); 217 | item.remove(); 218 | qq.preventDefault(e); 219 | return false; 220 | }); 221 | } 222 | }); 223 | -------------------------------------------------------------------------------- /test/dummy/public/javascripts/dragdrop.js: -------------------------------------------------------------------------------- 1 | // script.aculo.us dragdrop.js v1.8.3, Thu Oct 08 11:23:33 +0200 2009 2 | 3 | // Copyright (c) 2005-2009 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) 4 | // 5 | // script.aculo.us is freely distributable under the terms of an MIT-style license. 6 | // For details, see the script.aculo.us web site: http://script.aculo.us/ 7 | 8 | if(Object.isUndefined(Effect)) 9 | throw("dragdrop.js requires including script.aculo.us' effects.js library"); 10 | 11 | var Droppables = { 12 | drops: [], 13 | 14 | remove: function(element) { 15 | this.drops = this.drops.reject(function(d) { return d.element==$(element) }); 16 | }, 17 | 18 | add: function(element) { 19 | element = $(element); 20 | var options = Object.extend({ 21 | greedy: true, 22 | hoverclass: null, 23 | tree: false 24 | }, arguments[1] || { }); 25 | 26 | // cache containers 27 | if(options.containment) { 28 | options._containers = []; 29 | var containment = options.containment; 30 | if(Object.isArray(containment)) { 31 | containment.each( function(c) { options._containers.push($(c)) }); 32 | } else { 33 | options._containers.push($(containment)); 34 | } 35 | } 36 | 37 | if(options.accept) options.accept = [options.accept].flatten(); 38 | 39 | Element.makePositioned(element); // fix IE 40 | options.element = element; 41 | 42 | this.drops.push(options); 43 | }, 44 | 45 | findDeepestChild: function(drops) { 46 | deepest = drops[0]; 47 | 48 | for (i = 1; i < drops.length; ++i) 49 | if (Element.isParent(drops[i].element, deepest.element)) 50 | deepest = drops[i]; 51 | 52 | return deepest; 53 | }, 54 | 55 | isContained: function(element, drop) { 56 | var containmentNode; 57 | if(drop.tree) { 58 | containmentNode = element.treeNode; 59 | } else { 60 | containmentNode = element.parentNode; 61 | } 62 | return drop._containers.detect(function(c) { return containmentNode == c }); 63 | }, 64 | 65 | isAffected: function(point, element, drop) { 66 | return ( 67 | (drop.element!=element) && 68 | ((!drop._containers) || 69 | this.isContained(element, drop)) && 70 | ((!drop.accept) || 71 | (Element.classNames(element).detect( 72 | function(v) { return drop.accept.include(v) } ) )) && 73 | Position.within(drop.element, point[0], point[1]) ); 74 | }, 75 | 76 | deactivate: function(drop) { 77 | if(drop.hoverclass) 78 | Element.removeClassName(drop.element, drop.hoverclass); 79 | this.last_active = null; 80 | }, 81 | 82 | activate: function(drop) { 83 | if(drop.hoverclass) 84 | Element.addClassName(drop.element, drop.hoverclass); 85 | this.last_active = drop; 86 | }, 87 | 88 | show: function(point, element) { 89 | if(!this.drops.length) return; 90 | var drop, affected = []; 91 | 92 | this.drops.each( function(drop) { 93 | if(Droppables.isAffected(point, element, drop)) 94 | affected.push(drop); 95 | }); 96 | 97 | if(affected.length>0) 98 | drop = Droppables.findDeepestChild(affected); 99 | 100 | if(this.last_active && this.last_active != drop) this.deactivate(this.last_active); 101 | if (drop) { 102 | Position.within(drop.element, point[0], point[1]); 103 | if(drop.onHover) 104 | drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element)); 105 | 106 | if (drop != this.last_active) Droppables.activate(drop); 107 | } 108 | }, 109 | 110 | fire: function(event, element) { 111 | if(!this.last_active) return; 112 | Position.prepare(); 113 | 114 | if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active)) 115 | if (this.last_active.onDrop) { 116 | this.last_active.onDrop(element, this.last_active.element, event); 117 | return true; 118 | } 119 | }, 120 | 121 | reset: function() { 122 | if(this.last_active) 123 | this.deactivate(this.last_active); 124 | } 125 | }; 126 | 127 | var Draggables = { 128 | drags: [], 129 | observers: [], 130 | 131 | register: function(draggable) { 132 | if(this.drags.length == 0) { 133 | this.eventMouseUp = this.endDrag.bindAsEventListener(this); 134 | this.eventMouseMove = this.updateDrag.bindAsEventListener(this); 135 | this.eventKeypress = this.keyPress.bindAsEventListener(this); 136 | 137 | Event.observe(document, "mouseup", this.eventMouseUp); 138 | Event.observe(document, "mousemove", this.eventMouseMove); 139 | Event.observe(document, "keypress", this.eventKeypress); 140 | } 141 | this.drags.push(draggable); 142 | }, 143 | 144 | unregister: function(draggable) { 145 | this.drags = this.drags.reject(function(d) { return d==draggable }); 146 | if(this.drags.length == 0) { 147 | Event.stopObserving(document, "mouseup", this.eventMouseUp); 148 | Event.stopObserving(document, "mousemove", this.eventMouseMove); 149 | Event.stopObserving(document, "keypress", this.eventKeypress); 150 | } 151 | }, 152 | 153 | activate: function(draggable) { 154 | if(draggable.options.delay) { 155 | this._timeout = setTimeout(function() { 156 | Draggables._timeout = null; 157 | window.focus(); 158 | Draggables.activeDraggable = draggable; 159 | }.bind(this), draggable.options.delay); 160 | } else { 161 | window.focus(); // allows keypress events if window isn't currently focused, fails for Safari 162 | this.activeDraggable = draggable; 163 | } 164 | }, 165 | 166 | deactivate: function() { 167 | this.activeDraggable = null; 168 | }, 169 | 170 | updateDrag: function(event) { 171 | if(!this.activeDraggable) return; 172 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; 173 | // Mozilla-based browsers fire successive mousemove events with 174 | // the same coordinates, prevent needless redrawing (moz bug?) 175 | if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return; 176 | this._lastPointer = pointer; 177 | 178 | this.activeDraggable.updateDrag(event, pointer); 179 | }, 180 | 181 | endDrag: function(event) { 182 | if(this._timeout) { 183 | clearTimeout(this._timeout); 184 | this._timeout = null; 185 | } 186 | if(!this.activeDraggable) return; 187 | this._lastPointer = null; 188 | this.activeDraggable.endDrag(event); 189 | this.activeDraggable = null; 190 | }, 191 | 192 | keyPress: function(event) { 193 | if(this.activeDraggable) 194 | this.activeDraggable.keyPress(event); 195 | }, 196 | 197 | addObserver: function(observer) { 198 | this.observers.push(observer); 199 | this._cacheObserverCallbacks(); 200 | }, 201 | 202 | removeObserver: function(element) { // element instead of observer fixes mem leaks 203 | this.observers = this.observers.reject( function(o) { return o.element==element }); 204 | this._cacheObserverCallbacks(); 205 | }, 206 | 207 | notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag' 208 | if(this[eventName+'Count'] > 0) 209 | this.observers.each( function(o) { 210 | if(o[eventName]) o[eventName](eventName, draggable, event); 211 | }); 212 | if(draggable.options[eventName]) draggable.options[eventName](draggable, event); 213 | }, 214 | 215 | _cacheObserverCallbacks: function() { 216 | ['onStart','onEnd','onDrag'].each( function(eventName) { 217 | Draggables[eventName+'Count'] = Draggables.observers.select( 218 | function(o) { return o[eventName]; } 219 | ).length; 220 | }); 221 | } 222 | }; 223 | 224 | /*--------------------------------------------------------------------------*/ 225 | 226 | var Draggable = Class.create({ 227 | initialize: function(element) { 228 | var defaults = { 229 | handle: false, 230 | reverteffect: function(element, top_offset, left_offset) { 231 | var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02; 232 | new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur, 233 | queue: {scope:'_draggable', position:'end'} 234 | }); 235 | }, 236 | endeffect: function(element) { 237 | var toOpacity = Object.isNumber(element._opacity) ? element._opacity : 1.0; 238 | new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity, 239 | queue: {scope:'_draggable', position:'end'}, 240 | afterFinish: function(){ 241 | Draggable._dragging[element] = false 242 | } 243 | }); 244 | }, 245 | zindex: 1000, 246 | revert: false, 247 | quiet: false, 248 | scroll: false, 249 | scrollSensitivity: 20, 250 | scrollSpeed: 15, 251 | snap: false, // false, or xy or [x,y] or function(x,y){ return [x,y] } 252 | delay: 0 253 | }; 254 | 255 | if(!arguments[1] || Object.isUndefined(arguments[1].endeffect)) 256 | Object.extend(defaults, { 257 | starteffect: function(element) { 258 | element._opacity = Element.getOpacity(element); 259 | Draggable._dragging[element] = true; 260 | new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7}); 261 | } 262 | }); 263 | 264 | var options = Object.extend(defaults, arguments[1] || { }); 265 | 266 | this.element = $(element); 267 | 268 | if(options.handle && Object.isString(options.handle)) 269 | this.handle = this.element.down('.'+options.handle, 0); 270 | 271 | if(!this.handle) this.handle = $(options.handle); 272 | if(!this.handle) this.handle = this.element; 273 | 274 | if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) { 275 | options.scroll = $(options.scroll); 276 | this._isScrollChild = Element.childOf(this.element, options.scroll); 277 | } 278 | 279 | Element.makePositioned(this.element); // fix IE 280 | 281 | this.options = options; 282 | this.dragging = false; 283 | 284 | this.eventMouseDown = this.initDrag.bindAsEventListener(this); 285 | Event.observe(this.handle, "mousedown", this.eventMouseDown); 286 | 287 | Draggables.register(this); 288 | }, 289 | 290 | destroy: function() { 291 | Event.stopObserving(this.handle, "mousedown", this.eventMouseDown); 292 | Draggables.unregister(this); 293 | }, 294 | 295 | currentDelta: function() { 296 | return([ 297 | parseInt(Element.getStyle(this.element,'left') || '0'), 298 | parseInt(Element.getStyle(this.element,'top') || '0')]); 299 | }, 300 | 301 | initDrag: function(event) { 302 | if(!Object.isUndefined(Draggable._dragging[this.element]) && 303 | Draggable._dragging[this.element]) return; 304 | if(Event.isLeftClick(event)) { 305 | // abort on form elements, fixes a Firefox issue 306 | var src = Event.element(event); 307 | if((tag_name = src.tagName.toUpperCase()) && ( 308 | tag_name=='INPUT' || 309 | tag_name=='SELECT' || 310 | tag_name=='OPTION' || 311 | tag_name=='BUTTON' || 312 | tag_name=='TEXTAREA')) return; 313 | 314 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; 315 | var pos = this.element.cumulativeOffset(); 316 | this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) }); 317 | 318 | Draggables.activate(this); 319 | Event.stop(event); 320 | } 321 | }, 322 | 323 | startDrag: function(event) { 324 | this.dragging = true; 325 | if(!this.delta) 326 | this.delta = this.currentDelta(); 327 | 328 | if(this.options.zindex) { 329 | this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0); 330 | this.element.style.zIndex = this.options.zindex; 331 | } 332 | 333 | if(this.options.ghosting) { 334 | this._clone = this.element.cloneNode(true); 335 | this._originallyAbsolute = (this.element.getStyle('position') == 'absolute'); 336 | if (!this._originallyAbsolute) 337 | Position.absolutize(this.element); 338 | this.element.parentNode.insertBefore(this._clone, this.element); 339 | } 340 | 341 | if(this.options.scroll) { 342 | if (this.options.scroll == window) { 343 | var where = this._getWindowScroll(this.options.scroll); 344 | this.originalScrollLeft = where.left; 345 | this.originalScrollTop = where.top; 346 | } else { 347 | this.originalScrollLeft = this.options.scroll.scrollLeft; 348 | this.originalScrollTop = this.options.scroll.scrollTop; 349 | } 350 | } 351 | 352 | Draggables.notify('onStart', this, event); 353 | 354 | if(this.options.starteffect) this.options.starteffect(this.element); 355 | }, 356 | 357 | updateDrag: function(event, pointer) { 358 | if(!this.dragging) this.startDrag(event); 359 | 360 | if(!this.options.quiet){ 361 | Position.prepare(); 362 | Droppables.show(pointer, this.element); 363 | } 364 | 365 | Draggables.notify('onDrag', this, event); 366 | 367 | this.draw(pointer); 368 | if(this.options.change) this.options.change(this); 369 | 370 | if(this.options.scroll) { 371 | this.stopScrolling(); 372 | 373 | var p; 374 | if (this.options.scroll == window) { 375 | with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; } 376 | } else { 377 | p = Position.page(this.options.scroll); 378 | p[0] += this.options.scroll.scrollLeft + Position.deltaX; 379 | p[1] += this.options.scroll.scrollTop + Position.deltaY; 380 | p.push(p[0]+this.options.scroll.offsetWidth); 381 | p.push(p[1]+this.options.scroll.offsetHeight); 382 | } 383 | var speed = [0,0]; 384 | if(pointer[0] < (p[0]+this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[0]+this.options.scrollSensitivity); 385 | if(pointer[1] < (p[1]+this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[1]+this.options.scrollSensitivity); 386 | if(pointer[0] > (p[2]-this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[2]-this.options.scrollSensitivity); 387 | if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity); 388 | this.startScrolling(speed); 389 | } 390 | 391 | // fix AppleWebKit rendering 392 | if(Prototype.Browser.WebKit) window.scrollBy(0,0); 393 | 394 | Event.stop(event); 395 | }, 396 | 397 | finishDrag: function(event, success) { 398 | this.dragging = false; 399 | 400 | if(this.options.quiet){ 401 | Position.prepare(); 402 | var pointer = [Event.pointerX(event), Event.pointerY(event)]; 403 | Droppables.show(pointer, this.element); 404 | } 405 | 406 | if(this.options.ghosting) { 407 | if (!this._originallyAbsolute) 408 | Position.relativize(this.element); 409 | delete this._originallyAbsolute; 410 | Element.remove(this._clone); 411 | this._clone = null; 412 | } 413 | 414 | var dropped = false; 415 | if(success) { 416 | dropped = Droppables.fire(event, this.element); 417 | if (!dropped) dropped = false; 418 | } 419 | if(dropped && this.options.onDropped) this.options.onDropped(this.element); 420 | Draggables.notify('onEnd', this, event); 421 | 422 | var revert = this.options.revert; 423 | if(revert && Object.isFunction(revert)) revert = revert(this.element); 424 | 425 | var d = this.currentDelta(); 426 | if(revert && this.options.reverteffect) { 427 | if (dropped == 0 || revert != 'failure') 428 | this.options.reverteffect(this.element, 429 | d[1]-this.delta[1], d[0]-this.delta[0]); 430 | } else { 431 | this.delta = d; 432 | } 433 | 434 | if(this.options.zindex) 435 | this.element.style.zIndex = this.originalZ; 436 | 437 | if(this.options.endeffect) 438 | this.options.endeffect(this.element); 439 | 440 | Draggables.deactivate(this); 441 | Droppables.reset(); 442 | }, 443 | 444 | keyPress: function(event) { 445 | if(event.keyCode!=Event.KEY_ESC) return; 446 | this.finishDrag(event, false); 447 | Event.stop(event); 448 | }, 449 | 450 | endDrag: function(event) { 451 | if(!this.dragging) return; 452 | this.stopScrolling(); 453 | this.finishDrag(event, true); 454 | Event.stop(event); 455 | }, 456 | 457 | draw: function(point) { 458 | var pos = this.element.cumulativeOffset(); 459 | if(this.options.ghosting) { 460 | var r = Position.realOffset(this.element); 461 | pos[0] += r[0] - Position.deltaX; pos[1] += r[1] - Position.deltaY; 462 | } 463 | 464 | var d = this.currentDelta(); 465 | pos[0] -= d[0]; pos[1] -= d[1]; 466 | 467 | if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) { 468 | pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft; 469 | pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop; 470 | } 471 | 472 | var p = [0,1].map(function(i){ 473 | return (point[i]-pos[i]-this.offset[i]) 474 | }.bind(this)); 475 | 476 | if(this.options.snap) { 477 | if(Object.isFunction(this.options.snap)) { 478 | p = this.options.snap(p[0],p[1],this); 479 | } else { 480 | if(Object.isArray(this.options.snap)) { 481 | p = p.map( function(v, i) { 482 | return (v/this.options.snap[i]).round()*this.options.snap[i] }.bind(this)); 483 | } else { 484 | p = p.map( function(v) { 485 | return (v/this.options.snap).round()*this.options.snap }.bind(this)); 486 | } 487 | }} 488 | 489 | var style = this.element.style; 490 | if((!this.options.constraint) || (this.options.constraint=='horizontal')) 491 | style.left = p[0] + "px"; 492 | if((!this.options.constraint) || (this.options.constraint=='vertical')) 493 | style.top = p[1] + "px"; 494 | 495 | if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering 496 | }, 497 | 498 | stopScrolling: function() { 499 | if(this.scrollInterval) { 500 | clearInterval(this.scrollInterval); 501 | this.scrollInterval = null; 502 | Draggables._lastScrollPointer = null; 503 | } 504 | }, 505 | 506 | startScrolling: function(speed) { 507 | if(!(speed[0] || speed[1])) return; 508 | this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed]; 509 | this.lastScrolled = new Date(); 510 | this.scrollInterval = setInterval(this.scroll.bind(this), 10); 511 | }, 512 | 513 | scroll: function() { 514 | var current = new Date(); 515 | var delta = current - this.lastScrolled; 516 | this.lastScrolled = current; 517 | if(this.options.scroll == window) { 518 | with (this._getWindowScroll(this.options.scroll)) { 519 | if (this.scrollSpeed[0] || this.scrollSpeed[1]) { 520 | var d = delta / 1000; 521 | this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] ); 522 | } 523 | } 524 | } else { 525 | this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000; 526 | this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000; 527 | } 528 | 529 | Position.prepare(); 530 | Droppables.show(Draggables._lastPointer, this.element); 531 | Draggables.notify('onDrag', this); 532 | if (this._isScrollChild) { 533 | Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer); 534 | Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000; 535 | Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000; 536 | if (Draggables._lastScrollPointer[0] < 0) 537 | Draggables._lastScrollPointer[0] = 0; 538 | if (Draggables._lastScrollPointer[1] < 0) 539 | Draggables._lastScrollPointer[1] = 0; 540 | this.draw(Draggables._lastScrollPointer); 541 | } 542 | 543 | if(this.options.change) this.options.change(this); 544 | }, 545 | 546 | _getWindowScroll: function(w) { 547 | var T, L, W, H; 548 | with (w.document) { 549 | if (w.document.documentElement && documentElement.scrollTop) { 550 | T = documentElement.scrollTop; 551 | L = documentElement.scrollLeft; 552 | } else if (w.document.body) { 553 | T = body.scrollTop; 554 | L = body.scrollLeft; 555 | } 556 | if (w.innerWidth) { 557 | W = w.innerWidth; 558 | H = w.innerHeight; 559 | } else if (w.document.documentElement && documentElement.clientWidth) { 560 | W = documentElement.clientWidth; 561 | H = documentElement.clientHeight; 562 | } else { 563 | W = body.offsetWidth; 564 | H = body.offsetHeight; 565 | } 566 | } 567 | return { top: T, left: L, width: W, height: H }; 568 | } 569 | }); 570 | 571 | Draggable._dragging = { }; 572 | 573 | /*--------------------------------------------------------------------------*/ 574 | 575 | var SortableObserver = Class.create({ 576 | initialize: function(element, observer) { 577 | this.element = $(element); 578 | this.observer = observer; 579 | this.lastValue = Sortable.serialize(this.element); 580 | }, 581 | 582 | onStart: function() { 583 | this.lastValue = Sortable.serialize(this.element); 584 | }, 585 | 586 | onEnd: function() { 587 | Sortable.unmark(); 588 | if(this.lastValue != Sortable.serialize(this.element)) 589 | this.observer(this.element) 590 | } 591 | }); 592 | 593 | var Sortable = { 594 | SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/, 595 | 596 | sortables: { }, 597 | 598 | _findRootElement: function(element) { 599 | while (element.tagName.toUpperCase() != "BODY") { 600 | if(element.id && Sortable.sortables[element.id]) return element; 601 | element = element.parentNode; 602 | } 603 | }, 604 | 605 | options: function(element) { 606 | element = Sortable._findRootElement($(element)); 607 | if(!element) return; 608 | return Sortable.sortables[element.id]; 609 | }, 610 | 611 | destroy: function(element){ 612 | element = $(element); 613 | var s = Sortable.sortables[element.id]; 614 | 615 | if(s) { 616 | Draggables.removeObserver(s.element); 617 | s.droppables.each(function(d){ Droppables.remove(d) }); 618 | s.draggables.invoke('destroy'); 619 | 620 | delete Sortable.sortables[s.element.id]; 621 | } 622 | }, 623 | 624 | create: function(element) { 625 | element = $(element); 626 | var options = Object.extend({ 627 | element: element, 628 | tag: 'li', // assumes li children, override with tag: 'tagname' 629 | dropOnEmpty: false, 630 | tree: false, 631 | treeTag: 'ul', 632 | overlap: 'vertical', // one of 'vertical', 'horizontal' 633 | constraint: 'vertical', // one of 'vertical', 'horizontal', false 634 | containment: element, // also takes array of elements (or id's); or false 635 | handle: false, // or a CSS class 636 | only: false, 637 | delay: 0, 638 | hoverclass: null, 639 | ghosting: false, 640 | quiet: false, 641 | scroll: false, 642 | scrollSensitivity: 20, 643 | scrollSpeed: 15, 644 | format: this.SERIALIZE_RULE, 645 | 646 | // these take arrays of elements or ids and can be 647 | // used for better initialization performance 648 | elements: false, 649 | handles: false, 650 | 651 | onChange: Prototype.emptyFunction, 652 | onUpdate: Prototype.emptyFunction 653 | }, arguments[1] || { }); 654 | 655 | // clear any old sortable with same element 656 | this.destroy(element); 657 | 658 | // build options for the draggables 659 | var options_for_draggable = { 660 | revert: true, 661 | quiet: options.quiet, 662 | scroll: options.scroll, 663 | scrollSpeed: options.scrollSpeed, 664 | scrollSensitivity: options.scrollSensitivity, 665 | delay: options.delay, 666 | ghosting: options.ghosting, 667 | constraint: options.constraint, 668 | handle: options.handle }; 669 | 670 | if(options.starteffect) 671 | options_for_draggable.starteffect = options.starteffect; 672 | 673 | if(options.reverteffect) 674 | options_for_draggable.reverteffect = options.reverteffect; 675 | else 676 | if(options.ghosting) options_for_draggable.reverteffect = function(element) { 677 | element.style.top = 0; 678 | element.style.left = 0; 679 | }; 680 | 681 | if(options.endeffect) 682 | options_for_draggable.endeffect = options.endeffect; 683 | 684 | if(options.zindex) 685 | options_for_draggable.zindex = options.zindex; 686 | 687 | // build options for the droppables 688 | var options_for_droppable = { 689 | overlap: options.overlap, 690 | containment: options.containment, 691 | tree: options.tree, 692 | hoverclass: options.hoverclass, 693 | onHover: Sortable.onHover 694 | }; 695 | 696 | var options_for_tree = { 697 | onHover: Sortable.onEmptyHover, 698 | overlap: options.overlap, 699 | containment: options.containment, 700 | hoverclass: options.hoverclass 701 | }; 702 | 703 | // fix for gecko engine 704 | Element.cleanWhitespace(element); 705 | 706 | options.draggables = []; 707 | options.droppables = []; 708 | 709 | // drop on empty handling 710 | if(options.dropOnEmpty || options.tree) { 711 | Droppables.add(element, options_for_tree); 712 | options.droppables.push(element); 713 | } 714 | 715 | (options.elements || this.findElements(element, options) || []).each( function(e,i) { 716 | var handle = options.handles ? $(options.handles[i]) : 717 | (options.handle ? $(e).select('.' + options.handle)[0] : e); 718 | options.draggables.push( 719 | new Draggable(e, Object.extend(options_for_draggable, { handle: handle }))); 720 | Droppables.add(e, options_for_droppable); 721 | if(options.tree) e.treeNode = element; 722 | options.droppables.push(e); 723 | }); 724 | 725 | if(options.tree) { 726 | (Sortable.findTreeElements(element, options) || []).each( function(e) { 727 | Droppables.add(e, options_for_tree); 728 | e.treeNode = element; 729 | options.droppables.push(e); 730 | }); 731 | } 732 | 733 | // keep reference 734 | this.sortables[element.identify()] = options; 735 | 736 | // for onupdate 737 | Draggables.addObserver(new SortableObserver(element, options.onUpdate)); 738 | 739 | }, 740 | 741 | // return all suitable-for-sortable elements in a guaranteed order 742 | findElements: function(element, options) { 743 | return Element.findChildren( 744 | element, options.only, options.tree ? true : false, options.tag); 745 | }, 746 | 747 | findTreeElements: function(element, options) { 748 | return Element.findChildren( 749 | element, options.only, options.tree ? true : false, options.treeTag); 750 | }, 751 | 752 | onHover: function(element, dropon, overlap) { 753 | if(Element.isParent(dropon, element)) return; 754 | 755 | if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) { 756 | return; 757 | } else if(overlap>0.5) { 758 | Sortable.mark(dropon, 'before'); 759 | if(dropon.previousSibling != element) { 760 | var oldParentNode = element.parentNode; 761 | element.style.visibility = "hidden"; // fix gecko rendering 762 | dropon.parentNode.insertBefore(element, dropon); 763 | if(dropon.parentNode!=oldParentNode) 764 | Sortable.options(oldParentNode).onChange(element); 765 | Sortable.options(dropon.parentNode).onChange(element); 766 | } 767 | } else { 768 | Sortable.mark(dropon, 'after'); 769 | var nextElement = dropon.nextSibling || null; 770 | if(nextElement != element) { 771 | var oldParentNode = element.parentNode; 772 | element.style.visibility = "hidden"; // fix gecko rendering 773 | dropon.parentNode.insertBefore(element, nextElement); 774 | if(dropon.parentNode!=oldParentNode) 775 | Sortable.options(oldParentNode).onChange(element); 776 | Sortable.options(dropon.parentNode).onChange(element); 777 | } 778 | } 779 | }, 780 | 781 | onEmptyHover: function(element, dropon, overlap) { 782 | var oldParentNode = element.parentNode; 783 | var droponOptions = Sortable.options(dropon); 784 | 785 | if(!Element.isParent(dropon, element)) { 786 | var index; 787 | 788 | var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only}); 789 | var child = null; 790 | 791 | if(children) { 792 | var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap); 793 | 794 | for (index = 0; index < children.length; index += 1) { 795 | if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) { 796 | offset -= Element.offsetSize (children[index], droponOptions.overlap); 797 | } else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) { 798 | child = index + 1 < children.length ? children[index + 1] : null; 799 | break; 800 | } else { 801 | child = children[index]; 802 | break; 803 | } 804 | } 805 | } 806 | 807 | dropon.insertBefore(element, child); 808 | 809 | Sortable.options(oldParentNode).onChange(element); 810 | droponOptions.onChange(element); 811 | } 812 | }, 813 | 814 | unmark: function() { 815 | if(Sortable._marker) Sortable._marker.hide(); 816 | }, 817 | 818 | mark: function(dropon, position) { 819 | // mark on ghosting only 820 | var sortable = Sortable.options(dropon.parentNode); 821 | if(sortable && !sortable.ghosting) return; 822 | 823 | if(!Sortable._marker) { 824 | Sortable._marker = 825 | ($('dropmarker') || Element.extend(document.createElement('DIV'))). 826 | hide().addClassName('dropmarker').setStyle({position:'absolute'}); 827 | document.getElementsByTagName("body").item(0).appendChild(Sortable._marker); 828 | } 829 | var offsets = dropon.cumulativeOffset(); 830 | Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'}); 831 | 832 | if(position=='after') 833 | if(sortable.overlap == 'horizontal') 834 | Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'}); 835 | else 836 | Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'}); 837 | 838 | Sortable._marker.show(); 839 | }, 840 | 841 | _tree: function(element, options, parent) { 842 | var children = Sortable.findElements(element, options) || []; 843 | 844 | for (var i = 0; i < children.length; ++i) { 845 | var match = children[i].id.match(options.format); 846 | 847 | if (!match) continue; 848 | 849 | var child = { 850 | id: encodeURIComponent(match ? match[1] : null), 851 | element: element, 852 | parent: parent, 853 | children: [], 854 | position: parent.children.length, 855 | container: $(children[i]).down(options.treeTag) 856 | }; 857 | 858 | /* Get the element containing the children and recurse over it */ 859 | if (child.container) 860 | this._tree(child.container, options, child); 861 | 862 | parent.children.push (child); 863 | } 864 | 865 | return parent; 866 | }, 867 | 868 | tree: function(element) { 869 | element = $(element); 870 | var sortableOptions = this.options(element); 871 | var options = Object.extend({ 872 | tag: sortableOptions.tag, 873 | treeTag: sortableOptions.treeTag, 874 | only: sortableOptions.only, 875 | name: element.id, 876 | format: sortableOptions.format 877 | }, arguments[1] || { }); 878 | 879 | var root = { 880 | id: null, 881 | parent: null, 882 | children: [], 883 | container: element, 884 | position: 0 885 | }; 886 | 887 | return Sortable._tree(element, options, root); 888 | }, 889 | 890 | /* Construct a [i] index for a particular node */ 891 | _constructIndex: function(node) { 892 | var index = ''; 893 | do { 894 | if (node.id) index = '[' + node.position + ']' + index; 895 | } while ((node = node.parent) != null); 896 | return index; 897 | }, 898 | 899 | sequence: function(element) { 900 | element = $(element); 901 | var options = Object.extend(this.options(element), arguments[1] || { }); 902 | 903 | return $(this.findElements(element, options) || []).map( function(item) { 904 | return item.id.match(options.format) ? item.id.match(options.format)[1] : ''; 905 | }); 906 | }, 907 | 908 | setSequence: function(element, new_sequence) { 909 | element = $(element); 910 | var options = Object.extend(this.options(element), arguments[2] || { }); 911 | 912 | var nodeMap = { }; 913 | this.findElements(element, options).each( function(n) { 914 | if (n.id.match(options.format)) 915 | nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode]; 916 | n.parentNode.removeChild(n); 917 | }); 918 | 919 | new_sequence.each(function(ident) { 920 | var n = nodeMap[ident]; 921 | if (n) { 922 | n[1].appendChild(n[0]); 923 | delete nodeMap[ident]; 924 | } 925 | }); 926 | }, 927 | 928 | serialize: function(element) { 929 | element = $(element); 930 | var options = Object.extend(Sortable.options(element), arguments[1] || { }); 931 | var name = encodeURIComponent( 932 | (arguments[1] && arguments[1].name) ? arguments[1].name : element.id); 933 | 934 | if (options.tree) { 935 | return Sortable.tree(element, arguments[1]).children.map( function (item) { 936 | return [name + Sortable._constructIndex(item) + "[id]=" + 937 | encodeURIComponent(item.id)].concat(item.children.map(arguments.callee)); 938 | }).flatten().join('&'); 939 | } else { 940 | return Sortable.sequence(element, arguments[1]).map( function(item) { 941 | return name + "[]=" + encodeURIComponent(item); 942 | }).join('&'); 943 | } 944 | } 945 | }; 946 | 947 | // Returns true if child is contained within element 948 | Element.isParent = function(child, element) { 949 | if (!child.parentNode || child == element) return false; 950 | if (child.parentNode == element) return true; 951 | return Element.isParent(child.parentNode, element); 952 | }; 953 | 954 | Element.findChildren = function(element, only, recursive, tagName) { 955 | if(!element.hasChildNodes()) return null; 956 | tagName = tagName.toUpperCase(); 957 | if(only) only = [only].flatten(); 958 | var elements = []; 959 | $A(element.childNodes).each( function(e) { 960 | if(e.tagName && e.tagName.toUpperCase()==tagName && 961 | (!only || (Element.classNames(e).detect(function(v) { return only.include(v) })))) 962 | elements.push(e); 963 | if(recursive) { 964 | var grandchildren = Element.findChildren(e, only, recursive, tagName); 965 | if(grandchildren) elements.push(grandchildren); 966 | } 967 | }); 968 | 969 | return (elements.length>0 ? elements.flatten() : []); 970 | }; 971 | 972 | Element.offsetSize = function (element, type) { 973 | return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')]; 974 | }; -------------------------------------------------------------------------------- /test/dummy/public/javascripts/controls.js: -------------------------------------------------------------------------------- 1 | // script.aculo.us controls.js v1.8.3, Thu Oct 08 11:23:33 +0200 2009 2 | 3 | // Copyright (c) 2005-2009 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) 4 | // (c) 2005-2009 Ivan Krstic (http://blogs.law.harvard.edu/ivan) 5 | // (c) 2005-2009 Jon Tirsen (http://www.tirsen.com) 6 | // Contributors: 7 | // Richard Livsey 8 | // Rahul Bhargava 9 | // Rob Wills 10 | // 11 | // script.aculo.us is freely distributable under the terms of an MIT-style license. 12 | // For details, see the script.aculo.us web site: http://script.aculo.us/ 13 | 14 | // Autocompleter.Base handles all the autocompletion functionality 15 | // that's independent of the data source for autocompletion. This 16 | // includes drawing the autocompletion menu, observing keyboard 17 | // and mouse events, and similar. 18 | // 19 | // Specific autocompleters need to provide, at the very least, 20 | // a getUpdatedChoices function that will be invoked every time 21 | // the text inside the monitored textbox changes. This method 22 | // should get the text for which to provide autocompletion by 23 | // invoking this.getToken(), NOT by directly accessing 24 | // this.element.value. This is to allow incremental tokenized 25 | // autocompletion. Specific auto-completion logic (AJAX, etc) 26 | // belongs in getUpdatedChoices. 27 | // 28 | // Tokenized incremental autocompletion is enabled automatically 29 | // when an autocompleter is instantiated with the 'tokens' option 30 | // in the options parameter, e.g.: 31 | // new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' }); 32 | // will incrementally autocomplete with a comma as the token. 33 | // Additionally, ',' in the above example can be replaced with 34 | // a token array, e.g. { tokens: [',', '\n'] } which 35 | // enables autocompletion on multiple tokens. This is most 36 | // useful when one of the tokens is \n (a newline), as it 37 | // allows smart autocompletion after linebreaks. 38 | 39 | if(typeof Effect == 'undefined') 40 | throw("controls.js requires including script.aculo.us' effects.js library"); 41 | 42 | var Autocompleter = { }; 43 | Autocompleter.Base = Class.create({ 44 | baseInitialize: function(element, update, options) { 45 | element = $(element); 46 | this.element = element; 47 | this.update = $(update); 48 | this.hasFocus = false; 49 | this.changed = false; 50 | this.active = false; 51 | this.index = 0; 52 | this.entryCount = 0; 53 | this.oldElementValue = this.element.value; 54 | 55 | if(this.setOptions) 56 | this.setOptions(options); 57 | else 58 | this.options = options || { }; 59 | 60 | this.options.paramName = this.options.paramName || this.element.name; 61 | this.options.tokens = this.options.tokens || []; 62 | this.options.frequency = this.options.frequency || 0.4; 63 | this.options.minChars = this.options.minChars || 1; 64 | this.options.onShow = this.options.onShow || 65 | function(element, update){ 66 | if(!update.style.position || update.style.position=='absolute') { 67 | update.style.position = 'absolute'; 68 | Position.clone(element, update, { 69 | setHeight: false, 70 | offsetTop: element.offsetHeight 71 | }); 72 | } 73 | Effect.Appear(update,{duration:0.15}); 74 | }; 75 | this.options.onHide = this.options.onHide || 76 | function(element, update){ new Effect.Fade(update,{duration:0.15}) }; 77 | 78 | if(typeof(this.options.tokens) == 'string') 79 | this.options.tokens = new Array(this.options.tokens); 80 | // Force carriage returns as token delimiters anyway 81 | if (!this.options.tokens.include('\n')) 82 | this.options.tokens.push('\n'); 83 | 84 | this.observer = null; 85 | 86 | this.element.setAttribute('autocomplete','off'); 87 | 88 | Element.hide(this.update); 89 | 90 | Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this)); 91 | Event.observe(this.element, 'keydown', this.onKeyPress.bindAsEventListener(this)); 92 | }, 93 | 94 | show: function() { 95 | if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update); 96 | if(!this.iefix && 97 | (Prototype.Browser.IE) && 98 | (Element.getStyle(this.update, 'position')=='absolute')) { 99 | new Insertion.After(this.update, 100 | ''); 103 | this.iefix = $(this.update.id+'_iefix'); 104 | } 105 | if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50); 106 | }, 107 | 108 | fixIEOverlapping: function() { 109 | Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)}); 110 | this.iefix.style.zIndex = 1; 111 | this.update.style.zIndex = 2; 112 | Element.show(this.iefix); 113 | }, 114 | 115 | hide: function() { 116 | this.stopIndicator(); 117 | if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.element, this.update); 118 | if(this.iefix) Element.hide(this.iefix); 119 | }, 120 | 121 | startIndicator: function() { 122 | if(this.options.indicator) Element.show(this.options.indicator); 123 | }, 124 | 125 | stopIndicator: function() { 126 | if(this.options.indicator) Element.hide(this.options.indicator); 127 | }, 128 | 129 | onKeyPress: function(event) { 130 | if(this.active) 131 | switch(event.keyCode) { 132 | case Event.KEY_TAB: 133 | case Event.KEY_RETURN: 134 | this.selectEntry(); 135 | Event.stop(event); 136 | case Event.KEY_ESC: 137 | this.hide(); 138 | this.active = false; 139 | Event.stop(event); 140 | return; 141 | case Event.KEY_LEFT: 142 | case Event.KEY_RIGHT: 143 | return; 144 | case Event.KEY_UP: 145 | this.markPrevious(); 146 | this.render(); 147 | Event.stop(event); 148 | return; 149 | case Event.KEY_DOWN: 150 | this.markNext(); 151 | this.render(); 152 | Event.stop(event); 153 | return; 154 | } 155 | else 156 | if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN || 157 | (Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return; 158 | 159 | this.changed = true; 160 | this.hasFocus = true; 161 | 162 | if(this.observer) clearTimeout(this.observer); 163 | this.observer = 164 | setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000); 165 | }, 166 | 167 | activate: function() { 168 | this.changed = false; 169 | this.hasFocus = true; 170 | this.getUpdatedChoices(); 171 | }, 172 | 173 | onHover: function(event) { 174 | var element = Event.findElement(event, 'LI'); 175 | if(this.index != element.autocompleteIndex) 176 | { 177 | this.index = element.autocompleteIndex; 178 | this.render(); 179 | } 180 | Event.stop(event); 181 | }, 182 | 183 | onClick: function(event) { 184 | var element = Event.findElement(event, 'LI'); 185 | this.index = element.autocompleteIndex; 186 | this.selectEntry(); 187 | this.hide(); 188 | }, 189 | 190 | onBlur: function(event) { 191 | // needed to make click events working 192 | setTimeout(this.hide.bind(this), 250); 193 | this.hasFocus = false; 194 | this.active = false; 195 | }, 196 | 197 | render: function() { 198 | if(this.entryCount > 0) { 199 | for (var i = 0; i < this.entryCount; i++) 200 | this.index==i ? 201 | Element.addClassName(this.getEntry(i),"selected") : 202 | Element.removeClassName(this.getEntry(i),"selected"); 203 | if(this.hasFocus) { 204 | this.show(); 205 | this.active = true; 206 | } 207 | } else { 208 | this.active = false; 209 | this.hide(); 210 | } 211 | }, 212 | 213 | markPrevious: function() { 214 | if(this.index > 0) this.index--; 215 | else this.index = this.entryCount-1; 216 | this.getEntry(this.index).scrollIntoView(true); 217 | }, 218 | 219 | markNext: function() { 220 | if(this.index < this.entryCount-1) this.index++; 221 | else this.index = 0; 222 | this.getEntry(this.index).scrollIntoView(false); 223 | }, 224 | 225 | getEntry: function(index) { 226 | return this.update.firstChild.childNodes[index]; 227 | }, 228 | 229 | getCurrentEntry: function() { 230 | return this.getEntry(this.index); 231 | }, 232 | 233 | selectEntry: function() { 234 | this.active = false; 235 | this.updateElement(this.getCurrentEntry()); 236 | }, 237 | 238 | updateElement: function(selectedElement) { 239 | if (this.options.updateElement) { 240 | this.options.updateElement(selectedElement); 241 | return; 242 | } 243 | var value = ''; 244 | if (this.options.select) { 245 | var nodes = $(selectedElement).select('.' + this.options.select) || []; 246 | if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select); 247 | } else 248 | value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal'); 249 | 250 | var bounds = this.getTokenBounds(); 251 | if (bounds[0] != -1) { 252 | var newValue = this.element.value.substr(0, bounds[0]); 253 | var whitespace = this.element.value.substr(bounds[0]).match(/^\s+/); 254 | if (whitespace) 255 | newValue += whitespace[0]; 256 | this.element.value = newValue + value + this.element.value.substr(bounds[1]); 257 | } else { 258 | this.element.value = value; 259 | } 260 | this.oldElementValue = this.element.value; 261 | this.element.focus(); 262 | 263 | if (this.options.afterUpdateElement) 264 | this.options.afterUpdateElement(this.element, selectedElement); 265 | }, 266 | 267 | updateChoices: function(choices) { 268 | if(!this.changed && this.hasFocus) { 269 | this.update.innerHTML = choices; 270 | Element.cleanWhitespace(this.update); 271 | Element.cleanWhitespace(this.update.down()); 272 | 273 | if(this.update.firstChild && this.update.down().childNodes) { 274 | this.entryCount = 275 | this.update.down().childNodes.length; 276 | for (var i = 0; i < this.entryCount; i++) { 277 | var entry = this.getEntry(i); 278 | entry.autocompleteIndex = i; 279 | this.addObservers(entry); 280 | } 281 | } else { 282 | this.entryCount = 0; 283 | } 284 | 285 | this.stopIndicator(); 286 | this.index = 0; 287 | 288 | if(this.entryCount==1 && this.options.autoSelect) { 289 | this.selectEntry(); 290 | this.hide(); 291 | } else { 292 | this.render(); 293 | } 294 | } 295 | }, 296 | 297 | addObservers: function(element) { 298 | Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this)); 299 | Event.observe(element, "click", this.onClick.bindAsEventListener(this)); 300 | }, 301 | 302 | onObserverEvent: function() { 303 | this.changed = false; 304 | this.tokenBounds = null; 305 | if(this.getToken().length>=this.options.minChars) { 306 | this.getUpdatedChoices(); 307 | } else { 308 | this.active = false; 309 | this.hide(); 310 | } 311 | this.oldElementValue = this.element.value; 312 | }, 313 | 314 | getToken: function() { 315 | var bounds = this.getTokenBounds(); 316 | return this.element.value.substring(bounds[0], bounds[1]).strip(); 317 | }, 318 | 319 | getTokenBounds: function() { 320 | if (null != this.tokenBounds) return this.tokenBounds; 321 | var value = this.element.value; 322 | if (value.strip().empty()) return [-1, 0]; 323 | var diff = arguments.callee.getFirstDifferencePos(value, this.oldElementValue); 324 | var offset = (diff == this.oldElementValue.length ? 1 : 0); 325 | var prevTokenPos = -1, nextTokenPos = value.length; 326 | var tp; 327 | for (var index = 0, l = this.options.tokens.length; index < l; ++index) { 328 | tp = value.lastIndexOf(this.options.tokens[index], diff + offset - 1); 329 | if (tp > prevTokenPos) prevTokenPos = tp; 330 | tp = value.indexOf(this.options.tokens[index], diff + offset); 331 | if (-1 != tp && tp < nextTokenPos) nextTokenPos = tp; 332 | } 333 | return (this.tokenBounds = [prevTokenPos + 1, nextTokenPos]); 334 | } 335 | }); 336 | 337 | Autocompleter.Base.prototype.getTokenBounds.getFirstDifferencePos = function(newS, oldS) { 338 | var boundary = Math.min(newS.length, oldS.length); 339 | for (var index = 0; index < boundary; ++index) 340 | if (newS[index] != oldS[index]) 341 | return index; 342 | return boundary; 343 | }; 344 | 345 | Ajax.Autocompleter = Class.create(Autocompleter.Base, { 346 | initialize: function(element, update, url, options) { 347 | this.baseInitialize(element, update, options); 348 | this.options.asynchronous = true; 349 | this.options.onComplete = this.onComplete.bind(this); 350 | this.options.defaultParams = this.options.parameters || null; 351 | this.url = url; 352 | }, 353 | 354 | getUpdatedChoices: function() { 355 | this.startIndicator(); 356 | 357 | var entry = encodeURIComponent(this.options.paramName) + '=' + 358 | encodeURIComponent(this.getToken()); 359 | 360 | this.options.parameters = this.options.callback ? 361 | this.options.callback(this.element, entry) : entry; 362 | 363 | if(this.options.defaultParams) 364 | this.options.parameters += '&' + this.options.defaultParams; 365 | 366 | new Ajax.Request(this.url, this.options); 367 | }, 368 | 369 | onComplete: function(request) { 370 | this.updateChoices(request.responseText); 371 | } 372 | }); 373 | 374 | // The local array autocompleter. Used when you'd prefer to 375 | // inject an array of autocompletion options into the page, rather 376 | // than sending out Ajax queries, which can be quite slow sometimes. 377 | // 378 | // The constructor takes four parameters. The first two are, as usual, 379 | // the id of the monitored textbox, and id of the autocompletion menu. 380 | // The third is the array you want to autocomplete from, and the fourth 381 | // is the options block. 382 | // 383 | // Extra local autocompletion options: 384 | // - choices - How many autocompletion choices to offer 385 | // 386 | // - partialSearch - If false, the autocompleter will match entered 387 | // text only at the beginning of strings in the 388 | // autocomplete array. Defaults to true, which will 389 | // match text at the beginning of any *word* in the 390 | // strings in the autocomplete array. If you want to 391 | // search anywhere in the string, additionally set 392 | // the option fullSearch to true (default: off). 393 | // 394 | // - fullSsearch - Search anywhere in autocomplete array strings. 395 | // 396 | // - partialChars - How many characters to enter before triggering 397 | // a partial match (unlike minChars, which defines 398 | // how many characters are required to do any match 399 | // at all). Defaults to 2. 400 | // 401 | // - ignoreCase - Whether to ignore case when autocompleting. 402 | // Defaults to true. 403 | // 404 | // It's possible to pass in a custom function as the 'selector' 405 | // option, if you prefer to write your own autocompletion logic. 406 | // In that case, the other options above will not apply unless 407 | // you support them. 408 | 409 | Autocompleter.Local = Class.create(Autocompleter.Base, { 410 | initialize: function(element, update, array, options) { 411 | this.baseInitialize(element, update, options); 412 | this.options.array = array; 413 | }, 414 | 415 | getUpdatedChoices: function() { 416 | this.updateChoices(this.options.selector(this)); 417 | }, 418 | 419 | setOptions: function(options) { 420 | this.options = Object.extend({ 421 | choices: 10, 422 | partialSearch: true, 423 | partialChars: 2, 424 | ignoreCase: true, 425 | fullSearch: false, 426 | selector: function(instance) { 427 | var ret = []; // Beginning matches 428 | var partial = []; // Inside matches 429 | var entry = instance.getToken(); 430 | var count = 0; 431 | 432 | for (var i = 0; i < instance.options.array.length && 433 | ret.length < instance.options.choices ; i++) { 434 | 435 | var elem = instance.options.array[i]; 436 | var foundPos = instance.options.ignoreCase ? 437 | elem.toLowerCase().indexOf(entry.toLowerCase()) : 438 | elem.indexOf(entry); 439 | 440 | while (foundPos != -1) { 441 | if (foundPos == 0 && elem.length != entry.length) { 442 | ret.push("
  • " + elem.substr(0, entry.length) + "" + 443 | elem.substr(entry.length) + "
  • "); 444 | break; 445 | } else if (entry.length >= instance.options.partialChars && 446 | instance.options.partialSearch && foundPos != -1) { 447 | if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) { 448 | partial.push("
  • " + elem.substr(0, foundPos) + "" + 449 | elem.substr(foundPos, entry.length) + "" + elem.substr( 450 | foundPos + entry.length) + "
  • "); 451 | break; 452 | } 453 | } 454 | 455 | foundPos = instance.options.ignoreCase ? 456 | elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : 457 | elem.indexOf(entry, foundPos + 1); 458 | 459 | } 460 | } 461 | if (partial.length) 462 | ret = ret.concat(partial.slice(0, instance.options.choices - ret.length)); 463 | return ""; 464 | } 465 | }, options || { }); 466 | } 467 | }); 468 | 469 | // AJAX in-place editor and collection editor 470 | // Full rewrite by Christophe Porteneuve (April 2007). 471 | 472 | // Use this if you notice weird scrolling problems on some browsers, 473 | // the DOM might be a bit confused when this gets called so do this 474 | // waits 1 ms (with setTimeout) until it does the activation 475 | Field.scrollFreeActivate = function(field) { 476 | setTimeout(function() { 477 | Field.activate(field); 478 | }, 1); 479 | }; 480 | 481 | Ajax.InPlaceEditor = Class.create({ 482 | initialize: function(element, url, options) { 483 | this.url = url; 484 | this.element = element = $(element); 485 | this.prepareOptions(); 486 | this._controls = { }; 487 | arguments.callee.dealWithDeprecatedOptions(options); // DEPRECATION LAYER!!! 488 | Object.extend(this.options, options || { }); 489 | if (!this.options.formId && this.element.id) { 490 | this.options.formId = this.element.id + '-inplaceeditor'; 491 | if ($(this.options.formId)) 492 | this.options.formId = ''; 493 | } 494 | if (this.options.externalControl) 495 | this.options.externalControl = $(this.options.externalControl); 496 | if (!this.options.externalControl) 497 | this.options.externalControlOnly = false; 498 | this._originalBackground = this.element.getStyle('background-color') || 'transparent'; 499 | this.element.title = this.options.clickToEditText; 500 | this._boundCancelHandler = this.handleFormCancellation.bind(this); 501 | this._boundComplete = (this.options.onComplete || Prototype.emptyFunction).bind(this); 502 | this._boundFailureHandler = this.handleAJAXFailure.bind(this); 503 | this._boundSubmitHandler = this.handleFormSubmission.bind(this); 504 | this._boundWrapperHandler = this.wrapUp.bind(this); 505 | this.registerListeners(); 506 | }, 507 | checkForEscapeOrReturn: function(e) { 508 | if (!this._editing || e.ctrlKey || e.altKey || e.shiftKey) return; 509 | if (Event.KEY_ESC == e.keyCode) 510 | this.handleFormCancellation(e); 511 | else if (Event.KEY_RETURN == e.keyCode) 512 | this.handleFormSubmission(e); 513 | }, 514 | createControl: function(mode, handler, extraClasses) { 515 | var control = this.options[mode + 'Control']; 516 | var text = this.options[mode + 'Text']; 517 | if ('button' == control) { 518 | var btn = document.createElement('input'); 519 | btn.type = 'submit'; 520 | btn.value = text; 521 | btn.className = 'editor_' + mode + '_button'; 522 | if ('cancel' == mode) 523 | btn.onclick = this._boundCancelHandler; 524 | this._form.appendChild(btn); 525 | this._controls[mode] = btn; 526 | } else if ('link' == control) { 527 | var link = document.createElement('a'); 528 | link.href = '#'; 529 | link.appendChild(document.createTextNode(text)); 530 | link.onclick = 'cancel' == mode ? this._boundCancelHandler : this._boundSubmitHandler; 531 | link.className = 'editor_' + mode + '_link'; 532 | if (extraClasses) 533 | link.className += ' ' + extraClasses; 534 | this._form.appendChild(link); 535 | this._controls[mode] = link; 536 | } 537 | }, 538 | createEditField: function() { 539 | var text = (this.options.loadTextURL ? this.options.loadingText : this.getText()); 540 | var fld; 541 | if (1 >= this.options.rows && !/\r|\n/.test(this.getText())) { 542 | fld = document.createElement('input'); 543 | fld.type = 'text'; 544 | var size = this.options.size || this.options.cols || 0; 545 | if (0 < size) fld.size = size; 546 | } else { 547 | fld = document.createElement('textarea'); 548 | fld.rows = (1 >= this.options.rows ? this.options.autoRows : this.options.rows); 549 | fld.cols = this.options.cols || 40; 550 | } 551 | fld.name = this.options.paramName; 552 | fld.value = text; // No HTML breaks conversion anymore 553 | fld.className = 'editor_field'; 554 | if (this.options.submitOnBlur) 555 | fld.onblur = this._boundSubmitHandler; 556 | this._controls.editor = fld; 557 | if (this.options.loadTextURL) 558 | this.loadExternalText(); 559 | this._form.appendChild(this._controls.editor); 560 | }, 561 | createForm: function() { 562 | var ipe = this; 563 | function addText(mode, condition) { 564 | var text = ipe.options['text' + mode + 'Controls']; 565 | if (!text || condition === false) return; 566 | ipe._form.appendChild(document.createTextNode(text)); 567 | }; 568 | this._form = $(document.createElement('form')); 569 | this._form.id = this.options.formId; 570 | this._form.addClassName(this.options.formClassName); 571 | this._form.onsubmit = this._boundSubmitHandler; 572 | this.createEditField(); 573 | if ('textarea' == this._controls.editor.tagName.toLowerCase()) 574 | this._form.appendChild(document.createElement('br')); 575 | if (this.options.onFormCustomization) 576 | this.options.onFormCustomization(this, this._form); 577 | addText('Before', this.options.okControl || this.options.cancelControl); 578 | this.createControl('ok', this._boundSubmitHandler); 579 | addText('Between', this.options.okControl && this.options.cancelControl); 580 | this.createControl('cancel', this._boundCancelHandler, 'editor_cancel'); 581 | addText('After', this.options.okControl || this.options.cancelControl); 582 | }, 583 | destroy: function() { 584 | if (this._oldInnerHTML) 585 | this.element.innerHTML = this._oldInnerHTML; 586 | this.leaveEditMode(); 587 | this.unregisterListeners(); 588 | }, 589 | enterEditMode: function(e) { 590 | if (this._saving || this._editing) return; 591 | this._editing = true; 592 | this.triggerCallback('onEnterEditMode'); 593 | if (this.options.externalControl) 594 | this.options.externalControl.hide(); 595 | this.element.hide(); 596 | this.createForm(); 597 | this.element.parentNode.insertBefore(this._form, this.element); 598 | if (!this.options.loadTextURL) 599 | this.postProcessEditField(); 600 | if (e) Event.stop(e); 601 | }, 602 | enterHover: function(e) { 603 | if (this.options.hoverClassName) 604 | this.element.addClassName(this.options.hoverClassName); 605 | if (this._saving) return; 606 | this.triggerCallback('onEnterHover'); 607 | }, 608 | getText: function() { 609 | return this.element.innerHTML.unescapeHTML(); 610 | }, 611 | handleAJAXFailure: function(transport) { 612 | this.triggerCallback('onFailure', transport); 613 | if (this._oldInnerHTML) { 614 | this.element.innerHTML = this._oldInnerHTML; 615 | this._oldInnerHTML = null; 616 | } 617 | }, 618 | handleFormCancellation: function(e) { 619 | this.wrapUp(); 620 | if (e) Event.stop(e); 621 | }, 622 | handleFormSubmission: function(e) { 623 | var form = this._form; 624 | var value = $F(this._controls.editor); 625 | this.prepareSubmission(); 626 | var params = this.options.callback(form, value) || ''; 627 | if (Object.isString(params)) 628 | params = params.toQueryParams(); 629 | params.editorId = this.element.id; 630 | if (this.options.htmlResponse) { 631 | var options = Object.extend({ evalScripts: true }, this.options.ajaxOptions); 632 | Object.extend(options, { 633 | parameters: params, 634 | onComplete: this._boundWrapperHandler, 635 | onFailure: this._boundFailureHandler 636 | }); 637 | new Ajax.Updater({ success: this.element }, this.url, options); 638 | } else { 639 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); 640 | Object.extend(options, { 641 | parameters: params, 642 | onComplete: this._boundWrapperHandler, 643 | onFailure: this._boundFailureHandler 644 | }); 645 | new Ajax.Request(this.url, options); 646 | } 647 | if (e) Event.stop(e); 648 | }, 649 | leaveEditMode: function() { 650 | this.element.removeClassName(this.options.savingClassName); 651 | this.removeForm(); 652 | this.leaveHover(); 653 | this.element.style.backgroundColor = this._originalBackground; 654 | this.element.show(); 655 | if (this.options.externalControl) 656 | this.options.externalControl.show(); 657 | this._saving = false; 658 | this._editing = false; 659 | this._oldInnerHTML = null; 660 | this.triggerCallback('onLeaveEditMode'); 661 | }, 662 | leaveHover: function(e) { 663 | if (this.options.hoverClassName) 664 | this.element.removeClassName(this.options.hoverClassName); 665 | if (this._saving) return; 666 | this.triggerCallback('onLeaveHover'); 667 | }, 668 | loadExternalText: function() { 669 | this._form.addClassName(this.options.loadingClassName); 670 | this._controls.editor.disabled = true; 671 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); 672 | Object.extend(options, { 673 | parameters: 'editorId=' + encodeURIComponent(this.element.id), 674 | onComplete: Prototype.emptyFunction, 675 | onSuccess: function(transport) { 676 | this._form.removeClassName(this.options.loadingClassName); 677 | var text = transport.responseText; 678 | if (this.options.stripLoadedTextTags) 679 | text = text.stripTags(); 680 | this._controls.editor.value = text; 681 | this._controls.editor.disabled = false; 682 | this.postProcessEditField(); 683 | }.bind(this), 684 | onFailure: this._boundFailureHandler 685 | }); 686 | new Ajax.Request(this.options.loadTextURL, options); 687 | }, 688 | postProcessEditField: function() { 689 | var fpc = this.options.fieldPostCreation; 690 | if (fpc) 691 | $(this._controls.editor)['focus' == fpc ? 'focus' : 'activate'](); 692 | }, 693 | prepareOptions: function() { 694 | this.options = Object.clone(Ajax.InPlaceEditor.DefaultOptions); 695 | Object.extend(this.options, Ajax.InPlaceEditor.DefaultCallbacks); 696 | [this._extraDefaultOptions].flatten().compact().each(function(defs) { 697 | Object.extend(this.options, defs); 698 | }.bind(this)); 699 | }, 700 | prepareSubmission: function() { 701 | this._saving = true; 702 | this.removeForm(); 703 | this.leaveHover(); 704 | this.showSaving(); 705 | }, 706 | registerListeners: function() { 707 | this._listeners = { }; 708 | var listener; 709 | $H(Ajax.InPlaceEditor.Listeners).each(function(pair) { 710 | listener = this[pair.value].bind(this); 711 | this._listeners[pair.key] = listener; 712 | if (!this.options.externalControlOnly) 713 | this.element.observe(pair.key, listener); 714 | if (this.options.externalControl) 715 | this.options.externalControl.observe(pair.key, listener); 716 | }.bind(this)); 717 | }, 718 | removeForm: function() { 719 | if (!this._form) return; 720 | this._form.remove(); 721 | this._form = null; 722 | this._controls = { }; 723 | }, 724 | showSaving: function() { 725 | this._oldInnerHTML = this.element.innerHTML; 726 | this.element.innerHTML = this.options.savingText; 727 | this.element.addClassName(this.options.savingClassName); 728 | this.element.style.backgroundColor = this._originalBackground; 729 | this.element.show(); 730 | }, 731 | triggerCallback: function(cbName, arg) { 732 | if ('function' == typeof this.options[cbName]) { 733 | this.options[cbName](this, arg); 734 | } 735 | }, 736 | unregisterListeners: function() { 737 | $H(this._listeners).each(function(pair) { 738 | if (!this.options.externalControlOnly) 739 | this.element.stopObserving(pair.key, pair.value); 740 | if (this.options.externalControl) 741 | this.options.externalControl.stopObserving(pair.key, pair.value); 742 | }.bind(this)); 743 | }, 744 | wrapUp: function(transport) { 745 | this.leaveEditMode(); 746 | // Can't use triggerCallback due to backward compatibility: requires 747 | // binding + direct element 748 | this._boundComplete(transport, this.element); 749 | } 750 | }); 751 | 752 | Object.extend(Ajax.InPlaceEditor.prototype, { 753 | dispose: Ajax.InPlaceEditor.prototype.destroy 754 | }); 755 | 756 | Ajax.InPlaceCollectionEditor = Class.create(Ajax.InPlaceEditor, { 757 | initialize: function($super, element, url, options) { 758 | this._extraDefaultOptions = Ajax.InPlaceCollectionEditor.DefaultOptions; 759 | $super(element, url, options); 760 | }, 761 | 762 | createEditField: function() { 763 | var list = document.createElement('select'); 764 | list.name = this.options.paramName; 765 | list.size = 1; 766 | this._controls.editor = list; 767 | this._collection = this.options.collection || []; 768 | if (this.options.loadCollectionURL) 769 | this.loadCollection(); 770 | else 771 | this.checkForExternalText(); 772 | this._form.appendChild(this._controls.editor); 773 | }, 774 | 775 | loadCollection: function() { 776 | this._form.addClassName(this.options.loadingClassName); 777 | this.showLoadingText(this.options.loadingCollectionText); 778 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); 779 | Object.extend(options, { 780 | parameters: 'editorId=' + encodeURIComponent(this.element.id), 781 | onComplete: Prototype.emptyFunction, 782 | onSuccess: function(transport) { 783 | var js = transport.responseText.strip(); 784 | if (!/^\[.*\]$/.test(js)) // TODO: improve sanity check 785 | throw('Server returned an invalid collection representation.'); 786 | this._collection = eval(js); 787 | this.checkForExternalText(); 788 | }.bind(this), 789 | onFailure: this.onFailure 790 | }); 791 | new Ajax.Request(this.options.loadCollectionURL, options); 792 | }, 793 | 794 | showLoadingText: function(text) { 795 | this._controls.editor.disabled = true; 796 | var tempOption = this._controls.editor.firstChild; 797 | if (!tempOption) { 798 | tempOption = document.createElement('option'); 799 | tempOption.value = ''; 800 | this._controls.editor.appendChild(tempOption); 801 | tempOption.selected = true; 802 | } 803 | tempOption.update((text || '').stripScripts().stripTags()); 804 | }, 805 | 806 | checkForExternalText: function() { 807 | this._text = this.getText(); 808 | if (this.options.loadTextURL) 809 | this.loadExternalText(); 810 | else 811 | this.buildOptionList(); 812 | }, 813 | 814 | loadExternalText: function() { 815 | this.showLoadingText(this.options.loadingText); 816 | var options = Object.extend({ method: 'get' }, this.options.ajaxOptions); 817 | Object.extend(options, { 818 | parameters: 'editorId=' + encodeURIComponent(this.element.id), 819 | onComplete: Prototype.emptyFunction, 820 | onSuccess: function(transport) { 821 | this._text = transport.responseText.strip(); 822 | this.buildOptionList(); 823 | }.bind(this), 824 | onFailure: this.onFailure 825 | }); 826 | new Ajax.Request(this.options.loadTextURL, options); 827 | }, 828 | 829 | buildOptionList: function() { 830 | this._form.removeClassName(this.options.loadingClassName); 831 | this._collection = this._collection.map(function(entry) { 832 | return 2 === entry.length ? entry : [entry, entry].flatten(); 833 | }); 834 | var marker = ('value' in this.options) ? this.options.value : this._text; 835 | var textFound = this._collection.any(function(entry) { 836 | return entry[0] == marker; 837 | }.bind(this)); 838 | this._controls.editor.update(''); 839 | var option; 840 | this._collection.each(function(entry, index) { 841 | option = document.createElement('option'); 842 | option.value = entry[0]; 843 | option.selected = textFound ? entry[0] == marker : 0 == index; 844 | option.appendChild(document.createTextNode(entry[1])); 845 | this._controls.editor.appendChild(option); 846 | }.bind(this)); 847 | this._controls.editor.disabled = false; 848 | Field.scrollFreeActivate(this._controls.editor); 849 | } 850 | }); 851 | 852 | //**** DEPRECATION LAYER FOR InPlace[Collection]Editor! **** 853 | //**** This only exists for a while, in order to let **** 854 | //**** users adapt to the new API. Read up on the new **** 855 | //**** API and convert your code to it ASAP! **** 856 | 857 | Ajax.InPlaceEditor.prototype.initialize.dealWithDeprecatedOptions = function(options) { 858 | if (!options) return; 859 | function fallback(name, expr) { 860 | if (name in options || expr === undefined) return; 861 | options[name] = expr; 862 | }; 863 | fallback('cancelControl', (options.cancelLink ? 'link' : (options.cancelButton ? 'button' : 864 | options.cancelLink == options.cancelButton == false ? false : undefined))); 865 | fallback('okControl', (options.okLink ? 'link' : (options.okButton ? 'button' : 866 | options.okLink == options.okButton == false ? false : undefined))); 867 | fallback('highlightColor', options.highlightcolor); 868 | fallback('highlightEndColor', options.highlightendcolor); 869 | }; 870 | 871 | Object.extend(Ajax.InPlaceEditor, { 872 | DefaultOptions: { 873 | ajaxOptions: { }, 874 | autoRows: 3, // Use when multi-line w/ rows == 1 875 | cancelControl: 'link', // 'link'|'button'|false 876 | cancelText: 'cancel', 877 | clickToEditText: 'Click to edit', 878 | externalControl: null, // id|elt 879 | externalControlOnly: false, 880 | fieldPostCreation: 'activate', // 'activate'|'focus'|false 881 | formClassName: 'inplaceeditor-form', 882 | formId: null, // id|elt 883 | highlightColor: '#ffff99', 884 | highlightEndColor: '#ffffff', 885 | hoverClassName: '', 886 | htmlResponse: true, 887 | loadingClassName: 'inplaceeditor-loading', 888 | loadingText: 'Loading...', 889 | okControl: 'button', // 'link'|'button'|false 890 | okText: 'ok', 891 | paramName: 'value', 892 | rows: 1, // If 1 and multi-line, uses autoRows 893 | savingClassName: 'inplaceeditor-saving', 894 | savingText: 'Saving...', 895 | size: 0, 896 | stripLoadedTextTags: false, 897 | submitOnBlur: false, 898 | textAfterControls: '', 899 | textBeforeControls: '', 900 | textBetweenControls: '' 901 | }, 902 | DefaultCallbacks: { 903 | callback: function(form) { 904 | return Form.serialize(form); 905 | }, 906 | onComplete: function(transport, element) { 907 | // For backward compatibility, this one is bound to the IPE, and passes 908 | // the element directly. It was too often customized, so we don't break it. 909 | new Effect.Highlight(element, { 910 | startcolor: this.options.highlightColor, keepBackgroundImage: true }); 911 | }, 912 | onEnterEditMode: null, 913 | onEnterHover: function(ipe) { 914 | ipe.element.style.backgroundColor = ipe.options.highlightColor; 915 | if (ipe._effect) 916 | ipe._effect.cancel(); 917 | }, 918 | onFailure: function(transport, ipe) { 919 | alert('Error communication with the server: ' + transport.responseText.stripTags()); 920 | }, 921 | onFormCustomization: null, // Takes the IPE and its generated form, after editor, before controls. 922 | onLeaveEditMode: null, 923 | onLeaveHover: function(ipe) { 924 | ipe._effect = new Effect.Highlight(ipe.element, { 925 | startcolor: ipe.options.highlightColor, endcolor: ipe.options.highlightEndColor, 926 | restorecolor: ipe._originalBackground, keepBackgroundImage: true 927 | }); 928 | } 929 | }, 930 | Listeners: { 931 | click: 'enterEditMode', 932 | keydown: 'checkForEscapeOrReturn', 933 | mouseover: 'enterHover', 934 | mouseout: 'leaveHover' 935 | } 936 | }); 937 | 938 | Ajax.InPlaceCollectionEditor.DefaultOptions = { 939 | loadingCollectionText: 'Loading options...' 940 | }; 941 | 942 | // Delayed observer, like Form.Element.Observer, 943 | // but waits for delay after last key input 944 | // Ideal for live-search fields 945 | 946 | Form.Element.DelayedObserver = Class.create({ 947 | initialize: function(element, delay, callback) { 948 | this.delay = delay || 0.5; 949 | this.element = $(element); 950 | this.callback = callback; 951 | this.timer = null; 952 | this.lastValue = $F(this.element); 953 | Event.observe(this.element,'keyup',this.delayedListener.bindAsEventListener(this)); 954 | }, 955 | delayedListener: function(event) { 956 | if(this.lastValue == $F(this.element)) return; 957 | if(this.timer) clearTimeout(this.timer); 958 | this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay * 1000); 959 | this.lastValue = $F(this.element); 960 | }, 961 | onTimerEvent: function() { 962 | this.timer = null; 963 | this.callback(this.element, $F(this.element)); 964 | } 965 | }); --------------------------------------------------------------------------------