flms 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +3 -0
- data/Rakefile +33 -0
- data/app/assets/images/img/glyphicons-halflings-white.png +0 -0
- data/app/assets/images/img/glyphicons-halflings.png +0 -0
- data/app/assets/javascripts/flms/blocks/index.coffee +6 -0
- data/app/assets/javascripts/flms/bootstrap.js +6 -0
- data/app/assets/javascripts/flms/flms.js +17 -0
- data/app/assets/javascripts/flms/layout.coffee +8 -0
- data/app/assets/stylesheets/flms/blocks/index.sass +28 -0
- data/app/assets/stylesheets/flms/bootstrap.css +6159 -0
- data/app/assets/stylesheets/flms/flms.css +7 -0
- data/app/assets/stylesheets/flms/home.sass +30 -0
- data/app/assets/stylesheets/flms/layout.sass +19 -0
- data/app/assets/stylesheets/flms/login.sass +32 -0
- data/app/controllers/flms/admin_controller.rb +11 -0
- data/app/controllers/flms/application_controller.rb +4 -0
- data/app/controllers/flms/blocks_controller.rb +55 -0
- data/app/controllers/flms/dashboard_controller.rb +13 -0
- data/app/controllers/flms/pages_controller.rb +47 -0
- data/app/controllers/flms/sessions_controller.rb +16 -0
- data/app/controllers/flms/users_controller.rb +31 -0
- data/app/helpers/flms/application_helper.rb +4 -0
- data/app/models/flms/block.rb +10 -0
- data/app/models/flms/blocks_page.rb +8 -0
- data/app/models/flms/page.rb +14 -0
- data/app/models/flms/user.rb +10 -0
- data/app/views/flms/blocks/_form.html.haml +25 -0
- data/app/views/flms/blocks/edit.html.haml +3 -0
- data/app/views/flms/blocks/index.html.haml +41 -0
- data/app/views/flms/blocks/new.html.haml +2 -0
- data/app/views/flms/blocks/show.html.haml +1 -0
- data/app/views/flms/dashboard/index.html.haml +5 -0
- data/app/views/flms/pages/_form.html.haml +30 -0
- data/app/views/flms/pages/edit.html.haml +3 -0
- data/app/views/flms/pages/index.html.haml +21 -0
- data/app/views/flms/pages/new.html.haml +2 -0
- data/app/views/flms/pages/show.html.haml +8 -0
- data/app/views/flms/sessions/new.html.haml +21 -0
- data/app/views/flms/users/_form.html.haml +28 -0
- data/app/views/flms/users/index.html.haml +22 -0
- data/app/views/flms/users/new.html.haml +3 -0
- data/app/views/layouts/flms/admin.html.haml +40 -0
- data/app/views/layouts/flms/admin_login.html.haml +18 -0
- data/app/views/layouts/flms/application.html.erb +14 -0
- data/config/initializers/devise.rb +243 -0
- data/config/locales/devise.en.yml +59 -0
- data/config/routes.rb +23 -0
- data/db/migrate/20130208224914_flms_devise_create_users.rb +47 -0
- data/db/migrate/20130216032241_flms_create_pages.rb +12 -0
- data/db/migrate/20130302011705_create_flms_blocks.rb +10 -0
- data/db/migrate/20130302015459_create_flms_blocks_pages.rb +12 -0
- data/lib/flms.rb +7 -0
- data/lib/flms/engine.rb +5 -0
- data/lib/flms/version.rb +3 -0
- data/lib/tasks/flms_tasks.rake +26 -0
- data/spec/controllers/blocks_controller_spec.rb +61 -0
- data/spec/controllers/dashboard_controller_spec.rb +12 -0
- data/spec/controllers/pages_controller_spec.rb +60 -0
- data/spec/controllers/users_controller_spec.rb +37 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +12 -0
- data/spec/dummy/app/controllers/home_controller.rb +4 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/user.rb +8 -0
- data/spec/dummy/app/views/home/index.html.haml +10 -0
- data/spec/dummy/app/views/layouts/application.html.haml +10 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +69 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +52 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/devise.rb +244 -0
- data/spec/dummy/config/initializers/flms.rb +1 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/db/schema.rb +60 -0
- data/spec/dummy/log/development.log +39893 -0
- data/spec/dummy/log/test.log +42855 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/tmp/cache/assets/C9D/180/sprockets%2Fad74b633d2447502477133032afac84b +0 -0
- data/spec/dummy/tmp/cache/assets/CAB/C80/sprockets%2Fc4e739111d82c7189113aeb228816ba8 +0 -0
- data/spec/dummy/tmp/cache/assets/CB9/E30/sprockets%2F588cf5763c29e490d026f24e8aa90343 +0 -0
- data/spec/dummy/tmp/cache/assets/CCF/210/sprockets%2F43b5c2ffc8a62726b4ed6234001571a4 +0 -0
- data/spec/dummy/tmp/cache/assets/CDB/510/sprockets%2Fd61745a24c4636f8813fa14038bac69a +0 -0
- data/spec/dummy/tmp/cache/assets/CDE/D90/sprockets%2Fcc733e310d9646637216c4ffe109ea35 +0 -0
- data/spec/dummy/tmp/cache/assets/CE4/6B0/sprockets%2F6e38aec803936176be248f507eb3432d +0 -0
- data/spec/dummy/tmp/cache/assets/CED/6A0/sprockets%2Fcc221f5201d658cf456836f8b5547f9d +0 -0
- data/spec/dummy/tmp/cache/assets/CF6/740/sprockets%2F0cb88349ae46593e396c4bd68854ed08 +0 -0
- data/spec/dummy/tmp/cache/assets/D01/E80/sprockets%2Fc1a3694915166d6a041afa4bb062fd84 +0 -0
- data/spec/dummy/tmp/cache/assets/D06/AF0/sprockets%2F02a9a4f214368781ba86ed42d5bef203 +0 -0
- data/spec/dummy/tmp/cache/assets/D07/250/sprockets%2Fa8bf27de087b582dc454910a060e0b84 +0 -0
- data/spec/dummy/tmp/cache/assets/D09/5C0/sprockets%2F7cd3521c63623eb6f02dbb514991a6f6 +0 -0
- data/spec/dummy/tmp/cache/assets/D0C/570/sprockets%2F7e3d63e1412ae56b44b23d95547f0cc7 +0 -0
- data/spec/dummy/tmp/cache/assets/D0D/510/sprockets%2Fa189f1a8718ff11152feb3c43c75b841 +0 -0
- data/spec/dummy/tmp/cache/assets/D1A/3F0/sprockets%2F84181c4b293f3a1c4efe9226652f9e6e +0 -0
- data/spec/dummy/tmp/cache/assets/D1E/1D0/sprockets%2Ffeec495723d877b92c74db2055516bc9 +0 -0
- data/spec/dummy/tmp/cache/assets/D2E/F10/sprockets%2F156bfd6b405a129f5f003243cf60aea9 +0 -0
- data/spec/dummy/tmp/cache/assets/D35/470/sprockets%2F2c0a647ea89470a8285b9acb2006dbd5 +0 -0
- data/spec/dummy/tmp/cache/assets/D3B/440/sprockets%2Fabe31f236167ba051f6ff849d0a7282f +0 -0
- data/spec/dummy/tmp/cache/assets/D3B/550/sprockets%2Fb61caf9834ac8c31691851ac024f26ef +0 -0
- data/spec/dummy/tmp/cache/assets/D44/A50/sprockets%2F7602cd388554ba54eb914c1b2d7ff68c +0 -0
- data/spec/dummy/tmp/cache/assets/D46/210/sprockets%2F45a8d49c8edbaf91f38ab1495430384c +0 -0
- data/spec/dummy/tmp/cache/assets/D4A/B40/sprockets%2F509d2dc1350975d781c50dffc58ef53f +0 -0
- data/spec/dummy/tmp/cache/assets/D54/DE0/sprockets%2F77af3e306bb966f71f83e90e2a8868db +0 -0
- data/spec/dummy/tmp/cache/assets/D5B/1F0/sprockets%2F4d98e747aa4bb9fc8859f49a45159e1c +0 -0
- data/spec/dummy/tmp/cache/assets/D65/690/sprockets%2F511f71e32f87da06df67250bb39abdc2 +0 -0
- data/spec/dummy/tmp/cache/assets/D6E/870/sprockets%2F3f77e0b5396013fd145a6fdd21bc9b7f +0 -0
- data/spec/dummy/tmp/cache/assets/D74/EA0/sprockets%2Fccfc557971c4d47336cab75db207b89a +0 -0
- data/spec/dummy/tmp/cache/assets/D77/D80/sprockets%2F7ad794c63274a9812dfbabc1dae69692 +0 -0
- data/spec/dummy/tmp/cache/assets/D79/DE0/sprockets%2F9e7131d002f39be972e9918bdfbbcc66 +0 -0
- data/spec/dummy/tmp/cache/assets/D7C/3A0/sprockets%2Fec63717067efd6b2daf3423edf70f689 +0 -0
- data/spec/dummy/tmp/cache/assets/D81/300/sprockets%2F9543b0420abb11b0a126a8384edddafe +0 -0
- data/spec/dummy/tmp/cache/assets/D81/400/sprockets%2F566656e2cc9e698b1dbbd56d6e2e6b36 +0 -0
- data/spec/dummy/tmp/cache/assets/D93/0C0/sprockets%2Fab900c084c9c51c312ee3457cee8cdc2 +0 -0
- data/spec/dummy/tmp/cache/assets/D98/FD0/sprockets%2F6f253bd2bf824ac38c69bfe1418a04be +0 -0
- data/spec/dummy/tmp/cache/assets/D9E/220/sprockets%2Fd129fcf804e89814c2f4abbe0336cba9 +0 -0
- data/spec/dummy/tmp/cache/assets/DA0/9F0/sprockets%2Fd7c0d94bf0df6b0e03f83dcaf8873631 +0 -0
- data/spec/dummy/tmp/cache/assets/DA3/920/sprockets%2F6c45b9f8e0152c78b3c9be1c2e8be53c +0 -0
- data/spec/dummy/tmp/cache/assets/DA5/BC0/sprockets%2F5b86068a872e5bd841750d6cbd2dfdbf +0 -0
- data/spec/dummy/tmp/cache/assets/DA8/4A0/sprockets%2Ff8ba39d58ace11df624bf7e24c7b5882 +0 -0
- data/spec/dummy/tmp/cache/assets/DA8/C30/sprockets%2Fb7d14d588a70a56d7ddac542eef557d6 +0 -0
- data/spec/dummy/tmp/cache/assets/DA9/370/sprockets%2Fdf257d496b11ea9c1bb7ee139d68c83e +0 -0
- data/spec/dummy/tmp/cache/assets/DAC/D80/sprockets%2Fb8ca14c15e0301b935aaaef327bdc40a +0 -0
- data/spec/dummy/tmp/cache/assets/DAE/F10/sprockets%2F46950fdbeed95ba83ca8e7215194ddf8 +0 -0
- data/spec/dummy/tmp/cache/assets/DB2/1F0/sprockets%2F9fe57997f92efbee3b03df4510a2df57 +0 -0
- data/spec/dummy/tmp/cache/assets/DB8/C30/sprockets%2Fca860af2d10e0c745b6e38b12dabe05f +0 -0
- data/spec/dummy/tmp/cache/assets/DC1/530/sprockets%2F4db0b289eee7dad03453a5924c20dcfa +0 -0
- data/spec/dummy/tmp/cache/assets/DC6/7D0/sprockets%2Ff7689ac5fafbf683c537892e6ff7f8c3 +0 -0
- data/spec/dummy/tmp/cache/assets/DCC/B80/sprockets%2Fb7ae6e7bfa8984c54a4dfaf0011617cd +0 -0
- data/spec/dummy/tmp/cache/assets/DCE/790/sprockets%2Fff77d67febbb3af024f721c0bb4c4749 +0 -0
- data/spec/dummy/tmp/cache/assets/DCF/0B0/sprockets%2F23db7ab96a5549546bbdd0bc2e6ea86f +0 -0
- data/spec/dummy/tmp/cache/assets/DD5/440/sprockets%2F9b3488ba6d62c3dcc809f8cd3dc2d1f5 +0 -0
- data/spec/dummy/tmp/cache/assets/DE6/B10/sprockets%2Fa0adf3fb2b0054d04dd44c08aff1d6c8 +0 -0
- data/spec/dummy/tmp/cache/assets/DF1/A10/sprockets%2Fa925a815ca0bff4d6f42fbdf0d323a4f +0 -0
- data/spec/dummy/tmp/cache/assets/DFA/FF0/sprockets%2F75699fed9ddaecd89464492eeeef90d8 +0 -0
- data/spec/dummy/tmp/cache/assets/DFD/540/sprockets%2F181ef7a7c275dd2872aa1ed1fcfab75f +0 -0
- data/spec/dummy/tmp/cache/assets/E0E/B50/sprockets%2Fe78fa5fc4cfb0979876d23ae1b1f3ffd +0 -0
- data/spec/dummy/tmp/cache/assets/E3A/110/sprockets%2Fb6d5158ff7ac6d2ca99bfe56a7ddd2e3 +0 -0
- data/spec/dummy/tmp/cache/assets/E6D/8B0/sprockets%2F9edfdf7e85cdd1a69b0eb14c6ce8ed19 +0 -0
- data/spec/factories/blocks.rb +6 -0
- data/spec/factories/pages.rb +6 -0
- data/spec/factories/users.rb +7 -0
- data/spec/features/blocks/create_spec.rb +19 -0
- data/spec/features/blocks/delete_spec.rb +17 -0
- data/spec/features/blocks/edit_spec.rb +20 -0
- data/spec/features/blocks/show_spec.rb +14 -0
- data/spec/features/dashboard_spec.rb +11 -0
- data/spec/features/login_spec.rb +50 -0
- data/spec/features/pages/create_spec.rb +18 -0
- data/spec/features/pages/delete_spec.rb +14 -0
- data/spec/features/pages/index_spec.rb +14 -0
- data/spec/features/pages/update_spec.rb +17 -0
- data/spec/features/users/create_spec.rb +20 -0
- data/spec/features/users/delete_spec.rb +13 -0
- data/spec/features/users/index_spec.rb +16 -0
- data/spec/flms_spec.rb +7 -0
- data/spec/models/block_spec.rb +14 -0
- data/spec/models/page_spec.rb +15 -0
- data/spec/models/user_spec.rb +5 -0
- data/spec/spec_helper.rb +53 -0
- data/spec/support/access_control.rb +87 -0
- data/spec/support/capybara_helpers.rb +7 -0
- data/spec/support/lets.rb +8 -0
- data/vendor/assets/javascripts/bootstrapSwitch.js +238 -0
- data/vendor/assets/javascripts/jquery-ui.js +2221 -0
- data/vendor/assets/stylesheets/bootstrapSwitch.css +210 -0
- data/vendor/assets/stylesheets/jquery-ui.css +88 -0
- metadata +607 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
feature 'Users > Create' do
|
4
|
+
test_helpers
|
5
|
+
|
6
|
+
scenario 'creates new users' do
|
7
|
+
capybara_sign_in user_1
|
8
|
+
visit '/flms/users/new'
|
9
|
+
|
10
|
+
expect(page).to have_content('New User')
|
11
|
+
|
12
|
+
fill_in 'Email', with: 'foo@bar.com'
|
13
|
+
fill_in 'Password', with: '12345678'
|
14
|
+
click_button 'Create User'
|
15
|
+
expect(Flms::User).to have(2).instances
|
16
|
+
foo_user = Flms::User.find_by_email 'foo@bar.com'
|
17
|
+
expect(foo_user).to_not be_nil
|
18
|
+
expect(foo_user.email).to eql 'foo@bar.com'
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
feature 'Users > Delete' do
|
4
|
+
test_helpers
|
5
|
+
|
6
|
+
scenario 'allows to delete a new page', js: true do
|
7
|
+
capybara_sign_in user_1
|
8
|
+
visit "/flms/users"
|
9
|
+
page.evaluate_script('window.confirm = function() { return true; }')
|
10
|
+
click_link 'delete'
|
11
|
+
expect(Flms::User).to have(0).instances
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
feature 'Users > Index' do
|
4
|
+
test_helpers
|
5
|
+
|
6
|
+
scenario 'lists all existing users' do
|
7
|
+
user_1 ; user_2
|
8
|
+
|
9
|
+
capybara_sign_in user_1
|
10
|
+
visit '/flms/users'
|
11
|
+
|
12
|
+
expect(page).to have_content(user_1.email)
|
13
|
+
expect(page).to have_content(user_2.email)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/spec/flms_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
|
+
|
4
|
+
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
5
|
+
require "rails/test_help"
|
6
|
+
require 'rspec/rails'
|
7
|
+
require 'rspec/autorun'
|
8
|
+
require 'factory_girl_rails'
|
9
|
+
require 'faker'
|
10
|
+
require 'database_cleaner'
|
11
|
+
require 'capybara/rails'
|
12
|
+
require 'capybara/rspec'
|
13
|
+
|
14
|
+
Rails.backtrace_cleaner.remove_silencers!
|
15
|
+
Dir["#{File.dirname __FILE__}/support/**/*.rb"].each {|f| require f}
|
16
|
+
|
17
|
+
RSpec.configure do |config|
|
18
|
+
|
19
|
+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
20
|
+
# examples within a transaction, remove the following line or assign false
|
21
|
+
# instead of true.
|
22
|
+
config.use_transactional_fixtures = false
|
23
|
+
|
24
|
+
# If true, the base class of anonymous controllers will be inferred
|
25
|
+
# automatically. This will be the default behavior in future versions of
|
26
|
+
# rspec-rails.
|
27
|
+
config.infer_base_class_for_anonymous_controllers = false
|
28
|
+
|
29
|
+
# Run specs in random order to surface order dependencies. If you find an
|
30
|
+
# order dependency and want to debug it, you can fix the order by providing
|
31
|
+
# the seed, which is printed after each run.
|
32
|
+
# --seed 1234
|
33
|
+
#config.order = "random"
|
34
|
+
|
35
|
+
config.include Devise::TestHelpers, type: :controller
|
36
|
+
config.include FactoryGirl::Syntax::Methods
|
37
|
+
config.include CapybaraHelpers
|
38
|
+
|
39
|
+
DatabaseCleaner.strategy = :deletion
|
40
|
+
|
41
|
+
config.before(:suite) do
|
42
|
+
DatabaseCleaner.clean_with :deletion
|
43
|
+
end
|
44
|
+
|
45
|
+
config.before(:each) do
|
46
|
+
DatabaseCleaner.start
|
47
|
+
end
|
48
|
+
|
49
|
+
config.after(:each) do
|
50
|
+
DatabaseCleaner.clean
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,87 @@
|
|
1
|
+
#####
|
2
|
+
# Complex access control tests.
|
3
|
+
#
|
4
|
+
|
5
|
+
|
6
|
+
# Verifies that the given action is accessible to the 'user' account and admins.
|
7
|
+
shared_examples 'an action accessible only to logged-in users' do
|
8
|
+
it_should_behave_like 'an action accessible to the user'
|
9
|
+
it_should_behave_like 'an action not accessible to the world'
|
10
|
+
end
|
11
|
+
|
12
|
+
# Verifies that the given action is accessible to everybody.
|
13
|
+
shared_examples 'an action accessible without login' do
|
14
|
+
it_should_behave_like 'an action accessible to the user'
|
15
|
+
it_should_behave_like 'an action accessible to the world'
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
#####
|
20
|
+
# Individual access control tests.
|
21
|
+
#
|
22
|
+
|
23
|
+
|
24
|
+
# Verifies that the 'user' user can perform the current action.
|
25
|
+
shared_examples 'an action accessible to the user' do
|
26
|
+
before :each do
|
27
|
+
sign_in user_1
|
28
|
+
request
|
29
|
+
end
|
30
|
+
it_should_behave_like 'http access granted'
|
31
|
+
it_should_behave_like 'database changed'
|
32
|
+
end
|
33
|
+
|
34
|
+
# Verifies that the given action can be performed without login.
|
35
|
+
shared_examples 'an action accessible to the world' do
|
36
|
+
before :each do
|
37
|
+
request
|
38
|
+
end
|
39
|
+
it_should_behave_like 'http access granted'
|
40
|
+
it_should_behave_like 'database changed'
|
41
|
+
end
|
42
|
+
|
43
|
+
# Verifies that the given action cannot be performed without login.
|
44
|
+
shared_examples 'an action not accessible to the world' do
|
45
|
+
before :each do
|
46
|
+
request
|
47
|
+
end
|
48
|
+
it_should_behave_like 'http access denied'
|
49
|
+
it_should_behave_like 'database not changed'
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
#####
|
55
|
+
# Private implementation details.
|
56
|
+
#
|
57
|
+
|
58
|
+
# Verifies that the http response confirms access granted.
|
59
|
+
shared_examples 'http access granted' do
|
60
|
+
it do
|
61
|
+
response = defined?(access_granted_check) ? access_granted_check : default_access_granted_check
|
62
|
+
expect(response).to be_true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Verifies that the http response denied access.
|
67
|
+
shared_examples 'http access denied' do
|
68
|
+
it do
|
69
|
+
response = defined?(access_granted_check) ? access_granted_check : default_access_granted_check
|
70
|
+
expect(response).to be_false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Verifies that the database changes were performed.
|
75
|
+
shared_examples 'database changed' do
|
76
|
+
it do
|
77
|
+
expect(database_performed_check).to be_true if defined? database_performed_check
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Verifies that the database changes were not performed.
|
82
|
+
shared_examples 'database not changed' do
|
83
|
+
it do
|
84
|
+
expect(database_performed_check).to be_false if defined? database_performed_check
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
@@ -0,0 +1,238 @@
|
|
1
|
+
/* ============================================================
|
2
|
+
* bootstrapSwitch v1.2 by Larentis Mattia @spiritualGuru
|
3
|
+
* http://www.larentis.eu/switch/
|
4
|
+
* ============================================================
|
5
|
+
* Licensed under the Apache License, Version 2.0
|
6
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
7
|
+
* ============================================================ */
|
8
|
+
|
9
|
+
!function ($) {
|
10
|
+
"use strict";
|
11
|
+
|
12
|
+
$.fn['bootstrapSwitch'] = function (method) {
|
13
|
+
var methods = {
|
14
|
+
init: function () {
|
15
|
+
return this.each(function () {
|
16
|
+
var $element = $(this)
|
17
|
+
, $div
|
18
|
+
, $switchLeft
|
19
|
+
, $switchRight
|
20
|
+
, $label
|
21
|
+
, myClasses = ""
|
22
|
+
, classes = $element.attr('class')
|
23
|
+
, color
|
24
|
+
, moving
|
25
|
+
, onLabel = "ON"
|
26
|
+
, offLabel = "OFF";
|
27
|
+
|
28
|
+
$.each(['switch-mini', 'switch-small', 'switch-large'], function (i, el) {
|
29
|
+
if (classes.indexOf(el) >= 0)
|
30
|
+
myClasses = el;
|
31
|
+
});
|
32
|
+
|
33
|
+
$element.addClass('has-switch');
|
34
|
+
|
35
|
+
if ($element.data('on') !== undefined)
|
36
|
+
color = "switch-" + $element.data('on');
|
37
|
+
|
38
|
+
if ($element.data('on-label') !== undefined)
|
39
|
+
onLabel = $element.data('on-label');
|
40
|
+
|
41
|
+
if ($element.data('off-label') !== undefined)
|
42
|
+
offLabel = $element.data('off-label');
|
43
|
+
|
44
|
+
$switchLeft = $('<span>')
|
45
|
+
.addClass("switch-left")
|
46
|
+
.addClass(myClasses)
|
47
|
+
.addClass(color)
|
48
|
+
.html(onLabel);
|
49
|
+
|
50
|
+
color = '';
|
51
|
+
if ($element.data('off') !== undefined)
|
52
|
+
color = "switch-" + $element.data('off');
|
53
|
+
|
54
|
+
$switchRight = $('<span>')
|
55
|
+
.addClass("switch-right")
|
56
|
+
.addClass(myClasses)
|
57
|
+
.addClass(color)
|
58
|
+
.html(offLabel);
|
59
|
+
|
60
|
+
$label = $('<label>')
|
61
|
+
.html(" ")
|
62
|
+
.addClass(myClasses)
|
63
|
+
.attr('for', $element.find('input').attr('id'));
|
64
|
+
|
65
|
+
$div = $element.find(':checkbox').wrap($('<div>')).parent().data('animated', false);
|
66
|
+
|
67
|
+
if ($element.data('animated') !== false)
|
68
|
+
$div.addClass('switch-animate').data('animated', true);
|
69
|
+
|
70
|
+
$div
|
71
|
+
.append($switchLeft)
|
72
|
+
.append($label)
|
73
|
+
.append($switchRight);
|
74
|
+
|
75
|
+
$element.find('>div').addClass(
|
76
|
+
$element.find('input').is(':checked') ? 'switch-on' : 'switch-off'
|
77
|
+
);
|
78
|
+
|
79
|
+
if ($element.find('input').is(':disabled'))
|
80
|
+
$(this).addClass('deactivate');
|
81
|
+
|
82
|
+
var changeStatus = function ($this) {
|
83
|
+
$this.siblings('label').trigger('mousedown').trigger('mouseup').trigger('click');
|
84
|
+
};
|
85
|
+
|
86
|
+
$element.on('keydown', function (e) {
|
87
|
+
if (e.keyCode === 32) {
|
88
|
+
e.stopImmediatePropagation();
|
89
|
+
e.preventDefault();
|
90
|
+
changeStatus($(e.target).find('span:first'));
|
91
|
+
}
|
92
|
+
});
|
93
|
+
|
94
|
+
$switchLeft.on('click', function (e) {
|
95
|
+
changeStatus($(this));
|
96
|
+
});
|
97
|
+
|
98
|
+
$switchRight.on('click', function (e) {
|
99
|
+
changeStatus($(this));
|
100
|
+
});
|
101
|
+
|
102
|
+
$element.find('input').on('change', function (e) {
|
103
|
+
var $element = $(this).parent();
|
104
|
+
|
105
|
+
e.preventDefault();
|
106
|
+
e.stopImmediatePropagation();
|
107
|
+
|
108
|
+
$element.css('left', '');
|
109
|
+
|
110
|
+
if ($(this).is(':checked'))
|
111
|
+
$element.removeClass('switch-off').addClass('switch-on');
|
112
|
+
else $element.removeClass('switch-on').addClass('switch-off');
|
113
|
+
|
114
|
+
if ($element.data('animated') !== false)
|
115
|
+
$element.addClass("switch-animate");
|
116
|
+
|
117
|
+
$element.parent().trigger('switch-change', {'el': $(this), 'value': $(this).is(':checked')})
|
118
|
+
});
|
119
|
+
|
120
|
+
$element.find('label').on('mousedown touchstart', function (e) {
|
121
|
+
var $this = $(this);
|
122
|
+
moving = false;
|
123
|
+
|
124
|
+
e.preventDefault();
|
125
|
+
e.stopImmediatePropagation();
|
126
|
+
|
127
|
+
$this.closest('div').removeClass('switch-animate');
|
128
|
+
|
129
|
+
if ($this.closest('.switch').is('.deactivate'))
|
130
|
+
$this.unbind('click');
|
131
|
+
else {
|
132
|
+
$this.on('mousemove touchmove', function (e) {
|
133
|
+
var $element = $(this).closest('.switch')
|
134
|
+
, relativeX = (e.pageX || e.originalEvent.targetTouches[0].pageX) - $element.offset().left
|
135
|
+
, percent = (relativeX / $element.width()) * 100
|
136
|
+
, left = 25
|
137
|
+
, right = 75;
|
138
|
+
|
139
|
+
moving = true;
|
140
|
+
|
141
|
+
if (percent < left)
|
142
|
+
percent = left;
|
143
|
+
else if (percent > right)
|
144
|
+
percent = right;
|
145
|
+
|
146
|
+
$element.find('>div').css('left', (percent - right) + "%")
|
147
|
+
});
|
148
|
+
|
149
|
+
$this.on('click touchend', function (e) {
|
150
|
+
var $this = $(this)
|
151
|
+
, $target = $(e.target)
|
152
|
+
, $myCheckBox = $target.siblings('input');
|
153
|
+
|
154
|
+
e.stopImmediatePropagation();
|
155
|
+
e.preventDefault();
|
156
|
+
|
157
|
+
$this.unbind('mouseleave');
|
158
|
+
|
159
|
+
if (moving)
|
160
|
+
$myCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25));
|
161
|
+
else $myCheckBox.prop("checked", !$myCheckBox.is(":checked"));
|
162
|
+
|
163
|
+
moving = false;
|
164
|
+
$myCheckBox.trigger('change');
|
165
|
+
});
|
166
|
+
|
167
|
+
$this.on('mouseleave', function (e) {
|
168
|
+
var $this = $(this)
|
169
|
+
, $myCheckBox = $this.siblings('input');
|
170
|
+
|
171
|
+
e.preventDefault();
|
172
|
+
e.stopImmediatePropagation();
|
173
|
+
|
174
|
+
$this.unbind('mouseleave');
|
175
|
+
$this.trigger('mouseup');
|
176
|
+
|
177
|
+
$myCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25)).trigger('change');
|
178
|
+
});
|
179
|
+
|
180
|
+
$this.on('mouseup', function (e) {
|
181
|
+
e.stopImmediatePropagation();
|
182
|
+
e.preventDefault();
|
183
|
+
|
184
|
+
$(this).unbind('mousemove');
|
185
|
+
});
|
186
|
+
}
|
187
|
+
});
|
188
|
+
}
|
189
|
+
);
|
190
|
+
},
|
191
|
+
toggleActivation: function () {
|
192
|
+
$(this).toggleClass('deactivate');
|
193
|
+
},
|
194
|
+
isActive: function () {
|
195
|
+
return !$(this).hasClass('deactivate');
|
196
|
+
},
|
197
|
+
setActive: function (active) {
|
198
|
+
if (active)
|
199
|
+
$(this).removeClass('deactivate');
|
200
|
+
else $(this).addClass('deactivate');
|
201
|
+
},
|
202
|
+
toggleState: function (skipOnChange) {
|
203
|
+
var $input = $(this).find('input:checkbox');
|
204
|
+
$input.prop('checked', !$input.is(':checked')).trigger('change', skipOnChange);
|
205
|
+
},
|
206
|
+
setState: function (value, skipOnChange) {
|
207
|
+
$(this).find('input:checkbox').prop('checked', value).trigger('change', skipOnChange);
|
208
|
+
},
|
209
|
+
status: function () {
|
210
|
+
return $(this).find('input:checkbox').is(':checked');
|
211
|
+
},
|
212
|
+
destroy: function () {
|
213
|
+
var $div = $(this).find('div')
|
214
|
+
, $checkbox;
|
215
|
+
|
216
|
+
$div.find(':not(input:checkbox)').remove();
|
217
|
+
|
218
|
+
$checkbox = $div.children();
|
219
|
+
$checkbox.unwrap().unwrap();
|
220
|
+
|
221
|
+
$checkbox.unbind('change');
|
222
|
+
|
223
|
+
return $checkbox;
|
224
|
+
}
|
225
|
+
};
|
226
|
+
|
227
|
+
if (methods[method])
|
228
|
+
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
|
229
|
+
else if (typeof method === 'object' || !method)
|
230
|
+
return methods.init.apply(this, arguments);
|
231
|
+
else
|
232
|
+
$.error('Method ' + method + ' does not exist!');
|
233
|
+
};
|
234
|
+
}(jQuery);
|
235
|
+
|
236
|
+
$(function () {
|
237
|
+
$('.switch')['bootstrapSwitch']();
|
238
|
+
});
|