@@ 2618-3003 (lines=386) @@ | ||
2615 | ||
2616 | })(UIkit2); |
|
2617 | ||
2618 | (function(UI) { |
|
2619 | ||
2620 | "use strict"; |
|
2621 | ||
2622 | var active = false, activeCount = 0, $html = UI.$html, body; |
|
2623 | ||
2624 | UI.$win.on('resize orientationchange', UI.Utils.debounce(function(){ |
|
2625 | UI.$('.uk-modal.uk-open').each(function(){ |
|
2626 | return UI.$(this).data('modal') && UI.$(this).data('modal').resize(); |
|
2627 | }); |
|
2628 | }, 150)); |
|
2629 | ||
2630 | UI.component('modal', { |
|
2631 | ||
2632 | defaults: { |
|
2633 | keyboard: true, |
|
2634 | bgclose: true, |
|
2635 | minScrollHeight: 150, |
|
2636 | center: false, |
|
2637 | modal: true |
|
2638 | }, |
|
2639 | ||
2640 | scrollable: false, |
|
2641 | transition: false, |
|
2642 | hasTransitioned: true, |
|
2643 | ||
2644 | init: function() { |
|
2645 | ||
2646 | if (!body) body = UI.$('body'); |
|
2647 | ||
2648 | if (!this.element.length) return; |
|
2649 | ||
2650 | var $this = this; |
|
2651 | ||
2652 | this.paddingdir = 'padding-' + (UI.langdirection == 'left' ? 'right':'left'); |
|
2653 | this.dialog = this.find('.uk-modal-dialog'); |
|
2654 | ||
2655 | this.active = false; |
|
2656 | ||
2657 | // Update ARIA |
|
2658 | this.element.attr('aria-hidden', this.element.hasClass('uk-open')); |
|
2659 | ||
2660 | this.on('click', '.uk-modal-close', function(e) { |
|
2661 | e.preventDefault(); |
|
2662 | $this.hide(); |
|
2663 | }).on('click', function(e) { |
|
2664 | ||
2665 | var target = UI.$(e.target); |
|
2666 | ||
2667 | if (target[0] == $this.element[0] && $this.options.bgclose) { |
|
2668 | $this.hide(); |
|
2669 | } |
|
2670 | }); |
|
2671 | ||
2672 | UI.domObserve(this.element, function(e) { $this.resize(); }); |
|
2673 | }, |
|
2674 | ||
2675 | toggle: function() { |
|
2676 | return this[this.isActive() ? 'hide' : 'show'](); |
|
2677 | }, |
|
2678 | ||
2679 | show: function() { |
|
2680 | ||
2681 | if (!this.element.length) return; |
|
2682 | ||
2683 | var $this = this; |
|
2684 | ||
2685 | if (this.isActive()) return; |
|
2686 | ||
2687 | if (this.options.modal && active) { |
|
2688 | active.hide(true); |
|
2689 | } |
|
2690 | ||
2691 | this.element.removeClass('uk-open').show(); |
|
2692 | this.resize(true); |
|
2693 | ||
2694 | if (this.options.modal) { |
|
2695 | active = this; |
|
2696 | } |
|
2697 | ||
2698 | this.active = true; |
|
2699 | ||
2700 | activeCount++; |
|
2701 | ||
2702 | if (UI.support.transition) { |
|
2703 | this.hasTransitioned = false; |
|
2704 | this.element.one(UI.support.transition.end, function(){ |
|
2705 | $this.hasTransitioned = true; |
|
2706 | UI.Utils.focus($this.dialog, 'a[href]'); |
|
2707 | }).addClass('uk-open'); |
|
2708 | } else { |
|
2709 | this.element.addClass('uk-open'); |
|
2710 | UI.Utils.focus(this.dialog, 'a[href]'); |
|
2711 | } |
|
2712 | ||
2713 | $html.addClass('uk-modal-page').height(); // force browser engine redraw |
|
2714 | ||
2715 | // Update ARIA |
|
2716 | this.element.attr('aria-hidden', 'false'); |
|
2717 | ||
2718 | this.element.trigger('show.uk.modal'); |
|
2719 | ||
2720 | UI.Utils.checkDisplay(this.dialog, true); |
|
2721 | ||
2722 | return this; |
|
2723 | }, |
|
2724 | ||
2725 | hide: function(force) { |
|
2726 | ||
2727 | if (!force && UI.support.transition && this.hasTransitioned) { |
|
2728 | ||
2729 | var $this = this; |
|
2730 | ||
2731 | this.one(UI.support.transition.end, function() { |
|
2732 | $this._hide(); |
|
2733 | }).removeClass('uk-open'); |
|
2734 | ||
2735 | } else { |
|
2736 | ||
2737 | this._hide(); |
|
2738 | } |
|
2739 | ||
2740 | return this; |
|
2741 | }, |
|
2742 | ||
2743 | resize: function(force) { |
|
2744 | ||
2745 | if (!this.isActive() && !force) return; |
|
2746 | ||
2747 | var bodywidth = body.width(); |
|
2748 | ||
2749 | this.scrollbarwidth = window.innerWidth - bodywidth; |
|
2750 | ||
2751 | body.css(this.paddingdir, this.scrollbarwidth); |
|
2752 | ||
2753 | this.element.css('overflow-y', this.scrollbarwidth ? 'scroll' : 'auto'); |
|
2754 | ||
2755 | if (!this.updateScrollable() && this.options.center) { |
|
2756 | ||
2757 | var dh = this.dialog.outerHeight(), |
|
2758 | pad = parseInt(this.dialog.css('margin-top'), 10) + parseInt(this.dialog.css('margin-bottom'), 10); |
|
2759 | ||
2760 | if ((dh + pad) < window.innerHeight) { |
|
2761 | this.dialog.css({top: (window.innerHeight/2 - dh/2) - pad }); |
|
2762 | } else { |
|
2763 | this.dialog.css({top: ''}); |
|
2764 | } |
|
2765 | } |
|
2766 | }, |
|
2767 | ||
2768 | updateScrollable: function() { |
|
2769 | ||
2770 | // has scrollable? |
|
2771 | var scrollable = this.dialog.find('.uk-overflow-container:visible:first'); |
|
2772 | ||
2773 | if (scrollable.length) { |
|
2774 | ||
2775 | scrollable.css('height', 0); |
|
2776 | ||
2777 | var offset = Math.abs(parseInt(this.dialog.css('margin-top'), 10)), |
|
2778 | dh = this.dialog.outerHeight(), |
|
2779 | wh = window.innerHeight, |
|
2780 | h = wh - 2*(offset < 20 ? 20:offset) - dh; |
|
2781 | ||
2782 | scrollable.css({ |
|
2783 | maxHeight: (h < this.options.minScrollHeight ? '':h), |
|
2784 | height:'' |
|
2785 | }); |
|
2786 | ||
2787 | return true; |
|
2788 | } |
|
2789 | ||
2790 | return false; |
|
2791 | }, |
|
2792 | ||
2793 | _hide: function() { |
|
2794 | ||
2795 | this.active = false; |
|
2796 | if (activeCount > 0) activeCount--; |
|
2797 | else activeCount = 0; |
|
2798 | ||
2799 | this.element.hide().removeClass('uk-open'); |
|
2800 | ||
2801 | // Update ARIA |
|
2802 | this.element.attr('aria-hidden', 'true'); |
|
2803 | ||
2804 | if (!activeCount) { |
|
2805 | $html.removeClass('uk-modal-page'); |
|
2806 | body.css(this.paddingdir, ""); |
|
2807 | } |
|
2808 | ||
2809 | if (active===this) active = false; |
|
2810 | ||
2811 | this.trigger('hide.uk.modal'); |
|
2812 | }, |
|
2813 | ||
2814 | isActive: function() { |
|
2815 | return this.element.hasClass('uk-open'); |
|
2816 | } |
|
2817 | ||
2818 | }); |
|
2819 | ||
2820 | UI.component('modalTrigger', { |
|
2821 | ||
2822 | boot: function() { |
|
2823 | ||
2824 | // init code |
|
2825 | UI.$html.on('click.modal.uikit', '[data-uk-modal]', function(e) { |
|
2826 | ||
2827 | var ele = UI.$(this); |
|
2828 | ||
2829 | if (ele.is('a')) { |
|
2830 | e.preventDefault(); |
|
2831 | } |
|
2832 | ||
2833 | if (!ele.data('modalTrigger')) { |
|
2834 | var modal = UI.modalTrigger(ele, UI.Utils.options(ele.attr('data-uk-modal'))); |
|
2835 | modal.show(); |
|
2836 | } |
|
2837 | ||
2838 | }); |
|
2839 | ||
2840 | // close modal on esc button |
|
2841 | UI.$html.on('keydown.modal.uikit', function (e) { |
|
2842 | ||
2843 | if (active && e.keyCode === 27 && active.options.keyboard) { // ESC |
|
2844 | e.preventDefault(); |
|
2845 | active.hide(); |
|
2846 | } |
|
2847 | }); |
|
2848 | }, |
|
2849 | ||
2850 | init: function() { |
|
2851 | ||
2852 | var $this = this; |
|
2853 | ||
2854 | this.options = UI.$.extend({ |
|
2855 | target: $this.element.is('a') ? $this.element.attr('href') : false |
|
2856 | }, this.options); |
|
2857 | ||
2858 | this.modal = UI.modal(this.options.target, this.options); |
|
2859 | ||
2860 | this.on("click", function(e) { |
|
2861 | e.preventDefault(); |
|
2862 | $this.show(); |
|
2863 | }); |
|
2864 | ||
2865 | //methods |
|
2866 | this.proxy(this.modal, 'show hide isActive'); |
|
2867 | } |
|
2868 | }); |
|
2869 | ||
2870 | UI.modal.dialog = function(content, options) { |
|
2871 | ||
2872 | var modal = UI.modal(UI.$(UI.modal.dialog.template).appendTo('body'), options); |
|
2873 | ||
2874 | modal.on('hide.uk.modal', function(){ |
|
2875 | if (modal.persist) { |
|
2876 | modal.persist.appendTo(modal.persist.data('modalPersistParent')); |
|
2877 | modal.persist = false; |
|
2878 | } |
|
2879 | modal.element.remove(); |
|
2880 | }); |
|
2881 | ||
2882 | setContent(content, modal); |
|
2883 | ||
2884 | return modal; |
|
2885 | }; |
|
2886 | ||
2887 | UI.modal.dialog.template = '<div class="uk-modal"><div class="uk-modal-dialog" style="min-height:0;"></div></div>'; |
|
2888 | ||
2889 | UI.modal.alert = function(content, options) { |
|
2890 | ||
2891 | options = UI.$.extend(true, {bgclose:false, keyboard:false, modal:false, labels:UI.modal.labels}, options); |
|
2892 | ||
2893 | var modal = UI.modal.dialog(([ |
|
2894 | '<div class="uk-margin uk-modal-content">'+String(content)+'</div>', |
|
2895 | '<div class="uk-modal-footer uk-text-right"><button class="uk-button uk-button-primary uk-modal-close">'+options.labels.Ok+'</button></div>' |
|
2896 | ]).join(""), options); |
|
2897 | ||
2898 | modal.on('show.uk.modal', function(){ |
|
2899 | setTimeout(function(){ |
|
2900 | modal.element.find('button:first').focus(); |
|
2901 | }, 50); |
|
2902 | }); |
|
2903 | ||
2904 | return modal.show(); |
|
2905 | }; |
|
2906 | ||
2907 | UI.modal.confirm = function(content, onconfirm, oncancel) { |
|
2908 | ||
2909 | var options = arguments.length > 1 && arguments[arguments.length-1] ? arguments[arguments.length-1] : {}; |
|
2910 | ||
2911 | onconfirm = UI.$.isFunction(onconfirm) ? onconfirm : function(){}; |
|
2912 | oncancel = UI.$.isFunction(oncancel) ? oncancel : function(){}; |
|
2913 | options = UI.$.extend(true, {bgclose:false, keyboard:false, modal:false, labels:UI.modal.labels}, UI.$.isFunction(options) ? {}:options); |
|
2914 | ||
2915 | var modal = UI.modal.dialog(([ |
|
2916 | '<div class="uk-margin uk-modal-content">'+String(content)+'</div>', |
|
2917 | '<div class="uk-modal-footer uk-text-right"><button class="uk-button js-modal-confirm-cancel">'+options.labels.Cancel+'</button> <button class="uk-button uk-button-primary js-modal-confirm">'+options.labels.Ok+'</button></div>' |
|
2918 | ]).join(""), options); |
|
2919 | ||
2920 | modal.element.find(".js-modal-confirm, .js-modal-confirm-cancel").on("click", function(){ |
|
2921 | UI.$(this).is('.js-modal-confirm') ? onconfirm() : oncancel(); |
|
2922 | modal.hide(); |
|
2923 | }); |
|
2924 | ||
2925 | modal.on('show.uk.modal', function(){ |
|
2926 | setTimeout(function(){ |
|
2927 | modal.element.find('.js-modal-confirm').focus(); |
|
2928 | }, 50); |
|
2929 | }); |
|
2930 | ||
2931 | return modal.show(); |
|
2932 | }; |
|
2933 | ||
2934 | UI.modal.prompt = function(text, value, onsubmit, options) { |
|
2935 | ||
2936 | onsubmit = UI.$.isFunction(onsubmit) ? onsubmit : function(value){}; |
|
2937 | options = UI.$.extend(true, {bgclose:false, keyboard:false, modal:false, labels:UI.modal.labels}, options); |
|
2938 | ||
2939 | var modal = UI.modal.dialog(([ |
|
2940 | text ? '<div class="uk-modal-content uk-form">'+String(text)+'</div>':'', |
|
2941 | '<div class="uk-margin-small-top uk-modal-content uk-form"><p><input type="text" class="uk-width-1-1"></p></div>', |
|
2942 | '<div class="uk-modal-footer uk-text-right"><button class="uk-button uk-modal-close">'+options.labels.Cancel+'</button> <button class="uk-button uk-button-primary js-modal-ok">'+options.labels.Ok+'</button></div>' |
|
2943 | ]).join(""), options), |
|
2944 | ||
2945 | input = modal.element.find("input[type='text']").val(value || '').on('keyup', function(e){ |
|
2946 | if (e.keyCode == 13) { |
|
2947 | modal.element.find('.js-modal-ok').trigger('click'); |
|
2948 | } |
|
2949 | }); |
|
2950 | ||
2951 | modal.element.find('.js-modal-ok').on('click', function(){ |
|
2952 | if (onsubmit(input.val())!==false){ |
|
2953 | modal.hide(); |
|
2954 | } |
|
2955 | }); |
|
2956 | ||
2957 | return modal.show(); |
|
2958 | }; |
|
2959 | ||
2960 | UI.modal.blockUI = function(content, options) { |
|
2961 | ||
2962 | var modal = UI.modal.dialog(([ |
|
2963 | '<div class="uk-margin uk-modal-content">'+String(content || '<div class="uk-text-center">...</div>')+'</div>' |
|
2964 | ]).join(""), UI.$.extend({bgclose:false, keyboard:false, modal:false}, options)); |
|
2965 | ||
2966 | modal.content = modal.element.find('.uk-modal-content:first'); |
|
2967 | ||
2968 | return modal.show(); |
|
2969 | }; |
|
2970 | ||
2971 | UI.modal.labels = { |
|
2972 | Ok: 'Ok', |
|
2973 | Cancel: 'Cancel' |
|
2974 | }; |
|
2975 | ||
2976 | // helper functions |
|
2977 | function setContent(content, modal){ |
|
2978 | ||
2979 | if(!modal) return; |
|
2980 | ||
2981 | if (typeof content === 'object') { |
|
2982 | ||
2983 | // convert DOM object to a jQuery object |
|
2984 | content = content instanceof jQuery ? content : UI.$(content); |
|
2985 | ||
2986 | if(content.parent().length) { |
|
2987 | modal.persist = content; |
|
2988 | modal.persist.data('modalPersistParent', content.parent()); |
|
2989 | } |
|
2990 | }else if (typeof content === 'string' || typeof content === 'number') { |
|
2991 | // just insert the data as innerHTML |
|
2992 | content = UI.$('<div></div>').html(content); |
|
2993 | }else { |
|
2994 | // unsupported data type! |
|
2995 | content = UI.$('<div></div>').html('UIkit2.modal Error: Unsupported data type: ' + typeof content); |
|
2996 | } |
|
2997 | ||
2998 | content.appendTo(modal.element.find('.uk-modal-dialog')); |
|
2999 | ||
3000 | return modal; |
|
3001 | } |
|
3002 | ||
3003 | })(UIkit2); |
|
3004 | ||
3005 | (function(UI) { |
|
3006 |
@@ 2-387 (lines=386) @@ | ||
1 | /*! UIkit 2.27.4 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */ |
|
2 | (function(UI) { |
|
3 | ||
4 | "use strict"; |
|
5 | ||
6 | var active = false, activeCount = 0, $html = UI.$html, body; |
|
7 | ||
8 | UI.$win.on('resize orientationchange', UI.Utils.debounce(function(){ |
|
9 | UI.$('.uk-modal.uk-open').each(function(){ |
|
10 | return UI.$(this).data('modal') && UI.$(this).data('modal').resize(); |
|
11 | }); |
|
12 | }, 150)); |
|
13 | ||
14 | UI.component('modal', { |
|
15 | ||
16 | defaults: { |
|
17 | keyboard: true, |
|
18 | bgclose: true, |
|
19 | minScrollHeight: 150, |
|
20 | center: false, |
|
21 | modal: true |
|
22 | }, |
|
23 | ||
24 | scrollable: false, |
|
25 | transition: false, |
|
26 | hasTransitioned: true, |
|
27 | ||
28 | init: function() { |
|
29 | ||
30 | if (!body) body = UI.$('body'); |
|
31 | ||
32 | if (!this.element.length) return; |
|
33 | ||
34 | var $this = this; |
|
35 | ||
36 | this.paddingdir = 'padding-' + (UI.langdirection == 'left' ? 'right':'left'); |
|
37 | this.dialog = this.find('.uk-modal-dialog'); |
|
38 | ||
39 | this.active = false; |
|
40 | ||
41 | // Update ARIA |
|
42 | this.element.attr('aria-hidden', this.element.hasClass('uk-open')); |
|
43 | ||
44 | this.on('click', '.uk-modal-close', function(e) { |
|
45 | e.preventDefault(); |
|
46 | $this.hide(); |
|
47 | }).on('click', function(e) { |
|
48 | ||
49 | var target = UI.$(e.target); |
|
50 | ||
51 | if (target[0] == $this.element[0] && $this.options.bgclose) { |
|
52 | $this.hide(); |
|
53 | } |
|
54 | }); |
|
55 | ||
56 | UI.domObserve(this.element, function(e) { $this.resize(); }); |
|
57 | }, |
|
58 | ||
59 | toggle: function() { |
|
60 | return this[this.isActive() ? 'hide' : 'show'](); |
|
61 | }, |
|
62 | ||
63 | show: function() { |
|
64 | ||
65 | if (!this.element.length) return; |
|
66 | ||
67 | var $this = this; |
|
68 | ||
69 | if (this.isActive()) return; |
|
70 | ||
71 | if (this.options.modal && active) { |
|
72 | active.hide(true); |
|
73 | } |
|
74 | ||
75 | this.element.removeClass('uk-open').show(); |
|
76 | this.resize(true); |
|
77 | ||
78 | if (this.options.modal) { |
|
79 | active = this; |
|
80 | } |
|
81 | ||
82 | this.active = true; |
|
83 | ||
84 | activeCount++; |
|
85 | ||
86 | if (UI.support.transition) { |
|
87 | this.hasTransitioned = false; |
|
88 | this.element.one(UI.support.transition.end, function(){ |
|
89 | $this.hasTransitioned = true; |
|
90 | UI.Utils.focus($this.dialog, 'a[href]'); |
|
91 | }).addClass('uk-open'); |
|
92 | } else { |
|
93 | this.element.addClass('uk-open'); |
|
94 | UI.Utils.focus(this.dialog, 'a[href]'); |
|
95 | } |
|
96 | ||
97 | $html.addClass('uk-modal-page').height(); // force browser engine redraw |
|
98 | ||
99 | // Update ARIA |
|
100 | this.element.attr('aria-hidden', 'false'); |
|
101 | ||
102 | this.element.trigger('show.uk.modal'); |
|
103 | ||
104 | UI.Utils.checkDisplay(this.dialog, true); |
|
105 | ||
106 | return this; |
|
107 | }, |
|
108 | ||
109 | hide: function(force) { |
|
110 | ||
111 | if (!force && UI.support.transition && this.hasTransitioned) { |
|
112 | ||
113 | var $this = this; |
|
114 | ||
115 | this.one(UI.support.transition.end, function() { |
|
116 | $this._hide(); |
|
117 | }).removeClass('uk-open'); |
|
118 | ||
119 | } else { |
|
120 | ||
121 | this._hide(); |
|
122 | } |
|
123 | ||
124 | return this; |
|
125 | }, |
|
126 | ||
127 | resize: function(force) { |
|
128 | ||
129 | if (!this.isActive() && !force) return; |
|
130 | ||
131 | var bodywidth = body.width(); |
|
132 | ||
133 | this.scrollbarwidth = window.innerWidth - bodywidth; |
|
134 | ||
135 | body.css(this.paddingdir, this.scrollbarwidth); |
|
136 | ||
137 | this.element.css('overflow-y', this.scrollbarwidth ? 'scroll' : 'auto'); |
|
138 | ||
139 | if (!this.updateScrollable() && this.options.center) { |
|
140 | ||
141 | var dh = this.dialog.outerHeight(), |
|
142 | pad = parseInt(this.dialog.css('margin-top'), 10) + parseInt(this.dialog.css('margin-bottom'), 10); |
|
143 | ||
144 | if ((dh + pad) < window.innerHeight) { |
|
145 | this.dialog.css({top: (window.innerHeight/2 - dh/2) - pad }); |
|
146 | } else { |
|
147 | this.dialog.css({top: ''}); |
|
148 | } |
|
149 | } |
|
150 | }, |
|
151 | ||
152 | updateScrollable: function() { |
|
153 | ||
154 | // has scrollable? |
|
155 | var scrollable = this.dialog.find('.uk-overflow-container:visible:first'); |
|
156 | ||
157 | if (scrollable.length) { |
|
158 | ||
159 | scrollable.css('height', 0); |
|
160 | ||
161 | var offset = Math.abs(parseInt(this.dialog.css('margin-top'), 10)), |
|
162 | dh = this.dialog.outerHeight(), |
|
163 | wh = window.innerHeight, |
|
164 | h = wh - 2*(offset < 20 ? 20:offset) - dh; |
|
165 | ||
166 | scrollable.css({ |
|
167 | maxHeight: (h < this.options.minScrollHeight ? '':h), |
|
168 | height:'' |
|
169 | }); |
|
170 | ||
171 | return true; |
|
172 | } |
|
173 | ||
174 | return false; |
|
175 | }, |
|
176 | ||
177 | _hide: function() { |
|
178 | ||
179 | this.active = false; |
|
180 | if (activeCount > 0) activeCount--; |
|
181 | else activeCount = 0; |
|
182 | ||
183 | this.element.hide().removeClass('uk-open'); |
|
184 | ||
185 | // Update ARIA |
|
186 | this.element.attr('aria-hidden', 'true'); |
|
187 | ||
188 | if (!activeCount) { |
|
189 | $html.removeClass('uk-modal-page'); |
|
190 | body.css(this.paddingdir, ""); |
|
191 | } |
|
192 | ||
193 | if (active===this) active = false; |
|
194 | ||
195 | this.trigger('hide.uk.modal'); |
|
196 | }, |
|
197 | ||
198 | isActive: function() { |
|
199 | return this.element.hasClass('uk-open'); |
|
200 | } |
|
201 | ||
202 | }); |
|
203 | ||
204 | UI.component('modalTrigger', { |
|
205 | ||
206 | boot: function() { |
|
207 | ||
208 | // init code |
|
209 | UI.$html.on('click.modal.uikit', '[data-uk-modal]', function(e) { |
|
210 | ||
211 | var ele = UI.$(this); |
|
212 | ||
213 | if (ele.is('a')) { |
|
214 | e.preventDefault(); |
|
215 | } |
|
216 | ||
217 | if (!ele.data('modalTrigger')) { |
|
218 | var modal = UI.modalTrigger(ele, UI.Utils.options(ele.attr('data-uk-modal'))); |
|
219 | modal.show(); |
|
220 | } |
|
221 | ||
222 | }); |
|
223 | ||
224 | // close modal on esc button |
|
225 | UI.$html.on('keydown.modal.uikit', function (e) { |
|
226 | ||
227 | if (active && e.keyCode === 27 && active.options.keyboard) { // ESC |
|
228 | e.preventDefault(); |
|
229 | active.hide(); |
|
230 | } |
|
231 | }); |
|
232 | }, |
|
233 | ||
234 | init: function() { |
|
235 | ||
236 | var $this = this; |
|
237 | ||
238 | this.options = UI.$.extend({ |
|
239 | target: $this.element.is('a') ? $this.element.attr('href') : false |
|
240 | }, this.options); |
|
241 | ||
242 | this.modal = UI.modal(this.options.target, this.options); |
|
243 | ||
244 | this.on("click", function(e) { |
|
245 | e.preventDefault(); |
|
246 | $this.show(); |
|
247 | }); |
|
248 | ||
249 | //methods |
|
250 | this.proxy(this.modal, 'show hide isActive'); |
|
251 | } |
|
252 | }); |
|
253 | ||
254 | UI.modal.dialog = function(content, options) { |
|
255 | ||
256 | var modal = UI.modal(UI.$(UI.modal.dialog.template).appendTo('body'), options); |
|
257 | ||
258 | modal.on('hide.uk.modal', function(){ |
|
259 | if (modal.persist) { |
|
260 | modal.persist.appendTo(modal.persist.data('modalPersistParent')); |
|
261 | modal.persist = false; |
|
262 | } |
|
263 | modal.element.remove(); |
|
264 | }); |
|
265 | ||
266 | setContent(content, modal); |
|
267 | ||
268 | return modal; |
|
269 | }; |
|
270 | ||
271 | UI.modal.dialog.template = '<div class="uk-modal"><div class="uk-modal-dialog" style="min-height:0;"></div></div>'; |
|
272 | ||
273 | UI.modal.alert = function(content, options) { |
|
274 | ||
275 | options = UI.$.extend(true, {bgclose:false, keyboard:false, modal:false, labels:UI.modal.labels}, options); |
|
276 | ||
277 | var modal = UI.modal.dialog(([ |
|
278 | '<div class="uk-margin uk-modal-content">'+String(content)+'</div>', |
|
279 | '<div class="uk-modal-footer uk-text-right"><button class="uk-button uk-button-primary uk-modal-close">'+options.labels.Ok+'</button></div>' |
|
280 | ]).join(""), options); |
|
281 | ||
282 | modal.on('show.uk.modal', function(){ |
|
283 | setTimeout(function(){ |
|
284 | modal.element.find('button:first').focus(); |
|
285 | }, 50); |
|
286 | }); |
|
287 | ||
288 | return modal.show(); |
|
289 | }; |
|
290 | ||
291 | UI.modal.confirm = function(content, onconfirm, oncancel) { |
|
292 | ||
293 | var options = arguments.length > 1 && arguments[arguments.length-1] ? arguments[arguments.length-1] : {}; |
|
294 | ||
295 | onconfirm = UI.$.isFunction(onconfirm) ? onconfirm : function(){}; |
|
296 | oncancel = UI.$.isFunction(oncancel) ? oncancel : function(){}; |
|
297 | options = UI.$.extend(true, {bgclose:false, keyboard:false, modal:false, labels:UI.modal.labels}, UI.$.isFunction(options) ? {}:options); |
|
298 | ||
299 | var modal = UI.modal.dialog(([ |
|
300 | '<div class="uk-margin uk-modal-content">'+String(content)+'</div>', |
|
301 | '<div class="uk-modal-footer uk-text-right"><button class="uk-button js-modal-confirm-cancel">'+options.labels.Cancel+'</button> <button class="uk-button uk-button-primary js-modal-confirm">'+options.labels.Ok+'</button></div>' |
|
302 | ]).join(""), options); |
|
303 | ||
304 | modal.element.find(".js-modal-confirm, .js-modal-confirm-cancel").on("click", function(){ |
|
305 | UI.$(this).is('.js-modal-confirm') ? onconfirm() : oncancel(); |
|
306 | modal.hide(); |
|
307 | }); |
|
308 | ||
309 | modal.on('show.uk.modal', function(){ |
|
310 | setTimeout(function(){ |
|
311 | modal.element.find('.js-modal-confirm').focus(); |
|
312 | }, 50); |
|
313 | }); |
|
314 | ||
315 | return modal.show(); |
|
316 | }; |
|
317 | ||
318 | UI.modal.prompt = function(text, value, onsubmit, options) { |
|
319 | ||
320 | onsubmit = UI.$.isFunction(onsubmit) ? onsubmit : function(value){}; |
|
321 | options = UI.$.extend(true, {bgclose:false, keyboard:false, modal:false, labels:UI.modal.labels}, options); |
|
322 | ||
323 | var modal = UI.modal.dialog(([ |
|
324 | text ? '<div class="uk-modal-content uk-form">'+String(text)+'</div>':'', |
|
325 | '<div class="uk-margin-small-top uk-modal-content uk-form"><p><input type="text" class="uk-width-1-1"></p></div>', |
|
326 | '<div class="uk-modal-footer uk-text-right"><button class="uk-button uk-modal-close">'+options.labels.Cancel+'</button> <button class="uk-button uk-button-primary js-modal-ok">'+options.labels.Ok+'</button></div>' |
|
327 | ]).join(""), options), |
|
328 | ||
329 | input = modal.element.find("input[type='text']").val(value || '').on('keyup', function(e){ |
|
330 | if (e.keyCode == 13) { |
|
331 | modal.element.find('.js-modal-ok').trigger('click'); |
|
332 | } |
|
333 | }); |
|
334 | ||
335 | modal.element.find('.js-modal-ok').on('click', function(){ |
|
336 | if (onsubmit(input.val())!==false){ |
|
337 | modal.hide(); |
|
338 | } |
|
339 | }); |
|
340 | ||
341 | return modal.show(); |
|
342 | }; |
|
343 | ||
344 | UI.modal.blockUI = function(content, options) { |
|
345 | ||
346 | var modal = UI.modal.dialog(([ |
|
347 | '<div class="uk-margin uk-modal-content">'+String(content || '<div class="uk-text-center">...</div>')+'</div>' |
|
348 | ]).join(""), UI.$.extend({bgclose:false, keyboard:false, modal:false}, options)); |
|
349 | ||
350 | modal.content = modal.element.find('.uk-modal-content:first'); |
|
351 | ||
352 | return modal.show(); |
|
353 | }; |
|
354 | ||
355 | UI.modal.labels = { |
|
356 | Ok: 'Ok', |
|
357 | Cancel: 'Cancel' |
|
358 | }; |
|
359 | ||
360 | // helper functions |
|
361 | function setContent(content, modal){ |
|
362 | ||
363 | if(!modal) return; |
|
364 | ||
365 | if (typeof content === 'object') { |
|
366 | ||
367 | // convert DOM object to a jQuery object |
|
368 | content = content instanceof jQuery ? content : UI.$(content); |
|
369 | ||
370 | if(content.parent().length) { |
|
371 | modal.persist = content; |
|
372 | modal.persist.data('modalPersistParent', content.parent()); |
|
373 | } |
|
374 | }else if (typeof content === 'string' || typeof content === 'number') { |
|
375 | // just insert the data as innerHTML |
|
376 | content = UI.$('<div></div>').html(content); |
|
377 | }else { |
|
378 | // unsupported data type! |
|
379 | content = UI.$('<div></div>').html('UIkit2.modal Error: Unsupported data type: ' + typeof content); |
|
380 | } |
|
381 | ||
382 | content.appendTo(modal.element.find('.uk-modal-dialog')); |
|
383 | ||
384 | return modal; |
|
385 | } |
|
386 | ||
387 | })(UIkit2); |
|
388 |