rework frontend for book abstracts
[wolnelektury.git] / src / wolnelektury / static / js / contrib / jquery.shorten.js
1 /*
2  * jQuery Shorten plugin 1.1.0
3  *
4  * Copyright (c) 2014 Viral Patel
5  * http://viralpatel.net
6  *
7  * Licensed under the MIT license:
8  *   http://www.opensource.org/licenses/mit-license.php
9  */
10
11 /*
12 ** updated by Jeff Richardson
13 ** Updated to use strict,
14 ** IE 7 has a "bug" It is returning undefined when trying to reference string characters in this format
15 ** content[i]. IE 7 allows content.charAt(i) This works fine in all modern browsers.
16 ** I've also added brackets where they weren't added just for readability (mostly for me).
17 */
18
19 /*
20 ** largely modified by Jan Szejko
21 ** Now it doesn't shorten the text, just changes adds a class and lets CSS do the rest
22  */
23
24 (function($) {
25     $.fn.shorten = function(settings) {
26         "use strict";
27
28         var config = {
29             showChars: 100,
30             minHideChars: 10,
31             moreText: "more",
32             lessText: "less",
33             onLess: function() {},
34             onMore: function() {},
35             errMsg: null,
36             force: false
37         };
38
39         if (settings) {
40             $.extend(config, settings);
41         }
42
43         if ($(this).data('jquery.shorten') && !config.force) {
44             return false;
45         }
46         $(this).data('jquery.shorten', true);
47
48         $(document).off("click", '.morelink');
49
50         $(document).on({
51             click: function() {
52
53                 var $this = $(this), $main = $this.prev();
54                 if ($main.hasClass('short')) {
55                     $main.removeClass('short');
56                     $this.html(config.lessText);
57                     config.onMore();
58                 } else {
59                     $main.addClass('short');
60                     $this.html(config.moreText);
61                     config.onLess();
62                 }
63                 return false;
64             }
65         }, '.morelink');
66
67         return this.each(function() {
68             var $this = $(this);
69             var button = '<a href="javascript:void(0)" class="morelink">' + config.moreText + '</a></span>';
70             $this.addClass('short');
71             $this.after(button);
72         });
73
74     };
75
76 })(jQuery);