colly_wyx
2018-05-29 d479d9fbcb37f8b861031c94a83d0e37761210c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* ==========================================================
 * sco.confirm.js
 * http://github.com/terebentina/sco.js
 * ==========================================================
 * Copyright 2013 Dan Caragea.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== */
 
/*jshint laxcomma:true, sub:true, browser:true, jquery:true, devel:true */
 
;(function($, undefined) {
    "use strict";
 
    var pluginName = 'scojs_confirm';
 
    function Confirm(options) {
        this.options = $.extend({}, $.fn[pluginName].defaults, options);
 
        var $modal = $(this.options.target);
        if (!$modal.length) {
            $modal = $('<div class="modal" id="' + this.options.target.substr(1) + '"><div class="modal-body inner"/><div class="modal-footer"><a class="btn cancel" href="#" data-dismiss="modal">cancel</a> <a href="#" class="btn btn-danger" data-action="1">yes</a></div></div>').appendTo(this.options.appendTo).hide();
            if (typeof this.options.action == 'function') {
                var self = this;
                $modal.find('[data-action]').attr('href', '#').on('click.' + pluginName, function(e) {
                    e.preventDefault();
                    self.options.action.call(self);
                    self.close();
                });
            } else if (typeof this.options.action == 'string') {
                $modal.find('[data-action]').attr('href', this.options.action);
            }
        }
        this.scomodal = $.scojs_modal(this.options);
    }
 
    $.extend(Confirm.prototype, {
        show: function() {
            this.scomodal.show();
            return this;
        }
 
        ,close: function() {
            this.scomodal.close();
            return this;
        }
 
        ,destroy: function() {
            this.scomodal.destroy();
            return this;
        }
    });
 
 
    $.fn[pluginName] = function(options) {
        return this.each(function() {
            var obj;
            if (!(obj = $.data(this, pluginName))) {
                var $this = $(this)
                    ,data = $this.data()
                    ,title = $this.attr('title') || data.title
                    ,opts = $.extend({}, $.fn[pluginName].defaults, options, data)
                    ;
                if (!title) {
                    title = 'this';
                }
                opts.content = opts.content.replace(':title', title);
                if (!opts.action) {
                    opts.action = $this.attr('href');
                } else if (typeof window[opts.action] == 'function') {
                    opts.action = window[opts.action];
                }
                obj = new Confirm(opts);
                $.data(this, pluginName, obj);
            }
            obj.show();
        });
    };
 
    $[pluginName] = function(options) {
        return new Confirm(options);
    };
 
    $.fn[pluginName].defaults = {
        content: 'Are you sure you want to delete :title?'
        ,cssclass: 'confirm_modal'
        ,target: '#confirm_modal'    // this must be an id. This is a limitation for now, @todo should be fixed
        ,appendTo: 'body'    // where should the modal be appended to (default to document.body). Added for unit tests, not really needed in real life.
    };
 
    $(document).on('click.' + pluginName, '[data-trigger="confirm"]', function() {
        $(this)[pluginName]();
        return false;
    });
})(jQuery);