source: pro-violet-viettel/docs/template/assets/js/uncompressed/x-editable/ace-editable.js @ 400

Last change on this file since 400 was 400, checked in by dungnv, 11 years ago
  • Property svn:mime-type set to text/plain
File size: 9.7 KB
Line 
1/**
2Image editable input.
3**/
4(function ($) {
5    "use strict";
6   
7    var Image = function (options) {
8        this.init('image', options, Image.defaults);
9
10                if('on_error' in options.image) {
11                        this.on_error = options.image['on_error'];
12                        delete options.image['on_error']
13                }
14                if('on_success' in options.image) {
15                        this.on_success = options.image['on_success'];
16                        delete options.image['on_success']
17                }
18                if('max_size' in options.image) {
19                        this.max_size = options.image['max_size'];
20                        delete options.image['max_size']
21                }
22
23                this.initImage(options, Image.defaults);
24    };
25
26    //inherit from Abstract input
27    $.fn.editableutils.inherit(Image, $.fn.editabletypes.abstractinput);
28
29    $.extend(Image.prototype, {
30                initImage: function(options, defaults) {
31          this.options.image = $.extend({}, defaults.image, options.image);
32                  this.name = this.options.image.name || 'editable-image-input';
33        },
34
35        /**
36        Renders input from tpl
37
38        @method render()
39        **/       
40        render: function() {
41                        var self = this;
42                        this.$input = this.$tpl.find('input[type=hidden]:eq(0)');
43                        this.$file = this.$tpl.find('input[type=file]:eq(0)');
44
45                        this.$file.attr({'name':this.name});
46                        this.$input.attr({'name':this.name+'-hidden'});
47
48                       
49                        this.options.image.before_change = this.options.image.before_change || function(files, dropped) {
50                                var file = files[0];
51                                if(typeof file === "string") {
52                                        //files is just a file name here (in browsers that don't support FileReader API)
53                                        if(! (/\.(jpe?g|png|gif)$/i).test(file) ) {
54                                                if(self.on_error) self.on_error(1);
55                                                return false;
56                                        }
57                                }
58                                else {//file is a File object
59                                        var type = $.trim(file.type);
60                                        if( ( type.length > 0 && ! (/^image\/(jpe?g|png|gif)$/i).test(type) )
61                                                        || ( type.length == 0 && ! (/\.(jpe?g|png|gif)$/i).test(file.name) )//for android default browser!
62                                                )
63                                        {
64                                                if(self.on_error) self.on_error(1);
65                                                return false;
66                                        }
67                                        if( self.max_size && file.size > self.max_size ) {
68                                                if(self.on_error) self.on_error(2);
69                                                return false;
70                                        }
71                                }
72
73                                if(self.on_success) self.on_success();
74                                return true;
75                        }
76                        this.options.image.before_remove = this.options.image.before_remove || function() {
77                                self.$input.val(null);
78                                return true;
79                        }
80
81                        this.$file.ace_file_input(this.options.image).on('change', function(){
82                                var $rand = (self.$file.val() || self.$file.data('ace_input_files')) ? Math.random() + "" + (new Date()).getTime() : null;
83                                self.$input.val($rand)//set a random value, so that selected file is uploaded each time, even if it's the same file, because inline editable plugin does not update if the value is not changed!
84                        }).closest('.ace-file-input').css({'width':'150px'}).closest('.editable-input').addClass('editable-image');
85                }
86
87    });
88
89       
90    Image.defaults = $.extend({}, $.fn.editabletypes.abstractinput.defaults, {
91        tpl: '<span><input type="hidden" /></span><span><input type="file" /></span>',
92        inputclass: '',
93                image:
94                {
95                        style: 'well',
96                        btn_choose: 'Change Image',
97                        btn_change: null,
98                        no_icon: 'icon-picture',
99                        thumbnail: 'large'
100                }
101    });
102
103    $.fn.editabletypes.image = Image;
104
105}(window.jQuery));
106
107
108
109
110
111
112
113
114
115
116//Wysiwyg
117(function ($) {
118    "use strict";
119   
120    var Wysiwyg = function (options) {
121        this.init('wysiwyg', options, Wysiwyg.defaults);
122       
123        //extend wysiwyg manually as $.extend not recursive
124        this.options.wysiwyg = $.extend({}, Wysiwyg.defaults.wysiwyg, options.wysiwyg);
125    };
126
127    $.fn.editableutils.inherit(Wysiwyg, $.fn.editabletypes.abstractinput);
128
129    $.extend(Wysiwyg.prototype, {
130        render: function () {
131                        this.$editor = this.$input.nextAll('.wysiwyg-editor:eq(0)');
132                       
133                        this.$tpl.parent().find('.wysiwyg-editor').show().ace_wysiwyg(
134                         {
135                                toolbar:
136                                [
137                                'bold',
138                                'italic',
139                                'strikethrough',
140                                'underline',
141                                null,
142                                'foreColor',
143                                null,
144                                'insertImage'
145                                ]
146                          }
147                        )
148                        .prev().addClass('wysiwyg-style2')
149                        .closest('.editable-input').addClass('editable-wysiwyg')
150                        .closest('.editable-container').css({'display':'block'});//if display is inline-block, putting large images inside the editor will expand it out of bounding box!
151
152                        if(this.options.wysiwyg && this.options.wysiwyg.css)
153                                this.$tpl.closest('.editable-wysiwyg').css(this.options.wysiwyg.css);
154        },
155
156
157        value2html: function(value, element) {
158            $(element).html(value);
159                        return false;
160        },
161
162        html2value: function(html) {
163                        return html;
164        },
165
166        value2input: function(value) {
167                        this.$editor.html(value);
168        },
169                input2value: function() {
170                        return this.$editor.html();
171        },
172
173        activate: function() {
174           //this.$editor.focus().get(0).setSelectionRange(200,200);
175        }
176    });
177       
178
179
180    Wysiwyg.defaults = $.extend({}, $.fn.editabletypes.abstractinput.defaults, {
181                tpl: '<input type="hidden" /><div class="wysiwyg-editor"></div>',
182        inputclass: 'editable-wysiwyg',
183        wysiwyg: {
184           
185        }
186    });
187
188    $.fn.editabletypes.wysiwyg = Wysiwyg;
189
190}(window.jQuery));
191
192
193
194
195
196
197
198
199/**
200Spinner editable input.
201**/
202(function ($) {
203    "use strict";
204   
205    var Spinner = function (options) {
206        this.init('spinner', options, Spinner.defaults);
207                this.initSpinner(options, Spinner.defaults);
208    };
209
210    //inherit from Abstract input
211    $.fn.editableutils.inherit(Spinner, $.fn.editabletypes.abstractinput);
212
213    $.extend(Spinner.prototype, {
214                initSpinner: function(options, defaults) {
215            this.options.spinner = $.extend({}, defaults.spinner, options.spinner);
216        },
217
218        /**
219        Renders input from tpl
220
221        @method render()
222        **/       
223        render: function() {
224                },
225       
226        /**
227        Activates input: sets focus on the first field.
228       
229        @method activate()
230       **/       
231       activate: function() {
232            if(this.$input.is(':visible')) {
233                                this.$input.focus();
234                                $.fn.editableutils.setCursorPosition(this.$input.get(0), this.$input.val().length);
235                               
236                                var val = parseInt(this.$input.val());
237                                var options = $.extend({value:val}, this.options.spinner);
238                                this.$input.ace_spinner(options);
239            }
240       },
241       
242       /**
243        Attaches handler to submit form in case of 'showbuttons=false' mode
244       
245        @method autosubmit()
246       **/       
247       autosubmit: function() {
248           this.$input.keydown(function (e) {
249                if (e.which === 13) {
250                    $(this).closest('form').submit();
251                }
252           });
253       }       
254    });
255
256    Spinner.defaults = $.extend({}, $.fn.editabletypes.abstractinput.defaults, {
257        tpl: '<input type="text" />',
258        inputclass: '',
259                spinner:{
260                        min:0,
261                        max:100,
262                        step:1,
263                        icon_up:'icon-plus',
264                        icon_down:'icon-minus',
265                        btn_up_class:'btn-success',
266                        btn_down_class:'btn-danger'
267        }
268    });
269
270    $.fn.editabletypes.spinner = Spinner;
271
272}(window.jQuery));
273
274
275
276
277
278
279
280
281/**
282Slider editable input.
283**/
284(function ($) {
285    "use strict";
286   
287    var Slider = function (options) {
288        this.init('slider', options, Slider.defaults);
289                this.initSlider(options, Slider.defaults);
290    };
291
292    //inherit from Abstract input
293    $.fn.editableutils.inherit(Slider, $.fn.editabletypes.abstractinput);
294
295    $.extend(Slider.prototype, {
296                initSlider: function(options, defaults) {
297            this.options.slider = $.extend({}, defaults.slider, options.slider);
298        },
299
300        /**
301        Renders input from tpl
302
303        @method render()
304        **/       
305        render: function() {
306                },
307        /**
308        Activates input: sets focus on the first field.
309       
310        @method activate()
311       **/
312       activate: function() {
313            if(this.$input.is(':visible')) {
314                                this.$input.focus();
315                                $.fn.editableutils.setCursorPosition(this.$input.get(0), this.$input.val().length);
316
317                                        var self = this;
318                                        var val = parseInt(this.$input.val());
319                                        var width = this.options.slider.width || 200;
320                                        var options = $.extend(this.options.slider , {
321                                                value:val,
322                                                slide: function( event, ui ) {
323                                                        var val = parseInt(ui.value);
324                                                        self.$input.val(val);
325                                                       
326                                                        if(ui.handle.firstChild == null) {/* no tooltips attached to it */
327                                                                $(ui.handle).append("<div class='tooltip top in' style='display:none;top:-38px;left:-5px;'><div class='tooltip-arrow'></div><div class='tooltip-inner'></div></div>");
328                                                        }
329                                                        $(ui.handle.firstChild).show().children().eq(1).text(val);
330                                                }
331                                        });
332
333                                        this.$input.parent().addClass('editable-slider').css('width', width+'px').slider(options);
334            }
335       },
336           
337           value2html: function(value, element) {
338       },
339
340       /**
341        Attaches handler to submit form in case of 'showbuttons=false' mode
342       
343        @method autosubmit()
344       **/       
345       autosubmit: function() {
346           this.$input.keydown(function (e) {
347                if (e.which === 13) {
348                    $(this).closest('form').submit();
349                }
350           });
351       }       
352    });
353
354    Slider.defaults = $.extend({}, $.fn.editabletypes.abstractinput.defaults, {
355        tpl: '<input type="text" /><span class="inline ui-slider-green"><span class="slider-display"></span></span>',
356        inputclass: '',
357                slider:{
358                        min:1,
359                        max:100,
360                        step:1,
361                        range: "min"
362        }
363    });
364
365    $.fn.editabletypes.slider = Slider;
366
367}(window.jQuery));
368
Note: See TracBrowser for help on using the repository browser.