abstracted 0.0.6 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +11 -8
- data/app/assets/javascripts/abstract_resources.js +29 -25
- data/app/assets/javascripts/crud.js.coffee +174 -29
- data/app/assets/javascripts/initializers.js.coffee +403 -58
- data/app/assets/javascripts/pagescrolling.js.coffee +92 -0
- data/app/assets/stylesheets/abstract_resources.css +5 -0
- data/app/assets/stylesheets/scaffold.css +4 -2
- data/app/assets/stylesheets/selectize.css.scss +0 -0
- data/app/controllers/abstract_resources_controller.rb +473 -134
- data/app/controllers/application_controller.rb +22 -7
- data/app/helpers/abstract_resources_helper.rb +515 -9
- data/app/helpers/fab_button_helper.rb +82 -0
- data/app/models/abstract_resource.rb +141 -89
- data/app/models/ancestry_abstract_resource.rb +27 -0
- data/app/models/concerns/roleable.rb +1 -1
- data/app/policies/abstract_resource_policy.rb +25 -0
- data/app/views/abstract_resources/_title.html.haml +2 -0
- data/app/views/abstract_resources/activate.html.haml +5 -0
- data/app/views/abstract_resources/attach.html.haml +6 -0
- data/app/views/abstract_resources/deactivate.html.haml +5 -0
- data/app/views/abstract_resources/defer.html.haml +5 -0
- data/app/views/abstract_resources/destroy.js.haml +2 -1
- data/app/views/abstract_resources/detach.html.haml +5 -0
- data/app/views/abstract_resources/edit.html.haml +4 -9
- data/app/views/abstract_resources/error.html.haml +2 -0
- data/app/views/abstract_resources/error.js.haml +2 -0
- data/app/views/abstract_resources/index.html.haml +27 -0
- data/app/views/abstract_resources/index.js.haml +2 -0
- data/app/views/abstract_resources/new.html.haml +4 -10
- data/app/views/abstract_resources/prefer.html.haml +5 -0
- data/app/views/abstract_resources/print.html.haml +2 -0
- data/app/views/abstract_resources/print.js.haml +2 -0
- data/app/views/abstract_resources/show.html.haml +5 -11
- data/app/views/layouts/_messages.html.haml +1 -0
- data/app/views/layouts/_navigation.html.haml +13 -0
- data/bower.json +34 -0
- data/config/initializers/exception.rb +19 -0
- data/config/initializers/pundit.rb +22 -0
- data/config/routes.rb +24 -0
- data/lib/abstracted.rb +3 -0
- data/lib/abstracted/version.rb +1 -1
- data/lib/abstracted_responder.rb +1 -0
- data/lib/exceptions.rb +61 -0
- data/vendor/assets/components/jquery/.bower.json +38 -0
- data/vendor/assets/components/jquery/MIT-LICENSE.txt +21 -0
- data/vendor/assets/components/jquery/bower.json +28 -0
- data/vendor/assets/components/jquery/dist/jquery.js +9210 -0
- data/vendor/assets/components/jquery/dist/jquery.min.js +5 -0
- data/vendor/assets/components/jquery/dist/jquery.min.map +1 -0
- data/vendor/assets/components/jquery/src/ajax.js +786 -0
- data/vendor/assets/components/jquery/src/ajax/jsonp.js +89 -0
- data/vendor/assets/components/jquery/src/ajax/load.js +75 -0
- data/vendor/assets/components/jquery/src/ajax/parseJSON.js +13 -0
- data/vendor/assets/components/jquery/src/ajax/parseXML.js +28 -0
- data/vendor/assets/components/jquery/src/ajax/script.js +64 -0
- data/vendor/assets/components/jquery/src/ajax/var/nonce.js +5 -0
- data/vendor/assets/components/jquery/src/ajax/var/rquery.js +3 -0
- data/vendor/assets/components/jquery/src/ajax/xhr.js +136 -0
- data/vendor/assets/components/jquery/src/attributes.js +11 -0
- data/vendor/assets/components/jquery/src/attributes/attr.js +141 -0
- data/vendor/assets/components/jquery/src/attributes/classes.js +158 -0
- data/vendor/assets/components/jquery/src/attributes/prop.js +94 -0
- data/vendor/assets/components/jquery/src/attributes/support.js +35 -0
- data/vendor/assets/components/jquery/src/attributes/val.js +161 -0
- data/vendor/assets/components/jquery/src/callbacks.js +205 -0
- data/vendor/assets/components/jquery/src/core.js +502 -0
- data/vendor/assets/components/jquery/src/core/access.js +60 -0
- data/vendor/assets/components/jquery/src/core/init.js +123 -0
- data/vendor/assets/components/jquery/src/core/parseHTML.js +39 -0
- data/vendor/assets/components/jquery/src/core/ready.js +97 -0
- data/vendor/assets/components/jquery/src/core/var/rsingleTag.js +4 -0
- data/vendor/assets/components/jquery/src/css.js +450 -0
- data/vendor/assets/components/jquery/src/css/addGetHookIf.js +22 -0
- data/vendor/assets/components/jquery/src/css/curCSS.js +57 -0
- data/vendor/assets/components/jquery/src/css/defaultDisplay.js +70 -0
- data/vendor/assets/components/jquery/src/css/hiddenVisibleSelectors.js +15 -0
- data/vendor/assets/components/jquery/src/css/support.js +96 -0
- data/vendor/assets/components/jquery/src/css/swap.js +28 -0
- data/vendor/assets/components/jquery/src/css/var/cssExpand.js +3 -0
- data/vendor/assets/components/jquery/src/css/var/getStyles.js +12 -0
- data/vendor/assets/components/jquery/src/css/var/isHidden.js +13 -0
- data/vendor/assets/components/jquery/src/css/var/rmargin.js +3 -0
- data/vendor/assets/components/jquery/src/css/var/rnumnonpx.js +5 -0
- data/vendor/assets/components/jquery/src/data.js +178 -0
- data/vendor/assets/components/jquery/src/data/Data.js +181 -0
- data/vendor/assets/components/jquery/src/data/accepts.js +20 -0
- data/vendor/assets/components/jquery/src/data/var/data_priv.js +5 -0
- data/vendor/assets/components/jquery/src/data/var/data_user.js +5 -0
- data/vendor/assets/components/jquery/src/deferred.js +149 -0
- data/vendor/assets/components/jquery/src/deprecated.js +13 -0
- data/vendor/assets/components/jquery/src/dimensions.js +50 -0
- data/vendor/assets/components/jquery/src/effects.js +648 -0
- data/vendor/assets/components/jquery/src/effects/Tween.js +114 -0
- data/vendor/assets/components/jquery/src/effects/animatedSelector.js +13 -0
- data/vendor/assets/components/jquery/src/event.js +868 -0
- data/vendor/assets/components/jquery/src/event/ajax.js +13 -0
- data/vendor/assets/components/jquery/src/event/alias.js +39 -0
- data/vendor/assets/components/jquery/src/event/support.js +9 -0
- data/vendor/assets/components/jquery/src/exports/amd.js +24 -0
- data/vendor/assets/components/jquery/src/exports/global.js +32 -0
- data/vendor/assets/components/jquery/src/intro.js +44 -0
- data/vendor/assets/components/jquery/src/jquery.js +37 -0
- data/vendor/assets/components/jquery/src/manipulation.js +580 -0
- data/vendor/assets/components/jquery/src/manipulation/_evalUrl.js +18 -0
- data/vendor/assets/components/jquery/src/manipulation/support.js +32 -0
- data/vendor/assets/components/jquery/src/manipulation/var/rcheckableType.js +3 -0
- data/vendor/assets/components/jquery/src/offset.js +207 -0
- data/vendor/assets/components/jquery/src/outro.js +1 -0
- data/vendor/assets/components/jquery/src/queue.js +142 -0
- data/vendor/assets/components/jquery/src/queue/delay.js +22 -0
- data/vendor/assets/components/jquery/src/selector-native.js +172 -0
- data/vendor/assets/components/jquery/src/selector-sizzle.js +14 -0
- data/vendor/assets/components/jquery/src/selector.js +1 -0
- data/vendor/assets/components/jquery/src/serialize.js +111 -0
- data/vendor/assets/components/jquery/src/sizzle/dist/sizzle.js +2067 -0
- data/vendor/assets/components/jquery/src/sizzle/dist/sizzle.min.js +3 -0
- data/vendor/assets/components/jquery/src/sizzle/dist/sizzle.min.map +1 -0
- data/vendor/assets/components/jquery/src/traversing.js +199 -0
- data/vendor/assets/components/jquery/src/traversing/findFilter.js +100 -0
- data/vendor/assets/components/jquery/src/traversing/var/rneedsContext.js +6 -0
- data/vendor/assets/components/jquery/src/var/arr.js +3 -0
- data/vendor/assets/components/jquery/src/var/class2type.js +4 -0
- data/vendor/assets/components/jquery/src/var/concat.js +5 -0
- data/vendor/assets/components/jquery/src/var/hasOwn.js +5 -0
- data/vendor/assets/components/jquery/src/var/indexOf.js +5 -0
- data/vendor/assets/components/jquery/src/var/pnum.js +3 -0
- data/vendor/assets/components/jquery/src/var/push.js +5 -0
- data/vendor/assets/components/jquery/src/var/rnotwhite.js +3 -0
- data/vendor/assets/components/jquery/src/var/slice.js +5 -0
- data/vendor/assets/components/jquery/src/var/strundefined.js +3 -0
- data/vendor/assets/components/jquery/src/var/support.js +4 -0
- data/vendor/assets/components/jquery/src/var/toString.js +5 -0
- data/vendor/assets/components/jquery/src/wrap.js +79 -0
- data/vendor/assets/components/microplugin/.bower.json +38 -0
- data/vendor/assets/components/microplugin/.gitignore +2 -0
- data/vendor/assets/components/microplugin/.npmignore +3 -0
- data/vendor/assets/components/microplugin/README.md +115 -0
- data/vendor/assets/components/microplugin/bower.json +22 -0
- data/vendor/assets/components/microplugin/package.json +15 -0
- data/vendor/assets/components/microplugin/src/microplugin.js +135 -0
- data/vendor/assets/components/selectize/.bower.json +60 -0
- data/vendor/assets/components/selectize/.gitignore +5 -0
- data/vendor/assets/components/selectize/LICENSE +202 -0
- data/vendor/assets/components/selectize/README.md +105 -0
- data/vendor/assets/components/selectize/bower.json +40 -0
- data/vendor/assets/components/selectize/dist/css/selectize.bootstrap2.css +487 -0
- data/vendor/assets/components/selectize/dist/css/selectize.bootstrap3.css +401 -0
- data/vendor/assets/components/selectize/dist/css/selectize.css +317 -0
- data/vendor/assets/components/selectize/dist/css/selectize.default.css +387 -0
- data/vendor/assets/components/selectize/dist/css/selectize.legacy.css +364 -0
- data/vendor/assets/components/selectize/dist/js/selectize.js +3058 -0
- data/vendor/assets/components/selectize/dist/js/selectize.min.js +3 -0
- data/vendor/assets/components/selectize/dist/js/standalone/selectize.js +3667 -0
- data/vendor/assets/components/selectize/dist/js/standalone/selectize.min.js +3 -0
- data/vendor/assets/components/selectize/dist/less/plugins/drag_drop.less +16 -0
- data/vendor/assets/components/selectize/dist/less/plugins/dropdown_header.less +20 -0
- data/vendor/assets/components/selectize/dist/less/plugins/optgroup_columns.less +17 -0
- data/vendor/assets/components/selectize/dist/less/plugins/remove_button.less +37 -0
- data/vendor/assets/components/selectize/dist/less/selectize.bootstrap2.less +161 -0
- data/vendor/assets/components/selectize/dist/less/selectize.bootstrap3.less +150 -0
- data/vendor/assets/components/selectize/dist/less/selectize.default.less +84 -0
- data/vendor/assets/components/selectize/dist/less/selectize.legacy.less +75 -0
- data/vendor/assets/components/selectize/dist/less/selectize.less +295 -0
- data/vendor/assets/components/selectize/karma.conf.js +107 -0
- data/vendor/assets/components/sifter/.bower.json +44 -0
- data/vendor/assets/components/sifter/.gitignore +3 -0
- data/vendor/assets/components/sifter/.npmignore +4 -0
- data/vendor/assets/components/sifter/README.md +149 -0
- data/vendor/assets/components/sifter/bower.json +23 -0
- data/vendor/assets/components/sifter/package.json +48 -0
- data/vendor/assets/components/sifter/sifter.js +471 -0
- data/vendor/assets/components/sifter/sifter.min.js +2 -0
- data/vendor/assets/components/sweetalert/.bower.json +36 -0
- data/vendor/assets/components/sweetalert/.editorconfig +11 -0
- data/vendor/assets/components/sweetalert/.gitignore +5 -0
- data/vendor/assets/components/sweetalert/.jshintrc +11 -0
- data/vendor/assets/components/sweetalert/.travis.yml +6 -0
- data/vendor/assets/components/sweetalert/LICENSE +22 -0
- data/vendor/assets/components/sweetalert/README.md +157 -0
- data/vendor/assets/components/sweetalert/bower.json +25 -0
- data/vendor/assets/components/sweetalert/dev/gulpfile-wrap-template.js +18 -0
- data/vendor/assets/components/sweetalert/dev/ie9.css +23 -0
- data/vendor/assets/components/sweetalert/dev/loader-animation.css +209 -0
- data/vendor/assets/components/sweetalert/dev/modules/default-params.js +26 -0
- data/vendor/assets/components/sweetalert/dev/modules/handle-click.js +128 -0
- data/vendor/assets/components/sweetalert/dev/modules/handle-dom.js +161 -0
- data/vendor/assets/components/sweetalert/dev/modules/handle-key.js +73 -0
- data/vendor/assets/components/sweetalert/dev/modules/handle-swal-dom.js +148 -0
- data/vendor/assets/components/sweetalert/dev/modules/injected-html.js +69 -0
- data/vendor/assets/components/sweetalert/dev/modules/set-params.js +221 -0
- data/vendor/assets/components/sweetalert/dev/modules/utils.js +71 -0
- data/vendor/assets/components/sweetalert/dev/sweetalert.es6.js +311 -0
- data/vendor/assets/components/sweetalert/dev/sweetalert.scss +648 -0
- data/vendor/assets/components/sweetalert/dist/sweetalert-dev.js +1281 -0
- data/vendor/assets/components/sweetalert/dist/sweetalert.css +932 -0
- data/vendor/assets/components/sweetalert/dist/sweetalert.min.js +1 -0
- data/vendor/assets/components/sweetalert/example/example.css +442 -0
- data/vendor/assets/components/sweetalert/example/example.scss +580 -0
- data/vendor/assets/components/sweetalert/example/images/logo_big.png +0 -0
- data/vendor/assets/components/sweetalert/example/images/logo_big@2x.png +0 -0
- data/vendor/assets/components/sweetalert/example/images/logo_small.png +0 -0
- data/vendor/assets/components/sweetalert/example/images/logo_small@2x.png +0 -0
- data/vendor/assets/components/sweetalert/example/images/te-logo-small.svg +12 -0
- data/vendor/assets/components/sweetalert/example/images/thumbs-up.jpg +0 -0
- data/vendor/assets/components/sweetalert/example/images/vs_icon.png +0 -0
- data/vendor/assets/components/sweetalert/example/images/vs_icon@2x.png +0 -0
- data/vendor/assets/components/sweetalert/gulpfile.js +97 -0
- data/vendor/assets/components/sweetalert/index.html +584 -0
- data/vendor/assets/components/sweetalert/package.json +45 -0
- data/vendor/assets/components/sweetalert/sweetalert.gif +0 -0
- data/vendor/assets/components/sweetalert/test/index.html +20 -0
- data/vendor/assets/components/sweetalert/test/tests.js +143 -0
- data/vendor/assets/components/sweetalert/themes/facebook/facebook.css +111 -0
- data/vendor/assets/components/sweetalert/themes/facebook/facebook.scss +146 -0
- data/vendor/assets/components/sweetalert/themes/google/google.css +115 -0
- data/vendor/assets/components/sweetalert/themes/google/google.scss +148 -0
- data/vendor/assets/components/sweetalert/themes/twitter/twitter.css +140 -0
- data/vendor/assets/components/sweetalert/themes/twitter/twitter.scss +177 -0
- metadata +214 -5
- data/abstracted-0.0.4.gem +0 -0
- data/abstracted.gemspec +0 -51
- data/app/views/abstract_resources/index.html.erb +0 -1
@@ -0,0 +1,26 @@
|
|
1
|
+
var defaultParams = {
|
2
|
+
title: '',
|
3
|
+
text: '',
|
4
|
+
type: null,
|
5
|
+
allowOutsideClick: false,
|
6
|
+
showConfirmButton: true,
|
7
|
+
showCancelButton: false,
|
8
|
+
closeOnConfirm: true,
|
9
|
+
closeOnCancel: true,
|
10
|
+
confirmButtonText: 'OK',
|
11
|
+
confirmButtonColor: '#8CD4F5',
|
12
|
+
cancelButtonText: 'Cancel',
|
13
|
+
imageUrl: null,
|
14
|
+
imageSize: null,
|
15
|
+
timer: null,
|
16
|
+
customClass: '',
|
17
|
+
html: false,
|
18
|
+
animation: true,
|
19
|
+
allowEscapeKey: true,
|
20
|
+
inputType: 'text',
|
21
|
+
inputPlaceholder: '',
|
22
|
+
inputValue: '',
|
23
|
+
showLoaderOnConfirm: false
|
24
|
+
};
|
25
|
+
|
26
|
+
export default defaultParams;
|
@@ -0,0 +1,128 @@
|
|
1
|
+
import { colorLuminance } from './utils';
|
2
|
+
import { getModal } from './handle-swal-dom';
|
3
|
+
import { hasClass, isDescendant } from './handle-dom';
|
4
|
+
|
5
|
+
|
6
|
+
/*
|
7
|
+
* User clicked on "Confirm"/"OK" or "Cancel"
|
8
|
+
*/
|
9
|
+
var handleButton = function(event, params, modal) {
|
10
|
+
var e = event || window.event;
|
11
|
+
var target = e.target || e.srcElement;
|
12
|
+
|
13
|
+
var targetedConfirm = target.className.indexOf('confirm') !== -1;
|
14
|
+
var targetedOverlay = target.className.indexOf('sweet-overlay') !== -1;
|
15
|
+
var modalIsVisible = hasClass(modal, 'visible');
|
16
|
+
var doneFunctionExists = (params.doneFunction && modal.getAttribute('data-has-done-function') === 'true');
|
17
|
+
|
18
|
+
// Since the user can change the background-color of the confirm button programmatically,
|
19
|
+
// we must calculate what the color should be on hover/active
|
20
|
+
var normalColor, hoverColor, activeColor;
|
21
|
+
if (targetedConfirm && params.confirmButtonColor) {
|
22
|
+
normalColor = params.confirmButtonColor;
|
23
|
+
hoverColor = colorLuminance(normalColor, -0.04);
|
24
|
+
activeColor = colorLuminance(normalColor, -0.14);
|
25
|
+
}
|
26
|
+
|
27
|
+
function shouldSetConfirmButtonColor(color) {
|
28
|
+
if (targetedConfirm && params.confirmButtonColor) {
|
29
|
+
target.style.backgroundColor = color;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
switch (e.type) {
|
34
|
+
case 'mouseover':
|
35
|
+
shouldSetConfirmButtonColor(hoverColor);
|
36
|
+
break;
|
37
|
+
|
38
|
+
case 'mouseout':
|
39
|
+
shouldSetConfirmButtonColor(normalColor);
|
40
|
+
break;
|
41
|
+
|
42
|
+
case 'mousedown':
|
43
|
+
shouldSetConfirmButtonColor(activeColor);
|
44
|
+
break;
|
45
|
+
|
46
|
+
case 'mouseup':
|
47
|
+
shouldSetConfirmButtonColor(hoverColor);
|
48
|
+
break;
|
49
|
+
|
50
|
+
case 'focus':
|
51
|
+
let $confirmButton = modal.querySelector('button.confirm');
|
52
|
+
let $cancelButton = modal.querySelector('button.cancel');
|
53
|
+
|
54
|
+
if (targetedConfirm) {
|
55
|
+
$cancelButton.style.boxShadow = 'none';
|
56
|
+
} else {
|
57
|
+
$confirmButton.style.boxShadow = 'none';
|
58
|
+
}
|
59
|
+
break;
|
60
|
+
|
61
|
+
case 'click':
|
62
|
+
let clickedOnModal = (modal === target);
|
63
|
+
let clickedOnModalChild = isDescendant(modal, target);
|
64
|
+
|
65
|
+
// Ignore click outside if allowOutsideClick is false
|
66
|
+
if (!clickedOnModal && !clickedOnModalChild && modalIsVisible && !params.allowOutsideClick) {
|
67
|
+
break;
|
68
|
+
}
|
69
|
+
|
70
|
+
if (targetedConfirm && doneFunctionExists && modalIsVisible) {
|
71
|
+
handleConfirm(modal, params);
|
72
|
+
} else if (doneFunctionExists && modalIsVisible || targetedOverlay) {
|
73
|
+
handleCancel(modal, params);
|
74
|
+
} else if (isDescendant(modal, target) && target.tagName === 'BUTTON') {
|
75
|
+
sweetAlert.close();
|
76
|
+
}
|
77
|
+
break;
|
78
|
+
}
|
79
|
+
};
|
80
|
+
|
81
|
+
/*
|
82
|
+
* User clicked on "Confirm"/"OK"
|
83
|
+
*/
|
84
|
+
var handleConfirm = function(modal, params) {
|
85
|
+
var callbackValue = true;
|
86
|
+
|
87
|
+
if (hasClass(modal, 'show-input')) {
|
88
|
+
callbackValue = modal.querySelector('input').value;
|
89
|
+
|
90
|
+
if (!callbackValue) {
|
91
|
+
callbackValue = '';
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
params.doneFunction(callbackValue);
|
96
|
+
|
97
|
+
if (params.closeOnConfirm) {
|
98
|
+
sweetAlert.close();
|
99
|
+
}
|
100
|
+
// Disable cancel and confirm button if the parameter is true
|
101
|
+
if (params.showLoaderOnConfirm) {
|
102
|
+
sweetAlert.disableButtons();
|
103
|
+
}
|
104
|
+
};
|
105
|
+
|
106
|
+
/*
|
107
|
+
* User clicked on "Cancel"
|
108
|
+
*/
|
109
|
+
var handleCancel = function(modal, params) {
|
110
|
+
// Check if callback function expects a parameter (to track cancel actions)
|
111
|
+
var functionAsStr = String(params.doneFunction).replace(/\s/g, '');
|
112
|
+
var functionHandlesCancel = functionAsStr.substring(0, 9) === 'function(' && functionAsStr.substring(9, 10) !== ')';
|
113
|
+
|
114
|
+
if (functionHandlesCancel) {
|
115
|
+
params.doneFunction(false);
|
116
|
+
}
|
117
|
+
|
118
|
+
if (params.closeOnCancel) {
|
119
|
+
sweetAlert.close();
|
120
|
+
}
|
121
|
+
};
|
122
|
+
|
123
|
+
|
124
|
+
export default {
|
125
|
+
handleButton,
|
126
|
+
handleConfirm,
|
127
|
+
handleCancel
|
128
|
+
};
|
@@ -0,0 +1,161 @@
|
|
1
|
+
var hasClass = function(elem, className) {
|
2
|
+
return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
|
3
|
+
};
|
4
|
+
|
5
|
+
var addClass = function(elem, className) {
|
6
|
+
if (!hasClass(elem, className)) {
|
7
|
+
elem.className += ' ' + className;
|
8
|
+
}
|
9
|
+
};
|
10
|
+
|
11
|
+
var removeClass = function(elem, className) {
|
12
|
+
var newClass = ' ' + elem.className.replace(/[\t\r\n]/g, ' ') + ' ';
|
13
|
+
if (hasClass(elem, className)) {
|
14
|
+
while (newClass.indexOf(' ' + className + ' ') >= 0) {
|
15
|
+
newClass = newClass.replace(' ' + className + ' ', ' ');
|
16
|
+
}
|
17
|
+
elem.className = newClass.replace(/^\s+|\s+$/g, '');
|
18
|
+
}
|
19
|
+
};
|
20
|
+
|
21
|
+
var escapeHtml = function(str) {
|
22
|
+
var div = document.createElement('div');
|
23
|
+
div.appendChild(document.createTextNode(str));
|
24
|
+
return div.innerHTML;
|
25
|
+
};
|
26
|
+
|
27
|
+
var _show = function(elem) {
|
28
|
+
elem.style.opacity = '';
|
29
|
+
elem.style.display = 'block';
|
30
|
+
};
|
31
|
+
|
32
|
+
var show = function(elems) {
|
33
|
+
if (elems && !elems.length) {
|
34
|
+
return _show(elems);
|
35
|
+
}
|
36
|
+
for (var i = 0; i < elems.length; ++i) {
|
37
|
+
_show(elems[i]);
|
38
|
+
}
|
39
|
+
};
|
40
|
+
|
41
|
+
var _hide = function(elem) {
|
42
|
+
elem.style.opacity = '';
|
43
|
+
elem.style.display = 'none';
|
44
|
+
};
|
45
|
+
|
46
|
+
var hide = function(elems) {
|
47
|
+
if (elems && !elems.length) {
|
48
|
+
return _hide(elems);
|
49
|
+
}
|
50
|
+
for (var i = 0; i < elems.length; ++i) {
|
51
|
+
_hide(elems[i]);
|
52
|
+
}
|
53
|
+
};
|
54
|
+
|
55
|
+
var isDescendant = function(parent, child) {
|
56
|
+
var node = child.parentNode;
|
57
|
+
while (node !== null) {
|
58
|
+
if (node === parent) {
|
59
|
+
return true;
|
60
|
+
}
|
61
|
+
node = node.parentNode;
|
62
|
+
}
|
63
|
+
return false;
|
64
|
+
};
|
65
|
+
|
66
|
+
var getTopMargin = function(elem) {
|
67
|
+
elem.style.left = '-9999px';
|
68
|
+
elem.style.display = 'block';
|
69
|
+
|
70
|
+
var height = elem.clientHeight,
|
71
|
+
padding;
|
72
|
+
if (typeof getComputedStyle !== "undefined") { // IE 8
|
73
|
+
padding = parseInt(getComputedStyle(elem).getPropertyValue('padding-top'), 10);
|
74
|
+
} else {
|
75
|
+
padding = parseInt(elem.currentStyle.padding);
|
76
|
+
}
|
77
|
+
|
78
|
+
elem.style.left = '';
|
79
|
+
elem.style.display = 'none';
|
80
|
+
return ('-' + parseInt((height + padding) / 2) + 'px');
|
81
|
+
};
|
82
|
+
|
83
|
+
var fadeIn = function(elem, interval) {
|
84
|
+
if (+elem.style.opacity < 1) {
|
85
|
+
interval = interval || 16;
|
86
|
+
elem.style.opacity = 0;
|
87
|
+
elem.style.display = 'block';
|
88
|
+
var last = +new Date();
|
89
|
+
var tick = function() {
|
90
|
+
elem.style.opacity = +elem.style.opacity + (new Date() - last) / 100;
|
91
|
+
last = +new Date();
|
92
|
+
|
93
|
+
if (+elem.style.opacity < 1) {
|
94
|
+
setTimeout(tick, interval);
|
95
|
+
}
|
96
|
+
};
|
97
|
+
tick();
|
98
|
+
}
|
99
|
+
elem.style.display = 'block'; //fallback IE8
|
100
|
+
};
|
101
|
+
|
102
|
+
var fadeOut = function(elem, interval) {
|
103
|
+
interval = interval || 16;
|
104
|
+
elem.style.opacity = 1;
|
105
|
+
var last = +new Date();
|
106
|
+
var tick = function() {
|
107
|
+
elem.style.opacity = +elem.style.opacity - (new Date() - last) / 100;
|
108
|
+
last = +new Date();
|
109
|
+
|
110
|
+
if (+elem.style.opacity > 0) {
|
111
|
+
setTimeout(tick, interval);
|
112
|
+
} else {
|
113
|
+
elem.style.display = 'none';
|
114
|
+
}
|
115
|
+
};
|
116
|
+
tick();
|
117
|
+
};
|
118
|
+
|
119
|
+
var fireClick = function(node) {
|
120
|
+
// Taken from http://www.nonobtrusive.com/2011/11/29/programatically-fire-crossbrowser-click-event-with-javascript/
|
121
|
+
// Then fixed for today's Chrome browser.
|
122
|
+
if (typeof MouseEvent === 'function') {
|
123
|
+
// Up-to-date approach
|
124
|
+
var mevt = new MouseEvent('click', {
|
125
|
+
view: window,
|
126
|
+
bubbles: false,
|
127
|
+
cancelable: true
|
128
|
+
});
|
129
|
+
node.dispatchEvent(mevt);
|
130
|
+
} else if ( document.createEvent ) {
|
131
|
+
// Fallback
|
132
|
+
var evt = document.createEvent('MouseEvents');
|
133
|
+
evt.initEvent('click', false, false);
|
134
|
+
node.dispatchEvent(evt);
|
135
|
+
} else if (document.createEventObject) {
|
136
|
+
node.fireEvent('onclick') ;
|
137
|
+
} else if (typeof node.onclick === 'function' ) {
|
138
|
+
node.onclick();
|
139
|
+
}
|
140
|
+
};
|
141
|
+
|
142
|
+
var stopEventPropagation = function(e) {
|
143
|
+
// In particular, make sure the space bar doesn't scroll the main window.
|
144
|
+
if (typeof e.stopPropagation === 'function') {
|
145
|
+
e.stopPropagation();
|
146
|
+
e.preventDefault();
|
147
|
+
} else if (window.event && window.event.hasOwnProperty('cancelBubble')) {
|
148
|
+
window.event.cancelBubble = true;
|
149
|
+
}
|
150
|
+
};
|
151
|
+
|
152
|
+
export {
|
153
|
+
hasClass, addClass, removeClass,
|
154
|
+
escapeHtml,
|
155
|
+
_show, show, _hide, hide,
|
156
|
+
isDescendant,
|
157
|
+
getTopMargin,
|
158
|
+
fadeIn, fadeOut,
|
159
|
+
fireClick,
|
160
|
+
stopEventPropagation
|
161
|
+
};
|
@@ -0,0 +1,73 @@
|
|
1
|
+
import { stopEventPropagation, fireClick } from './handle-dom';
|
2
|
+
import { setFocusStyle } from './handle-swal-dom';
|
3
|
+
|
4
|
+
|
5
|
+
var handleKeyDown = function(event, params, modal) {
|
6
|
+
var e = event || window.event;
|
7
|
+
var keyCode = e.keyCode || e.which;
|
8
|
+
|
9
|
+
var $okButton = modal.querySelector('button.confirm');
|
10
|
+
var $cancelButton = modal.querySelector('button.cancel');
|
11
|
+
var $modalButtons = modal.querySelectorAll('button[tabindex]');
|
12
|
+
|
13
|
+
|
14
|
+
if ([9, 13, 32, 27].indexOf(keyCode) === -1) {
|
15
|
+
// Don't do work on keys we don't care about.
|
16
|
+
return;
|
17
|
+
}
|
18
|
+
|
19
|
+
var $targetElement = e.target || e.srcElement;
|
20
|
+
|
21
|
+
var btnIndex = -1; // Find the button - note, this is a nodelist, not an array.
|
22
|
+
for (var i = 0; i < $modalButtons.length; i++) {
|
23
|
+
if ($targetElement === $modalButtons[i]) {
|
24
|
+
btnIndex = i;
|
25
|
+
break;
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
if (keyCode === 9) {
|
30
|
+
// TAB
|
31
|
+
if (btnIndex === -1) {
|
32
|
+
// No button focused. Jump to the confirm button.
|
33
|
+
$targetElement = $okButton;
|
34
|
+
} else {
|
35
|
+
// Cycle to the next button
|
36
|
+
if (btnIndex === $modalButtons.length - 1) {
|
37
|
+
$targetElement = $modalButtons[0];
|
38
|
+
} else {
|
39
|
+
$targetElement = $modalButtons[btnIndex + 1];
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
stopEventPropagation(e);
|
44
|
+
$targetElement.focus();
|
45
|
+
|
46
|
+
if (params.confirmButtonColor) {
|
47
|
+
setFocusStyle($targetElement, params.confirmButtonColor);
|
48
|
+
}
|
49
|
+
} else {
|
50
|
+
if (keyCode === 13) {
|
51
|
+
if ($targetElement.tagName === 'INPUT') {
|
52
|
+
$targetElement = $okButton;
|
53
|
+
$okButton.focus();
|
54
|
+
}
|
55
|
+
|
56
|
+
if (btnIndex === -1) {
|
57
|
+
// ENTER/SPACE clicked outside of a button.
|
58
|
+
$targetElement = $okButton;
|
59
|
+
} else {
|
60
|
+
// Do nothing - let the browser handle it.
|
61
|
+
$targetElement = undefined;
|
62
|
+
}
|
63
|
+
} else if (keyCode === 27 && params.allowEscapeKey === true) {
|
64
|
+
$targetElement = $cancelButton;
|
65
|
+
fireClick($targetElement, e);
|
66
|
+
} else {
|
67
|
+
// Fallback - let the browser handle it.
|
68
|
+
$targetElement = undefined;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
};
|
72
|
+
|
73
|
+
export default handleKeyDown;
|
@@ -0,0 +1,148 @@
|
|
1
|
+
import { hexToRgb } from './utils';
|
2
|
+
import { removeClass, getTopMargin, fadeIn, show, addClass } from './handle-dom';
|
3
|
+
import defaultParams from './default-params';
|
4
|
+
|
5
|
+
var modalClass = '.sweet-alert';
|
6
|
+
var overlayClass = '.sweet-overlay';
|
7
|
+
|
8
|
+
/*
|
9
|
+
* Add modal + overlay to DOM
|
10
|
+
*/
|
11
|
+
import injectedHTML from './injected-html';
|
12
|
+
|
13
|
+
var sweetAlertInitialize = function() {
|
14
|
+
var sweetWrap = document.createElement('div');
|
15
|
+
sweetWrap.innerHTML = injectedHTML;
|
16
|
+
|
17
|
+
// Append elements to body
|
18
|
+
while (sweetWrap.firstChild) {
|
19
|
+
document.body.appendChild(sweetWrap.firstChild);
|
20
|
+
}
|
21
|
+
};
|
22
|
+
|
23
|
+
/*
|
24
|
+
* Get DOM element of modal
|
25
|
+
*/
|
26
|
+
var getModal = function() {
|
27
|
+
var $modal = document.querySelector(modalClass);
|
28
|
+
|
29
|
+
if (!$modal) {
|
30
|
+
sweetAlertInitialize();
|
31
|
+
$modal = getModal();
|
32
|
+
}
|
33
|
+
|
34
|
+
return $modal;
|
35
|
+
};
|
36
|
+
|
37
|
+
/*
|
38
|
+
* Get DOM element of input (in modal)
|
39
|
+
*/
|
40
|
+
var getInput = function() {
|
41
|
+
var $modal = getModal();
|
42
|
+
if ($modal) {
|
43
|
+
return $modal.querySelector('input');
|
44
|
+
}
|
45
|
+
};
|
46
|
+
|
47
|
+
/*
|
48
|
+
* Get DOM element of overlay
|
49
|
+
*/
|
50
|
+
var getOverlay = function() {
|
51
|
+
return document.querySelector(overlayClass);
|
52
|
+
};
|
53
|
+
|
54
|
+
/*
|
55
|
+
* Add box-shadow style to button (depending on its chosen bg-color)
|
56
|
+
*/
|
57
|
+
var setFocusStyle = function($button, bgColor) {
|
58
|
+
var rgbColor = hexToRgb(bgColor);
|
59
|
+
$button.style.boxShadow = '0 0 2px rgba(' + rgbColor + ', 0.8), inset 0 0 0 1px rgba(0, 0, 0, 0.05)';
|
60
|
+
};
|
61
|
+
|
62
|
+
/*
|
63
|
+
* Animation when opening modal
|
64
|
+
*/
|
65
|
+
var openModal = function(callback) {
|
66
|
+
var $modal = getModal();
|
67
|
+
fadeIn(getOverlay(), 10);
|
68
|
+
show($modal);
|
69
|
+
addClass($modal, 'showSweetAlert');
|
70
|
+
removeClass($modal, 'hideSweetAlert');
|
71
|
+
|
72
|
+
window.previousActiveElement = document.activeElement;
|
73
|
+
var $okButton = $modal.querySelector('button.confirm');
|
74
|
+
$okButton.focus();
|
75
|
+
|
76
|
+
setTimeout(function () {
|
77
|
+
addClass($modal, 'visible');
|
78
|
+
}, 500);
|
79
|
+
|
80
|
+
var timer = $modal.getAttribute('data-timer');
|
81
|
+
|
82
|
+
if (timer !== 'null' && timer !== '') {
|
83
|
+
var timerCallback = callback;
|
84
|
+
$modal.timeout = setTimeout(function() {
|
85
|
+
var doneFunctionExists = ((timerCallback || null) && $modal.getAttribute('data-has-done-function') === 'true');
|
86
|
+
if (doneFunctionExists) {
|
87
|
+
timerCallback(null);
|
88
|
+
}
|
89
|
+
else {
|
90
|
+
sweetAlert.close();
|
91
|
+
}
|
92
|
+
}, timer);
|
93
|
+
}
|
94
|
+
};
|
95
|
+
|
96
|
+
/*
|
97
|
+
* Reset the styling of the input
|
98
|
+
* (for example if errors have been shown)
|
99
|
+
*/
|
100
|
+
var resetInput = function() {
|
101
|
+
var $modal = getModal();
|
102
|
+
var $input = getInput();
|
103
|
+
|
104
|
+
removeClass($modal, 'show-input');
|
105
|
+
$input.value = defaultParams.inputValue;
|
106
|
+
$input.setAttribute('type', defaultParams.inputType);
|
107
|
+
$input.setAttribute('placeholder', defaultParams.inputPlaceholder);
|
108
|
+
|
109
|
+
resetInputError();
|
110
|
+
};
|
111
|
+
|
112
|
+
|
113
|
+
var resetInputError = function(event) {
|
114
|
+
// If press enter => ignore
|
115
|
+
if (event && event.keyCode === 13) {
|
116
|
+
return false;
|
117
|
+
}
|
118
|
+
|
119
|
+
var $modal = getModal();
|
120
|
+
|
121
|
+
var $errorIcon = $modal.querySelector('.sa-input-error');
|
122
|
+
removeClass($errorIcon, 'show');
|
123
|
+
|
124
|
+
var $errorContainer = $modal.querySelector('.sa-error-container');
|
125
|
+
removeClass($errorContainer, 'show');
|
126
|
+
};
|
127
|
+
|
128
|
+
|
129
|
+
/*
|
130
|
+
* Set "margin-top"-property on modal based on its computed height
|
131
|
+
*/
|
132
|
+
var fixVerticalPosition = function() {
|
133
|
+
var $modal = getModal();
|
134
|
+
$modal.style.marginTop = getTopMargin(getModal());
|
135
|
+
};
|
136
|
+
|
137
|
+
|
138
|
+
export {
|
139
|
+
sweetAlertInitialize,
|
140
|
+
getModal,
|
141
|
+
getOverlay,
|
142
|
+
getInput,
|
143
|
+
setFocusStyle,
|
144
|
+
openModal,
|
145
|
+
resetInput,
|
146
|
+
resetInputError,
|
147
|
+
fixVerticalPosition
|
148
|
+
};
|