├── grapher
├── index.php
├── chosen-sprite.png
├── chosen-sprite@2x.png
├── grapher.css
├── chosen.css
├── grapher.js
└── chosen.jquery.js
├── grapher.php
└── README.md
/grapher/index.php:
--------------------------------------------------------------------------------
1 | header('Location: ../grapher.php'); ?>
2 |
--------------------------------------------------------------------------------
/grapher/chosen-sprite.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sepich/zabbixGrapher/HEAD/grapher/chosen-sprite.png
--------------------------------------------------------------------------------
/grapher/chosen-sprite@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sepich/zabbixGrapher/HEAD/grapher/chosen-sprite@2x.png
--------------------------------------------------------------------------------
/grapher.php:
--------------------------------------------------------------------------------
1 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
41 |
42 |
45 |
46 |
47 |
48 |
49 |
50 | require_once dirname(__FILE__).'/include/page_footer.php';
51 |
--------------------------------------------------------------------------------
/grapher/grapher.css:
--------------------------------------------------------------------------------
1 | /*3.2*/
2 | .header-title {
3 | text-align: left;
4 | }
5 | .header-title ul {
6 | bottom: 6px;
7 | position: absolute;
8 | right: 0;
9 | }
10 |
11 | /*hide chosen items*/
12 | .chosen-container-multi .chosen-choices li.search-field input[type="text"] {
13 | height: inherit;
14 | }
15 | .chosen-container-multi .chosen-choices li.search-choice {
16 | display: none;
17 | }
18 |
19 | /*allow deselect selected*/
20 | .chosen-container-multi .chosen-drop .result-selected {
21 | color: #fff;
22 | background-color: #4885e3;
23 | cursor: pointer;
24 | display: list-item;
25 | }
26 | .chosen-container-multi .chosen-choices li.search-field {
27 | width: 100%;
28 | white-space: inherit;
29 | }
30 | .chosen-container .chosen-results li.highlighted,li.selected-result{
31 | background-color:#2a62bc;
32 | background-image: none;
33 | color:#fff
34 | }
35 | .header-title .chosen-container ul {bottom: 0;}
36 | .header-title .chosen-container ul li {float: inherit;}
37 | /*select optgroup*/
38 | .chosen-container .chosen-results li.group-result {cursor: pointer;}
39 |
40 | /* number hint and clear button*/
41 | .chsn-hint, .chsn-clean {
42 | color: #999;
43 | display: inline;
44 | float: right;
45 | padding-top: 4px;
46 | }
47 | .chsn-clean {
48 | font-size: 21px;
49 | cursor: pointer;
50 | font-weight: 700;
51 | font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
52 | padding-left: 10px;
53 | }
54 | .chsn-clean:hover {
55 | color: #F00;
56 | }
57 |
58 | /* pics */
59 | #pics {
60 | text-align: center;
61 | }
62 | div.flickerfreescreen {
63 | display: inline-block;
64 | position: relative;
65 | margin: 10px;
66 | vertical-align: top;
67 | width: 730px;
68 | min-height: 400px;
69 | }
70 | div.flickerfreescreen:hover div.close-graph {display: block;}
71 | div.close-graph {
72 | display: none;
73 | font-size: 21px;
74 | cursor: pointer;
75 | font-weight: 700;
76 | font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
77 | color: #ccc;
78 | position: absolute;
79 | right: 10px;
80 | top: 0;
81 | z-index: 10;
82 | }
83 | div.close-graph:after {content: '\00d7';}
84 | div.close-graph:hover {color: #F00;}
85 |
86 | /*pager*/
87 | span.link.bold {
88 | text-decoration: none;
89 | cursor: default;
90 | }
91 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # zabbixGrapher
2 | This is one page js application for quick search/view zabbix graphs, dinamically graph any collected item
3 | * First step is to select host, just start typing HostGroup name or Hostname
4 | 
5 | If you choose one Host - all its graphs would start drawing right away (quick host overview supposed).
6 | If you select multiple Hosts - no graphs would be drawn (manual comparison supposed) and you can draw graphs for all selected Hosts by clicking `Select All`
7 | 
8 | * You can also draw any items from selected hosts on single graph
9 | Select items in interest and then click to create either `Normal` or `Stacked` graph
10 | 
11 | * Time Zooming using usual Zabbix timeline and area selection on graphs supported
12 | * You can click group name in list to select/deselect all subitems ("Filesystems" in screenshot above)
13 | * To share selected Graphs just copy and send the URL (it dynamically updated with current state)
14 |
15 | ##### Installation
16 | Unzip to root of your zabbix-web folder (Note: `master` branch is for Zabbix 3.0, use `zabbix2.4` branch for older versions).
17 | Then add to `Main Menu` with something like this:
18 | ```diff
19 | +++ ./include/menu.inc.php 2015-12-16 00:49:20.939693369 -0800
20 | @@ -79,6 +79,10 @@
21 | 'sub_pages' => array('chart2.php', 'chart3.php', 'chart6.php', 'chart7.php')
22 | ],
23 | + [
24 | + 'url' => 'grapher.php',
25 | + 'label' => _('Grapher')
26 | + ],
27 | [
28 | 'url' => 'screens.php',
29 | 'label' => _('Screens'),
30 | 'sub_pages' => array('slides.php')
31 |
32 | ```
33 | After Installation you could realise that your zabbix-fronted is not powerful enough to render so many images on one page at once. If so - check out this caching story: http://blog.sepa.spb.ru/2016/01/speed-up-zabbix-graphs-with-nginx.html
34 |
35 | ##### Options
36 | You can tweak some options in the beginning of `grapher.js` file. Please verify URL, if your Zabbix installed to /zabbix it should be `url: '/zabbix/api_jsonrpc.php'`
37 | ```js
38 | // main ---------------------------------------------------------------------
39 | jQuery(function() {
40 | var options = {
41 | url: '/api_jsonrpc.php', // zabbix API url
42 | timeout: 5000, // to API in msec
43 | ssid: getCookie('zbx_sessionid'), // key to API, get from current logged in user
44 | pagelen: 24, // graphs per page
45 | width: 600, // of graph
46 | height: 200 // of graph
47 | },
48 | ```
49 |
--------------------------------------------------------------------------------
/grapher/chosen.css:
--------------------------------------------------------------------------------
1 | /* Chosen v1.4.2 | (c) 2011-2015 by Harvest | MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md */
2 |
3 | .chosen-container{position:relative;display:inline-block;vertical-align:middle;font-size:13px;zoom:1;*display:inline;-webkit-user-select:none;-moz-user-select:none;user-select:none}.chosen-container *{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.chosen-container .chosen-drop{position:absolute;top:100%;left:-9999px;z-index:1010;width:100%;border:1px solid #aaa;border-top:0;background:#fff;box-shadow:0 4px 5px rgba(0,0,0,.15)}.chosen-container.chosen-with-drop .chosen-drop{left:0}.chosen-container a{cursor:pointer}.chosen-container .search-choice .group-name,.chosen-container .chosen-single .group-name{margin-right:4px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;font-weight:400;color:#999}.chosen-container .search-choice .group-name:after,.chosen-container .chosen-single .group-name:after{content:":";padding-left:2px;vertical-align:top}.chosen-container-single .chosen-single{position:relative;display:block;overflow:hidden;padding:0 0 0 8px;height:25px;border:1px solid #aaa;border-radius:5px;background-color:#fff;background:-webkit-gradient(linear,50% 0,50% 100%,color-stop(20%,#fff),color-stop(50%,#f6f6f6),color-stop(52%,#eee),color-stop(100%,#f4f4f4));background:-webkit-linear-gradient(top,#fff 20%,#f6f6f6 50%,#eee 52%,#f4f4f4 100%);background:-moz-linear-gradient(top,#fff 20%,#f6f6f6 50%,#eee 52%,#f4f4f4 100%);background:-o-linear-gradient(top,#fff 20%,#f6f6f6 50%,#eee 52%,#f4f4f4 100%);background:linear-gradient(top,#fff 20%,#f6f6f6 50%,#eee 52%,#f4f4f4 100%);background-clip:padding-box;box-shadow:0 0 3px #fff inset,0 1px 1px rgba(0,0,0,.1);color:#444;text-decoration:none;white-space:nowrap;line-height:24px}.chosen-container-single .chosen-default{color:#999}.chosen-container-single .chosen-single span{display:block;overflow:hidden;margin-right:26px;text-overflow:ellipsis;white-space:nowrap}.chosen-container-single .chosen-single-with-deselect span{margin-right:38px}.chosen-container-single .chosen-single abbr{position:absolute;top:6px;right:26px;display:block;width:12px;height:12px;background:url(chosen-sprite.png) -42px 1px no-repeat;font-size:1px}.chosen-container-single .chosen-single abbr:hover{background-position:-42px -10px}.chosen-container-single.chosen-disabled .chosen-single abbr:hover{background-position:-42px -10px}.chosen-container-single .chosen-single div{position:absolute;top:0;right:0;display:block;width:18px;height:100%}.chosen-container-single .chosen-single div b{display:block;width:100%;height:100%;background:url(chosen-sprite.png) no-repeat 0 2px}.chosen-container-single .chosen-search{position:relative;z-index:1010;margin:0;padding:3px 4px;white-space:nowrap}.chosen-container-single .chosen-search input[type=text]{margin:1px 0;padding:4px 20px 4px 5px;width:100%;height:auto;outline:0;border:1px solid #aaa;background:#fff url(chosen-sprite.png) no-repeat 100% -20px;background:url(chosen-sprite.png) no-repeat 100% -20px;font-size:1em;font-family:sans-serif;line-height:normal;border-radius:0}.chosen-container-single .chosen-drop{margin-top:-1px;border-radius:0 0 4px 4px;background-clip:padding-box}.chosen-container-single.chosen-container-single-nosearch .chosen-search{position:absolute;left:-9999px}.chosen-container .chosen-results{color:#444;position:relative;overflow-x:hidden;overflow-y:auto;margin:0 4px 4px 0;padding:0 0 0 4px;max-height:240px;-webkit-overflow-scrolling:touch}.chosen-container .chosen-results li{display:none;margin:0;padding:5px 6px;list-style:none;line-height:15px;word-wrap:break-word;-webkit-touch-callout:none}.chosen-container .chosen-results li.active-result{display:list-item;cursor:pointer}.chosen-container .chosen-results li.disabled-result{display:list-item;color:#ccc;cursor:default}.chosen-container .chosen-results li.highlighted,li.selected-result{background-color:#3875d7;background-image:-webkit-gradient(linear,50% 0,50% 100%,color-stop(20%,#3875d7),color-stop(90%,#2a62bc));background-image:-webkit-linear-gradient(#3875d7 20%,#2a62bc 90%);background-image:-moz-linear-gradient(#3875d7 20%,#2a62bc 90%);background-image:-o-linear-gradient(#3875d7 20%,#2a62bc 90%);background-image:linear-gradient(#3875d7 20%,#2a62bc 90%);color:#fff}.chosen-container .chosen-results li.no-results{color:#777;display:list-item;background:#f4f4f4}.chosen-container .chosen-results li.group-result{display:list-item;font-weight:700;cursor:default}.chosen-container .chosen-results li.group-option{padding-left:15px}.chosen-container .chosen-results li em{font-style:normal;text-decoration:underline}.chosen-container-multi .chosen-choices{position:relative;overflow:hidden;margin:0;padding:0 5px;width:100%;height:auto!important;height:1%;border:1px solid #aaa;background-color:#fff;background-image:-webkit-gradient(linear,50% 0,50% 100%,color-stop(1%,#eee),color-stop(15%,#fff));background-image:-webkit-linear-gradient(#eee 1%,#fff 15%);background-image:-moz-linear-gradient(#eee 1%,#fff 15%);background-image:-o-linear-gradient(#eee 1%,#fff 15%);background-image:linear-gradient(#eee 1%,#fff 15%);cursor:text}.chosen-container-multi .chosen-choices li{float:left;list-style:none}.chosen-container-multi .chosen-choices li.search-field{margin:0;padding:0;white-space:nowrap}.chosen-container-multi .chosen-choices li.search-field input[type=text]{margin:1px 0;padding:0;height:25px;outline:0;border:0!important;background:transparent!important;box-shadow:none;color:#999;font-size:100%;font-family:sans-serif;line-height:normal;border-radius:0}.chosen-container-multi .chosen-choices li.search-choice{position:relative;margin:3px 5px 3px 0;padding:3px 20px 3px 5px;border:1px solid #aaa;max-width:100%;border-radius:3px;background-color:#eee;background-image:-webkit-gradient(linear,50% 0,50% 100%,color-stop(20%,#f4f4f4),color-stop(50%,#f0f0f0),color-stop(52%,#e8e8e8),color-stop(100%,#eee));background-image:-webkit-linear-gradient(#f4f4f4 20%,#f0f0f0 50%,#e8e8e8 52%,#eee 100%);background-image:-moz-linear-gradient(#f4f4f4 20%,#f0f0f0 50%,#e8e8e8 52%,#eee 100%);background-image:-o-linear-gradient(#f4f4f4 20%,#f0f0f0 50%,#e8e8e8 52%,#eee 100%);background-image:linear-gradient(#f4f4f4 20%,#f0f0f0 50%,#e8e8e8 52%,#eee 100%);background-size:100% 19px;background-repeat:repeat-x;background-clip:padding-box;box-shadow:0 0 2px #fff inset,0 1px 0 rgba(0,0,0,.05);color:#333;line-height:13px;cursor:default}.chosen-container-multi .chosen-choices li.search-choice span{word-wrap:break-word}.chosen-container-multi .chosen-choices li.search-choice .search-choice-close{position:absolute;top:4px;right:3px;display:block;width:12px;height:12px;background:url(chosen-sprite.png) -42px 1px no-repeat;font-size:1px}.chosen-container-multi .chosen-choices li.search-choice .search-choice-close:hover{background-position:-42px -10px}.chosen-container-multi .chosen-choices li.search-choice-disabled{padding-right:5px;border:1px solid #ccc;background-color:#e4e4e4;background-image:-webkit-gradient(linear,50% 0,50% 100%,color-stop(20%,#f4f4f4),color-stop(50%,#f0f0f0),color-stop(52%,#e8e8e8),color-stop(100%,#eee));background-image:-webkit-linear-gradient(top,#f4f4f4 20%,#f0f0f0 50%,#e8e8e8 52%,#eee 100%);background-image:-moz-linear-gradient(top,#f4f4f4 20%,#f0f0f0 50%,#e8e8e8 52%,#eee 100%);background-image:-o-linear-gradient(top,#f4f4f4 20%,#f0f0f0 50%,#e8e8e8 52%,#eee 100%);background-image:linear-gradient(top,#f4f4f4 20%,#f0f0f0 50%,#e8e8e8 52%,#eee 100%);color:#666}.chosen-container-multi .chosen-choices li.search-choice-focus{background:#d4d4d4}.chosen-container-multi .chosen-choices li.search-choice-focus .search-choice-close{background-position:-42px -10px}.chosen-container-multi .chosen-results{margin:0;padding:0}.chosen-container-multi .chosen-drop .result-selected{display:list-item;color:#ccc;cursor:default}.chosen-container-active .chosen-single{border:1px solid #5897fb;box-shadow:0 0 5px rgba(0,0,0,.3)}.chosen-container-active.chosen-with-drop .chosen-single{border:1px solid #aaa;-moz-border-radius-bottomright:0;border-bottom-right-radius:0;-moz-border-radius-bottomleft:0;border-bottom-left-radius:0;background-image:-webkit-gradient(linear,50% 0,50% 100%,color-stop(20%,#eee),color-stop(80%,#fff));background-image:-webkit-linear-gradient(#eee 20%,#fff 80%);background-image:-moz-linear-gradient(#eee 20%,#fff 80%);background-image:-o-linear-gradient(#eee 20%,#fff 80%);background-image:linear-gradient(#eee 20%,#fff 80%);box-shadow:0 1px 0 #fff inset}.chosen-container-active.chosen-with-drop .chosen-single div{border-left:0;background:transparent}.chosen-container-active.chosen-with-drop .chosen-single div b{background-position:-18px 2px}.chosen-container-active .chosen-choices{border:1px solid #5897fb;box-shadow:0 0 5px rgba(0,0,0,.3)}.chosen-container-active .chosen-choices li.search-field input[type=text]{color:#222!important}.chosen-disabled{opacity:.5!important;cursor:default}.chosen-disabled .chosen-single{cursor:default}.chosen-disabled .chosen-choices .search-choice .search-choice-close{cursor:default}.chosen-rtl{text-align:right}.chosen-rtl .chosen-single{overflow:visible;padding:0 8px 0 0}.chosen-rtl .chosen-single span{margin-right:0;margin-left:26px;direction:rtl}.chosen-rtl .chosen-single-with-deselect span{margin-left:38px}.chosen-rtl .chosen-single div{right:auto;left:3px}.chosen-rtl .chosen-single abbr{right:auto;left:26px}.chosen-rtl .chosen-choices li{float:right}.chosen-rtl .chosen-choices li.search-field input[type=text]{direction:rtl}.chosen-rtl .chosen-choices li.search-choice{margin:3px 5px 3px 0;padding:3px 5px 3px 19px}.chosen-rtl .chosen-choices li.search-choice .search-choice-close{right:auto;left:4px}.chosen-rtl.chosen-container-single-nosearch .chosen-search,.chosen-rtl .chosen-drop{left:9999px}.chosen-rtl.chosen-container-single .chosen-results{margin:0 0 4px 4px;padding:0 4px 0 0}.chosen-rtl .chosen-results li.group-option{padding-right:15px;padding-left:0}.chosen-rtl.chosen-container-active.chosen-with-drop .chosen-single div{border-right:0}.chosen-rtl .chosen-search input[type=text]{padding:4px 5px 4px 20px;background:#fff url(chosen-sprite.png) no-repeat -30px -20px;background:url(chosen-sprite.png) no-repeat -30px -20px;direction:rtl}.chosen-rtl.chosen-container-single .chosen-single div b{background-position:6px 2px}.chosen-rtl.chosen-container-single.chosen-with-drop .chosen-single div b{background-position:-12px 2px}@media only screen and (-webkit-min-device-pixel-ratio:1.5),only screen and (min-resolution:144dpi),only screen and (min-resolution:1.5dppx){.chosen-rtl .chosen-search input[type=text],.chosen-container-single .chosen-single abbr,.chosen-container-single .chosen-single div b,.chosen-container-single .chosen-search input[type=text],.chosen-container-multi .chosen-choices .search-choice .search-choice-close,.chosen-container .chosen-results-scroll-down span,.chosen-container .chosen-results-scroll-up span{background-image:url(chosen-sprite@2x.png)!important;background-size:52px 37px!important;background-repeat:no-repeat!important}}
--------------------------------------------------------------------------------
/grapher/grapher.js:
--------------------------------------------------------------------------------
1 | // zabbixGrapher
2 | // https://github.com/sepich/zabbixGrapher
3 |
4 | jQuery(function() {
5 | var options = {
6 | url: '/api_jsonrpc.php', // zabbix API url
7 | timeout: 5000, // to API in msec
8 | ssid: getCookie('zbx_sessionid'), // key to API, get from current logged in user
9 | pagelen: 24, // graphs per page
10 | width: 600, // of graph
11 | height: 200 // of graph
12 | },
13 | itemgraphs=[], //array of items obj to draw
14 | skipHistory=false, //do not save this draw to history (from navigation)
15 | timeoutID,
16 | $=jQuery;
17 | console.log('starting');
18 |
19 | //timeline init
20 | window.flickerfreeScreenShadow.timeout = 30000 ;
21 | window.flickerfreeScreenShadow.responsiveness = 10000;
22 | var d=new Date();
23 | d.setFullYear(d.getFullYear() - 1);
24 | timeControl.addObject("scrollbar", {
25 | "period": 10800,
26 | "starttime": cdumpts(d),
27 | "isNow": 1
28 | },
29 | {
30 | "id":'scrollbar',
31 | "loadScroll":1,
32 | "mainObject":1,
33 | "periodFixed":'1',
34 | "sliderMaximumTimePeriod":63072000
35 | });
36 | timeControl.processObjects();
37 | cookie.init();
38 |
39 | //ZabbixApi wrapper
40 | function ZabbixApi(method, params, success) {
41 | $.ajax({
42 | contentType: 'application/json-rpc',
43 | type: 'POST',
44 | timeout: options.timeout,
45 | url: options.url,
46 | data: JSON.stringify({
47 | jsonrpc: '2.0',
48 | method: method,
49 | id: 0,
50 | auth: options.ssid,
51 | params: params,
52 | }),
53 | success: function(response, status) {
54 | success(response, status);
55 | }
56 | });
57 | }
58 |
59 | //Hosts list
60 | $("#hosts").chosen({search_contains: true}).change(function(e){
61 | console.log('hosts changed');
62 | updateHint(this);
63 | //delay update while user is clicking (shift-click race)
64 | window.clearTimeout(timeoutID);
65 | timeoutID = window.setTimeout(function(){
66 | updateGraphs();
67 | updateItems();
68 | }, 1500);
69 | });
70 | //fill in hosts list on start
71 | ZabbixApi('hostgroup.get', {
72 | monitored_hosts: 1,
73 | sortfield: 'name',
74 | selectHosts: ['name'],
75 | output: ['name']
76 | },
77 | function(r){
78 | $.each(r.result, function(){
79 | var og = $('');
80 | $.each(this.hosts, function(){
81 | $('').html(this.name).appendTo(og);
82 | });
83 | og.appendTo( $('#hosts') );
84 | });
85 | $('#hosts').trigger("chosen:updated");
86 | resoreState(readUrl()); // on page load try to read state from URL
87 | }
88 | );
89 |
90 | //Graphs list
91 | $("#graphs").chosen({search_contains: true}).change(function(e){
92 | console.log('graphs changed');
93 | updateHint(this);
94 | drawGraphs();
95 | });
96 | //Update graphs lists
97 | function updateGraphs(toselect){
98 | if($('#hosts').val() == null){
99 | $('#graphs').empty();
100 | $('#graphs').trigger("chosen:updated");
101 | $('#graphs').trigger('change');
102 | }
103 | else {
104 | ZabbixApi('graph.get', {
105 | hostids: $('#hosts').val(),
106 | expandName: 1,
107 | output: ['name','graphtype'],
108 | sortfield: 'name',
109 | },
110 | function(r){
111 | //parse graphs to draw as name=[ids,]
112 | var graphs={}
113 | $.each(r.result, function(){
114 | if(graphs[this.name]==undefined) graphs[this.name]=[this.graphid];
115 | else graphs[this.name].push(this.graphid);
116 | });
117 | //update options vals to store already selected
118 | $.each($('#graphs option'), function(){
119 | var k=$(this).text();
120 | if(k in graphs){
121 | $(this).val(graphs[k]);
122 | delete graphs[k];
123 | }
124 | else $(this).remove();
125 | });
126 | //add new options
127 | $.each(graphs, function(k,v){
128 | $('').html(k).appendTo($('#graphs'));
129 | });
130 | //select deferred or all if only one Host selected
131 | if(toselect!=null) selectGraphs(toselect);
132 | else if($('#hosts').val().length==1 && $('#graphs').val()==null) $('#graphs option').prop('selected', true);
133 | $('#graphs').trigger("chosen:updated");
134 | $('#graphs').trigger('change');
135 | });
136 | }
137 | }
138 | //Select All Graphs
139 | $('#graphs-all').click(function(){
140 | $('#graphs option').prop('selected', true);
141 | $('#graphs').trigger("chosen:updated");
142 | $('#graphs').trigger('change');
143 | });
144 |
145 | //Items list
146 | $("#items").chosen({search_contains: true}).change(function(e){
147 | console.log('items changed');
148 | updateHint(this);
149 | });
150 | //Update items lists
151 | function updateItems(){
152 | $('#items').empty();
153 | if($('#hosts').val() == null){
154 | if(itemgraphs.length){
155 | itemgraphs=[];
156 | drawGraphs();
157 | }
158 | $('#items').trigger("chosen:updated");
159 | $('#items').trigger('change');
160 | return;
161 | }
162 | ZabbixApi('item.get', {
163 | hostids: $('#hosts').val(),
164 | selectApplications: ['name'],
165 | filter: {
166 | state: 0, //supported
167 | status: 0, //enabled
168 | value_type: [0,3] //numeric
169 | },
170 | output: ['name','description','error','key_','units'],
171 | sortfield: 'name',
172 | },
173 | function(r){
174 | apps={}
175 | $.each(r.result, function(){
176 | var app=(this.applications.length)? this.applications[0].name : '-';
177 | //expand $1 in name from .key_
178 | if(~this.name.indexOf('$')){
179 | var keys=/\[([^\]]+)\]/.exec(this.key_);
180 | if(keys[1]!=undefined){
181 | keys=keys[1].split(',');
182 | for (var i=1; i<=keys.length; i++) {
183 | this.name=this.name.replace('$'+i, keys[i-1].trim());
184 | }
185 | }
186 | }
187 | if(apps[app]==undefined) apps[app]={}
188 | if(apps[app][this.name]==undefined) apps[app][this.name]=[this.itemid];
189 | else apps[app][this.name].push(this.itemid);
190 | });
191 | $.each(apps, function(app,items){
192 | var og = $('');
193 | $.each(items, function(i,v){
194 | $('').html(i).appendTo(og);
195 | });
196 | og.appendTo( $('#items') );
197 | });
198 | $('#items').trigger("chosen:updated");
199 | $('#items').trigger('change');
200 | }
201 | );
202 | }
203 | //Add item graph
204 | $('span.itemgraph').click(function(){
205 | if($('#items').val()!=null){
206 | var items=[]
207 | $.each( $('#items').val(), function(){
208 | items=items.concat(this.split(','));
209 | });
210 | itemgraphs.push( {items: items, type: $(this).data('type'), id: Math.random().toString(16).slice(2)} );
211 | $('#items option:selected').removeAttr('selected');
212 | $('#items').trigger('chosen:updated');
213 | $('#items').trigger('change');
214 | drawGraphs();
215 | }
216 | });
217 |
218 | //Number hint and clear button
219 | ['hosts','graphs','items'].each(function(i){
220 | $('#'+i+'_chosen li.search-field').append('');
221 | });
222 | $('li.search-field').click(function(){
223 | $(this).find('input').focus();
224 | });
225 | $('.chsn-clean').click(function(){
226 | var s=$(this).closest('.chosen-container').prev('select');
227 | s.find('option:selected').removeAttr('selected');
228 | s.trigger('chosen:updated');
229 | s.trigger('change');
230 | $(this).prev('input').val('');
231 | });
232 | function updateHint(o){
233 | if($(o).val() == null){
234 | $(o).next().find('.chsn-hint').html('');
235 | $(o).next().find('.chsn-clean').html('');
236 | }
237 | else {
238 | $(o).next().find('.chsn-hint').html( $(o).val().length + ' selected' );
239 | $(o).next().find('.chsn-clean').html('×');
240 | }
241 | }
242 | //Click optgroup to toggle all subitems
243 | $(document).on('click', '.group-result', function() {
244 | var unselected = $(this).nextUntil('.group-result').not('.result-selected');
245 | if(unselected.length) unselected.trigger('mouseup');
246 | else $(this).nextUntil('.group-result').each(function() {
247 | $('a.search-choice-close[data-option-array-index="' + $(this).data('option-array-index') + '"]').trigger('click');
248 | });
249 | });
250 |
251 | //Draw selected graphs (page)
252 | function drawGraphs(page){
253 | //cleanup
254 | $('#pics').empty();
255 | timeControl.objectList={};
256 | $.each(flickerfreeScreen.screens, function(){
257 | if(this.timeoutHandler!=null) clearTimeout(this.timeoutHandler);
258 | });
259 | flickerfreeScreen.screens=[];
260 | ZBX_SBOX={};
261 |
262 | //prepare graphs ids
263 | graphs=[]
264 | if($('#graphs').val()!=null) $.each( $('#graphs').val(), function(){
265 | graphs=graphs.concat(this.split(','));
266 | });
267 | var stime = cdumpts((timeControl.timeline._usertime-timeControl.timeline._period)*1000);
268 | //prepage pager
269 | if(graphs.length > options.pagelen){
270 | if(page==undefined) page=0;
271 | pages=Math.floor(graphs.length/options.pagelen);
272 | start = page * options.pagelen;
273 | end = Math.min(start + options.pagelen, graphs.length);
274 | var s='';
275 | for(var i=0; i<=pages; i++){
276 | s+=(i==page)? ''+(i+1)+'' : ''+(i+1)+'';
277 | }
278 | if(page>0) s='< Previous'+s;
279 | if(page!=pages) s+='Next >';
280 | pager=$('').append(s);
281 | pager=$('').append(pager);
282 | pager.appendTo( $('#pics') );
283 | }
284 | else {
285 | start=0;
286 | end=graphs.length;
287 | }
288 | //add itemgraphs
289 | for (var i=0; i')
297 | .append('')
298 | .append('')
299 | );
300 | timeControl.addObject(id, {
301 | "period": timeControl.timeline._period,
302 | "starttime": cdumpts(timeControl.timeline._starttime),
303 | "usertime": cdumpts(timeControl.timeline._usertime),
304 | "isNow": timeControl.timeline._isNow
305 | },
306 | {
307 | "containerid":"itemgraph_"+id,
308 | "objDims":{
309 | "shiftYtop":35,
310 | "yaxis":"0",
311 | "graphtype":"0",
312 | "graphHeight": options.height,
313 | "shiftXleft": 65,
314 | "shiftXright": 65,
315 | "width": options.width
316 | },
317 | "loadSBox":1,
318 | "loadImage":1,
319 | "periodFixed":"1",
320 | "sliderMaximumTimePeriod": timeControl.timeline.maxperiod,
321 | "src": 'chart.php?'+uri+'type='+itemgraphs[i].type+'&batch=1&width='+options.width+'&height='+options.height+'&period='+timeControl.timeline._period+'&stime='+stime
322 | });
323 | window.flickerfreeScreen.add({
324 | "id": id,
325 | "isFlickerfree":true,
326 | "pageFile":'history.php',
327 | "resourcetype":'17',
328 | "mode":2,
329 | "interval":'60',
330 | "timeline":{
331 | "period": timeControl.timeline._period,
332 | "stime": stime,
333 | "isNow": timeControl.timeline._isNow
334 | },
335 | "data":{"itemids":[itemgraphs[i].items],"action":'showgraph',"filter":'',"filterTask":null,"markColor":1}
336 | });
337 | }
338 | //add graphs
339 | for (var i=start; i')
343 | .append('')
344 | .append('')
345 | );
346 | timeControl.addObject(id,
347 | {
348 | "period": timeControl.timeline._period,
349 | "starttime": cdumpts(timeControl.timeline._starttime),
350 | "usertime": cdumpts(timeControl.timeline._usertime),
351 | "isNow": timeControl.timeline._isNow
352 | },
353 | {
354 | "containerid":"graph_container_"+id,
355 | "objDims":{
356 | "shiftYtop":35,
357 | "yaxis":"0",
358 | "graphtype":"0",
359 | "graphHeight": options.height,
360 | "shiftXleft": 65,
361 | "shiftXright": 65,
362 | "width": options.width
363 | },
364 | "loadSBox":1,
365 | "loadImage":1,
366 | "periodFixed":"1",
367 | "sliderMaximumTimePeriod": timeControl.timeline.maxperiod,
368 | "src": "chart2.php?graphid="+id+'&width='+options.width+'&height='+options.height+'&period='+timeControl.timeline._period+'&stime='+stime
369 | });
370 | window.flickerfreeScreen.add({
371 | "id": id,
372 | "isFlickerfree":true,
373 | "pageFile":'screens.php',
374 | "resourcetype":'0',
375 | "mode":0,
376 | // "timestamp":1450022637,
377 | "interval":'60',
378 | // "screenitemid":'336',
379 | // "screenid":'34',
380 | // "groupid":null,
381 | // "hostid":0,
382 | "timeline":{
383 | "period": timeControl.timeline._period,
384 | "stime": stime,
385 | // "stimeNow":'20161212070357',
386 | // "starttime":'20131213080357',
387 | // "usertime":'20151213080357',
388 | "isNow": timeControl.timeline._isNow
389 | },
390 | // "profileIdx":'web.screens',
391 | // "profileIdx2":'34',
392 | // "updateProfile":true,
393 | "data":null
394 | });
395 | };
396 | //pager at bottom
397 | if(graphs.length > options.pagelen) {
398 | pager.clone().appendTo( $('#pics') );
399 | $('div.paging-btn-container a').click(function(){
400 | drawGraphs( $(this).data('num') );
401 | });
402 | }
403 | //attach close button event
404 | $('.close-graph').click(function(e){
405 | removeGraph(this);
406 | });
407 | //live update/select time period
408 | timeControl.useTimeRefresh(60);
409 | timeControl.processObjects();
410 | chkbxRange.init();
411 | //update url/history
412 | if(skipHistory) skipHistory=false;
413 | else{
414 | var state={hosts: $('#hosts').val(), itemgraphs: itemgraphs, graphs: graphs, page: page};
415 | history.pushState(state, '', makeUrl(state));
416 | }
417 | }
418 |
419 | // Remove graph (id)
420 | function removeGraph(o){
421 | var id=$(o).data('id');
422 | if($(o).prev('a[id^=graph_container_]').length){
423 | //deselect graph
424 | if($('#hosts').val().length==1){
425 | $('#graphs option[value='+id+']').removeAttr('selected');
426 | $('#graphs').trigger('chosen:updated');
427 | $('#graphs').trigger('change');
428 | }
429 | else{
430 | $('div#flickerfreescreen_'+id).remove();
431 | }
432 | }
433 | else{
434 | //remove itemgraph
435 | for(var i=0; i350) w=350;
448 | $('.chosen-container').css('width',w+'px');
449 | // Restore history state
450 | window.onpopstate = function(event) {
451 | if(event.state) resoreState(event.state);
452 | }
453 | // page to state
454 | function resoreState(state){
455 | if(!state['hosts']) return;
456 | console.log('restoring state');
457 | skipHistory=true;
458 | itemgraphs=state.itemgraphs;
459 | //hosts already selected, select graphs
460 | if( compare($('#hosts').val(), state.hosts) ){
461 | selectGraphs(state.graphs);
462 | $('#graphs').trigger("chosen:updated");
463 | updateHint( $('#graphs') );
464 | drawGraphs(state.page);
465 | }
466 | //hosts changed, load graphs and items
467 | else{
468 | $('#hosts option:selected').removeAttr('selected');
469 | $.each(state.hosts, function(){
470 | $('#hosts option[value="'+this+'"]').prop('selected', true);
471 | });
472 | updateHint( $('#hosts') );
473 | $('#hosts').trigger("chosen:updated");
474 | updateGraphs(state.graphs);
475 | updateItems();
476 | }
477 | };
478 | // select graphs from array
479 | function selectGraphs(graphs){
480 | $('#graphs option:selected').removeAttr('selected');
481 | $.each(graphs, function(){
482 | var o=$('#graphs option[value="'+this+'"]');
483 | if(!o.length) o=$('#graphs option[value^="'+this+',"]');
484 | if(!o.length) o=$('#graphs option[value*=",'+this+',"]');
485 | $(o).prop('selected', true);
486 | });
487 | }
488 | // make URL from state
489 | function makeUrl(state){
490 | var items='', page='';
491 | $.each(state.itemgraphs, function(){
492 | items+='&items[]='+this.type+','+this.items;
493 | })
494 | if(state.page) page='&page='+state.page;
495 | return location.pathname+'?hosts='+state.hosts+'&graphs='+state.graphs+items+page;
496 | }
497 | // read state from URL
498 | function readUrl(){
499 | var match,
500 | search = /([^&=]+)=?([^&]*)/g,
501 | decode = function(s) { return decodeURIComponent(s); },
502 | query = window.location.search.substring(1),
503 | state = {itemgraphs:[]};
504 |
505 | while (match = search.exec(query)){
506 | if(match[1]=='items[]'){
507 | var item={items: decode(match[2]).split(','), id: Math.random().toString(16).slice(2)};
508 | item['type']=item['items'][0];
509 | item['items'].splice(0,1);
510 | state['itemgraphs'].push(item);
511 | }
512 | else if(match[1]=='page') state['page']=match[2];
513 | else state[decode(match[1])] = decode(match[2]).split(',');
514 | }
515 | return state;
516 | }
517 | });
518 |
519 | // helpers ------------------------------------------------------------------
520 | // fix for JSON.stringify arrays
521 | if(window.Prototype) {delete Array.prototype.toJSON;}
522 | // parse cookie names
523 | function getCookie(key) {
524 | var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
525 | return keyValue ? keyValue[2] : null;
526 | }
527 | function compare(arrayA, arrayB) {
528 | if(!arrayA || !arrayB) return false;
529 | if(arrayA.length != arrayB.length) return false;
530 | var a = jQuery.extend(true, [], arrayA);
531 | var b = jQuery.extend(true, [], arrayB);
532 | a.sort();
533 | b.sort();
534 | for (var i = 0, l = a.length; i < l; i++) {
535 | if (a[i] !== b[i]) {
536 | return false;
537 | }
538 | }
539 | return true;
540 | }
--------------------------------------------------------------------------------
/grapher/chosen.jquery.js:
--------------------------------------------------------------------------------
1 | /*!
2 | Please note that it is modified ChosenJS, do not replace it with newer version or you will loose:
3 | - leave results open while focused
4 | - do not reset search query on select
5 | - deselect by click on selected item
6 |
7 | -----
8 | Chosen, a Select Box Enhancer for jQuery and Prototype
9 | by Patrick Filler for Harvest, http://getharvest.com
10 |
11 | Version 1.4.2
12 | Full source at https://github.com/harvesthq/chosen
13 | Copyright (c) 2011-2015 Harvest http://getharvest.com
14 |
15 | MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
16 | This file is generated by `grunt build`, do not edit it by hand.
17 | */
18 |
19 | (function() {
20 | var $, AbstractChosen, Chosen, SelectParser, _ref,
21 | __hasProp = {}.hasOwnProperty,
22 | __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
23 |
24 | SelectParser = (function() {
25 | function SelectParser() {
26 | this.options_index = 0;
27 | this.parsed = [];
28 | }
29 |
30 | SelectParser.prototype.add_node = function(child) {
31 | if (child.nodeName.toUpperCase() === "OPTGROUP") {
32 | return this.add_group(child);
33 | } else {
34 | return this.add_option(child);
35 | }
36 | };
37 |
38 | SelectParser.prototype.add_group = function(group) {
39 | var group_position, option, _i, _len, _ref, _results;
40 | group_position = this.parsed.length;
41 | this.parsed.push({
42 | array_index: group_position,
43 | group: true,
44 | label: this.escapeExpression(group.label),
45 | title: group.title ? group.title : void 0,
46 | children: 0,
47 | disabled: group.disabled,
48 | classes: group.className
49 | });
50 | _ref = group.childNodes;
51 | _results = [];
52 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
53 | option = _ref[_i];
54 | _results.push(this.add_option(option, group_position, group.disabled));
55 | }
56 | return _results;
57 | };
58 |
59 | SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
60 | if (option.nodeName.toUpperCase() === "OPTION") {
61 | if (option.text !== "") {
62 | if (group_position != null) {
63 | this.parsed[group_position].children += 1;
64 | }
65 | this.parsed.push({
66 | array_index: this.parsed.length,
67 | options_index: this.options_index,
68 | value: option.value,
69 | text: option.text,
70 | html: option.innerHTML,
71 | title: option.title ? option.title : void 0,
72 | selected: option.selected,
73 | disabled: group_disabled === true ? group_disabled : option.disabled,
74 | group_array_index: group_position,
75 | group_label: group_position != null ? this.parsed[group_position].label : null,
76 | classes: option.className,
77 | style: option.style.cssText
78 | });
79 | } else {
80 | this.parsed.push({
81 | array_index: this.parsed.length,
82 | options_index: this.options_index,
83 | empty: true
84 | });
85 | }
86 | return this.options_index += 1;
87 | }
88 | };
89 |
90 | SelectParser.prototype.escapeExpression = function(text) {
91 | var map, unsafe_chars;
92 | if ((text == null) || text === false) {
93 | return "";
94 | }
95 | if (!/[\&\<\>\"\'\`]/.test(text)) {
96 | return text;
97 | }
98 | map = {
99 | "<": "<",
100 | ">": ">",
101 | '"': """,
102 | "'": "'",
103 | "`": "`"
104 | };
105 | unsafe_chars = /&(?!\w+;)|[\<\>\"\'\`]/g;
106 | return text.replace(unsafe_chars, function(chr) {
107 | return map[chr] || "&";
108 | });
109 | };
110 |
111 | return SelectParser;
112 |
113 | })();
114 |
115 | SelectParser.select_to_array = function(select) {
116 | var child, parser, _i, _len, _ref;
117 | parser = new SelectParser();
118 | _ref = select.childNodes;
119 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
120 | child = _ref[_i];
121 | parser.add_node(child);
122 | }
123 | return parser.parsed;
124 | };
125 |
126 | AbstractChosen = (function() {
127 | function AbstractChosen(form_field, options) {
128 | this.form_field = form_field;
129 | this.options = options != null ? options : {};
130 | if (!AbstractChosen.browser_is_supported()) {
131 | return;
132 | }
133 | this.is_multiple = this.form_field.multiple;
134 | this.set_default_text();
135 | this.set_default_values();
136 | this.setup();
137 | this.set_up_html();
138 | this.register_observers();
139 | this.on_ready();
140 | }
141 |
142 | AbstractChosen.prototype.set_default_values = function() {
143 | var _this = this;
144 | this.click_test_action = function(evt) {
145 | return _this.test_active_click(evt);
146 | };
147 | this.activate_action = function(evt) {
148 | return _this.activate_field(evt);
149 | };
150 | this.active_field = false;
151 | this.mouse_on_container = false;
152 | this.results_showing = false;
153 | this.result_highlighted = null;
154 | this.allow_single_deselect = (this.options.allow_single_deselect != null) && (this.form_field.options[0] != null) && this.form_field.options[0].text === "" ? this.options.allow_single_deselect : false;
155 | this.disable_search_threshold = this.options.disable_search_threshold || 0;
156 | this.disable_search = this.options.disable_search || false;
157 | this.enable_split_word_search = this.options.enable_split_word_search != null ? this.options.enable_split_word_search : true;
158 | this.group_search = this.options.group_search != null ? this.options.group_search : true;
159 | this.search_contains = this.options.search_contains || false;
160 | this.single_backstroke_delete = this.options.single_backstroke_delete != null ? this.options.single_backstroke_delete : true;
161 | this.max_selected_options = this.options.max_selected_options || Infinity;
162 | this.inherit_select_classes = this.options.inherit_select_classes || false;
163 | this.display_selected_options = this.options.display_selected_options != null ? this.options.display_selected_options : true;
164 | this.display_disabled_options = this.options.display_disabled_options != null ? this.options.display_disabled_options : true;
165 | return this.include_group_label_in_selected = this.options.include_group_label_in_selected || false;
166 | };
167 |
168 | AbstractChosen.prototype.set_default_text = function() {
169 | if (this.form_field.getAttribute("data-placeholder")) {
170 | this.default_text = this.form_field.getAttribute("data-placeholder");
171 | } else if (this.is_multiple) {
172 | this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || AbstractChosen.default_multiple_text;
173 | } else {
174 | this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text;
175 | }
176 | return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text;
177 | };
178 |
179 | AbstractChosen.prototype.choice_label = function(item) {
180 | if (this.include_group_label_in_selected && (item.group_label != null)) {
181 | return "" + item.group_label + "" + item.html;
182 | } else {
183 | return item.html;
184 | }
185 | };
186 |
187 | AbstractChosen.prototype.mouse_enter = function() {
188 | return this.mouse_on_container = true;
189 | };
190 |
191 | AbstractChosen.prototype.mouse_leave = function() {
192 | return this.mouse_on_container = false;
193 | };
194 |
195 | AbstractChosen.prototype.input_focus = function(evt) {
196 | var _this = this;
197 | if (this.is_multiple) {
198 | if (!this.active_field) {
199 | return setTimeout((function() {
200 | return _this.container_mousedown();
201 | }), 50);
202 | }
203 | } else {
204 | if (!this.active_field) {
205 | return this.activate_field();
206 | }
207 | }
208 | };
209 |
210 | AbstractChosen.prototype.input_blur = function(evt) {
211 | var _this = this;
212 | if (!this.mouse_on_container) {
213 | this.active_field = false;
214 | return setTimeout((function() {
215 | return _this.blur_test();
216 | }), 100);
217 | }
218 | };
219 |
220 | AbstractChosen.prototype.results_option_build = function(options) {
221 | var content, data, _i, _len, _ref;
222 | content = '';
223 | _ref = this.results_data;
224 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
225 | data = _ref[_i];
226 | if (data.group) {
227 | content += this.result_add_group(data);
228 | } else {
229 | content += this.result_add_option(data);
230 | }
231 | if (options != null ? options.first : void 0) {
232 | if (data.selected && this.is_multiple) {
233 | this.choice_build(data);
234 | } else if (data.selected && !this.is_multiple) {
235 | this.single_set_selected_text(this.choice_label(data));
236 | }
237 | }
238 | }
239 | return content;
240 | };
241 |
242 | AbstractChosen.prototype.result_add_option = function(option) {
243 | var classes, option_el;
244 | if (!option.search_match) {
245 | return '';
246 | }
247 | if (!this.include_option_in_results(option)) {
248 | return '';
249 | }
250 | classes = [];
251 | if (!option.disabled && !(option.selected && this.is_multiple)) {
252 | classes.push("active-result");
253 | }
254 | if (option.disabled && !(option.selected && this.is_multiple)) {
255 | classes.push("disabled-result");
256 | }
257 | if (option.selected) {
258 | classes.push("result-selected");
259 | }
260 | if (option.group_array_index != null) {
261 | classes.push("group-option");
262 | }
263 | if (option.classes !== "") {
264 | classes.push(option.classes);
265 | }
266 | option_el = document.createElement("li");
267 | option_el.className = classes.join(" ");
268 | option_el.style.cssText = option.style;
269 | option_el.setAttribute("data-option-array-index", option.array_index);
270 | option_el.innerHTML = option.search_text;
271 | if (option.title) {
272 | option_el.title = option.title;
273 | }
274 | return this.outerHTML(option_el);
275 | };
276 |
277 | AbstractChosen.prototype.result_add_group = function(group) {
278 | var classes, group_el;
279 | if (!(group.search_match || group.group_match)) {
280 | return '';
281 | }
282 | if (!(group.active_options > 0)) {
283 | return '';
284 | }
285 | classes = [];
286 | classes.push("group-result");
287 | if (group.classes) {
288 | classes.push(group.classes);
289 | }
290 | group_el = document.createElement("li");
291 | group_el.className = classes.join(" ");
292 | group_el.innerHTML = group.search_text;
293 | if (group.title) {
294 | group_el.title = group.title;
295 | }
296 | return this.outerHTML(group_el);
297 | };
298 |
299 | AbstractChosen.prototype.results_update_field = function() {
300 | this.set_default_text();
301 | if (!this.is_multiple) {
302 | this.results_reset_cleanup();
303 | }
304 | this.result_clear_highlight();
305 | this.results_build();
306 | if (this.results_showing) {
307 | return this.winnow_results();
308 | }
309 | };
310 |
311 | AbstractChosen.prototype.reset_single_select_options = function() {
312 | var result, _i, _len, _ref, _results;
313 | _ref = this.results_data;
314 | _results = [];
315 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
316 | result = _ref[_i];
317 | if (result.selected) {
318 | _results.push(result.selected = false);
319 | } else {
320 | _results.push(void 0);
321 | }
322 | }
323 | return _results;
324 | };
325 |
326 | AbstractChosen.prototype.results_toggle = function() {
327 | if (this.results_showing) {
328 | return this.results_hide();
329 | } else {
330 | return this.results_show();
331 | }
332 | };
333 |
334 | AbstractChosen.prototype.results_search = function(evt) {
335 | if (this.results_showing) {
336 | return this.winnow_results();
337 | } else {
338 | return this.results_show();
339 | }
340 | };
341 |
342 | AbstractChosen.prototype.winnow_results = function() {
343 | var escapedSearchText, option, regex, results, results_group, searchText, startpos, text, zregex, _i, _len, _ref;
344 | this.no_results_clear();
345 | results = 0;
346 | searchText = this.get_search_text();
347 | escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
348 | zregex = new RegExp(escapedSearchText, 'i');
349 | regex = this.get_search_regex(escapedSearchText);
350 | _ref = this.results_data;
351 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
352 | option = _ref[_i];
353 | option.search_match = false;
354 | results_group = null;
355 | if (this.include_option_in_results(option)) {
356 | if (option.group) {
357 | option.group_match = false;
358 | option.active_options = 0;
359 | }
360 | if ((option.group_array_index != null) && this.results_data[option.group_array_index]) {
361 | results_group = this.results_data[option.group_array_index];
362 | if (results_group.active_options === 0 && results_group.search_match) {
363 | results += 1;
364 | }
365 | results_group.active_options += 1;
366 | }
367 | option.search_text = option.group ? option.label : option.html;
368 | if (!(option.group && !this.group_search)) {
369 | option.search_match = this.search_string_match(option.search_text, regex);
370 | if (option.search_match && !option.group) {
371 | results += 1;
372 | }
373 | if (option.search_match) {
374 | if (searchText.length) {
375 | startpos = option.search_text.search(zregex);
376 | text = option.search_text.substr(0, startpos + searchText.length) + '' + option.search_text.substr(startpos + searchText.length);
377 | option.search_text = text.substr(0, startpos) + '' + text.substr(startpos);
378 | }
379 | if (results_group != null) {
380 | results_group.group_match = true;
381 | }
382 | } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
383 | option.search_match = true;
384 | }
385 | }
386 | }
387 | }
388 | this.result_clear_highlight();
389 | if (results < 1 && searchText.length) {
390 | this.update_results_content("");
391 | return this.no_results(searchText);
392 | } else {
393 | this.update_results_content(this.results_option_build());
394 | return this.winnow_results_set_highlight();
395 | }
396 | };
397 |
398 | AbstractChosen.prototype.get_search_regex = function(escaped_search_string) {
399 | var regex_anchor;
400 | regex_anchor = this.search_contains ? "" : "^";
401 | return new RegExp(regex_anchor + escaped_search_string, 'i');
402 | };
403 |
404 | AbstractChosen.prototype.search_string_match = function(search_string, regex) {
405 | var part, parts, _i, _len;
406 | if (regex.test(search_string)) {
407 | return true;
408 | } else if (this.enable_split_word_search && (search_string.indexOf(" ") >= 0 || search_string.indexOf("[") === 0)) {
409 | parts = search_string.replace(/\[|\]/g, "").split(" ");
410 | if (parts.length) {
411 | for (_i = 0, _len = parts.length; _i < _len; _i++) {
412 | part = parts[_i];
413 | if (regex.test(part)) {
414 | return true;
415 | }
416 | }
417 | }
418 | }
419 | };
420 |
421 | AbstractChosen.prototype.choices_count = function() {
422 | var option, _i, _len, _ref;
423 | if (this.selected_option_count != null) {
424 | return this.selected_option_count;
425 | }
426 | this.selected_option_count = 0;
427 | _ref = this.form_field.options;
428 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
429 | option = _ref[_i];
430 | if (option.selected) {
431 | this.selected_option_count += 1;
432 | }
433 | }
434 | return this.selected_option_count;
435 | };
436 |
437 | AbstractChosen.prototype.choices_click = function(evt) {
438 | evt.preventDefault();
439 | if (!(this.results_showing || this.is_disabled)) {
440 | return this.results_show();
441 | }
442 | };
443 |
444 | AbstractChosen.prototype.keyup_checker = function(evt) {
445 | var stroke, _ref;
446 | stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
447 | this.search_field_scale();
448 | switch (stroke) {
449 | case 8:
450 | if (this.is_multiple && this.backstroke_length < 1 && this.choices_count() > 0) {
451 | return this.keydown_backstroke();
452 | } else if (!this.pending_backstroke) {
453 | this.result_clear_highlight();
454 | return this.results_search();
455 | }
456 | break;
457 | case 13:
458 | evt.preventDefault();
459 | if (this.results_showing) {
460 | return this.result_select(evt);
461 | }
462 | break;
463 | case 27:
464 | if (this.results_showing) {
465 | this.results_hide();
466 | }
467 | return true;
468 | case 9:
469 | case 38:
470 | case 40:
471 | case 16:
472 | case 91:
473 | case 17:
474 | break;
475 | default:
476 | return this.results_search();
477 | }
478 | };
479 |
480 | AbstractChosen.prototype.clipboard_event_checker = function(evt) {
481 | var _this = this;
482 | return setTimeout((function() {
483 | return _this.results_search();
484 | }), 50);
485 | };
486 |
487 | AbstractChosen.prototype.container_width = function() {
488 | if (this.options.width != null) {
489 | return this.options.width;
490 | } else {
491 | return "" + this.form_field.offsetWidth + "px";
492 | }
493 | };
494 |
495 | AbstractChosen.prototype.include_option_in_results = function(option) {
496 | if (this.is_multiple && (!this.display_selected_options && option.selected)) {
497 | return false;
498 | }
499 | if (!this.display_disabled_options && option.disabled) {
500 | return false;
501 | }
502 | if (option.empty) {
503 | return false;
504 | }
505 | return true;
506 | };
507 |
508 | AbstractChosen.prototype.search_results_touchstart = function(evt) {
509 | this.touch_started = true;
510 | return this.search_results_mouseover(evt);
511 | };
512 |
513 | AbstractChosen.prototype.search_results_touchmove = function(evt) {
514 | this.touch_started = false;
515 | return this.search_results_mouseout(evt);
516 | };
517 |
518 | AbstractChosen.prototype.search_results_touchend = function(evt) {
519 | if (this.touch_started) {
520 | return this.search_results_mouseup(evt);
521 | }
522 | };
523 |
524 | AbstractChosen.prototype.outerHTML = function(element) {
525 | var tmp;
526 | if (element.outerHTML) {
527 | return element.outerHTML;
528 | }
529 | tmp = document.createElement("div");
530 | tmp.appendChild(element);
531 | return tmp.innerHTML;
532 | };
533 |
534 | AbstractChosen.browser_is_supported = function() {
535 | if (window.navigator.appName === "Microsoft Internet Explorer") {
536 | return document.documentMode >= 8;
537 | }
538 | if (/iP(od|hone)/i.test(window.navigator.userAgent)) {
539 | return false;
540 | }
541 | if (/Android/i.test(window.navigator.userAgent)) {
542 | if (/Mobile/i.test(window.navigator.userAgent)) {
543 | return false;
544 | }
545 | }
546 | return true;
547 | };
548 |
549 | AbstractChosen.default_multiple_text = "Select Some Options";
550 |
551 | AbstractChosen.default_single_text = "Select an Option";
552 |
553 | AbstractChosen.default_no_result_text = "No results match";
554 |
555 | return AbstractChosen;
556 |
557 | })();
558 |
559 | $ = jQuery;
560 |
561 | $.fn.extend({
562 | chosen: function(options) {
563 | if (!AbstractChosen.browser_is_supported()) {
564 | return this;
565 | }
566 | return this.each(function(input_field) {
567 | var $this, chosen;
568 | $this = $(this);
569 | chosen = $this.data('chosen');
570 | if (options === 'destroy' && chosen instanceof Chosen) {
571 | chosen.destroy();
572 | } else if (!(chosen instanceof Chosen)) {
573 | $this.data('chosen', new Chosen(this, options));
574 | }
575 | });
576 | }
577 | });
578 |
579 | Chosen = (function(_super) {
580 | __extends(Chosen, _super);
581 |
582 | function Chosen() {
583 | _ref = Chosen.__super__.constructor.apply(this, arguments);
584 | return _ref;
585 | }
586 |
587 | Chosen.prototype.setup = function() {
588 | this.form_field_jq = $(this.form_field);
589 | this.current_selectedIndex = this.form_field.selectedIndex;
590 | return this.is_rtl = this.form_field_jq.hasClass("chosen-rtl");
591 | };
592 |
593 | Chosen.prototype.set_up_html = function() {
594 | var container_classes, container_props;
595 | container_classes = ["chosen-container"];
596 | container_classes.push("chosen-container-" + (this.is_multiple ? "multi" : "single"));
597 | if (this.inherit_select_classes && this.form_field.className) {
598 | container_classes.push(this.form_field.className);
599 | }
600 | if (this.is_rtl) {
601 | container_classes.push("chosen-rtl");
602 | }
603 | container_props = {
604 | 'class': container_classes.join(' '),
605 | 'style': "width: " + (this.container_width()) + ";",
606 | 'title': this.form_field.title
607 | };
608 | if (this.form_field.id.length) {
609 | container_props.id = this.form_field.id.replace(/[^\w]/g, '_') + "_chosen";
610 | }
611 | this.container = $("", container_props);
612 | if (this.is_multiple) {
613 | this.container.html('');
614 | } else {
615 | this.container.html('' + this.default_text + '
');
616 | }
617 | this.form_field_jq.hide().after(this.container);
618 | this.dropdown = this.container.find('div.chosen-drop').first();
619 | this.search_field = this.container.find('input').first();
620 | this.search_results = this.container.find('ul.chosen-results').first();
621 | this.search_field_scale();
622 | this.search_no_results = this.container.find('li.no-results').first();
623 | if (this.is_multiple) {
624 | this.search_choices = this.container.find('ul.chosen-choices').first();
625 | this.search_container = this.container.find('li.search-field').first();
626 | } else {
627 | this.search_container = this.container.find('div.chosen-search').first();
628 | this.selected_item = this.container.find('.chosen-single').first();
629 | }
630 | this.results_build();
631 | this.set_tab_index();
632 | return this.set_label_behavior();
633 | };
634 |
635 | Chosen.prototype.on_ready = function() {
636 | return this.form_field_jq.trigger("chosen:ready", {
637 | chosen: this
638 | });
639 | };
640 |
641 | Chosen.prototype.register_observers = function() {
642 | var _this = this;
643 | this.container.bind('touchstart.chosen', function(evt) {
644 | _this.container_mousedown(evt);
645 | return evt.preventDefault();
646 | });
647 | this.container.bind('touchend.chosen', function(evt) {
648 | _this.container_mouseup(evt);
649 | return evt.preventDefault();
650 | });
651 | this.container.bind('mousedown.chosen', function(evt) {
652 | _this.container_mousedown(evt);
653 | });
654 | this.container.bind('mouseup.chosen', function(evt) {
655 | _this.container_mouseup(evt);
656 | });
657 | this.container.bind('mouseenter.chosen', function(evt) {
658 | _this.mouse_enter(evt);
659 | });
660 | this.container.bind('mouseleave.chosen', function(evt) {
661 | _this.mouse_leave(evt);
662 | });
663 | this.search_results.bind('mouseup.chosen', function(evt) {
664 | _this.search_results_mouseup(evt);
665 | });
666 | this.search_results.bind('mouseover.chosen', function(evt) {
667 | _this.search_results_mouseover(evt);
668 | });
669 | this.search_results.bind('mouseout.chosen', function(evt) {
670 | _this.search_results_mouseout(evt);
671 | });
672 | this.search_results.bind('mousewheel.chosen DOMMouseScroll.chosen', function(evt) {
673 | _this.search_results_mousewheel(evt);
674 | });
675 | this.search_results.bind('touchstart.chosen', function(evt) {
676 | _this.search_results_touchstart(evt);
677 | });
678 | this.search_results.bind('touchmove.chosen', function(evt) {
679 | _this.search_results_touchmove(evt);
680 | });
681 | this.search_results.bind('touchend.chosen', function(evt) {
682 | _this.search_results_touchend(evt);
683 | });
684 | this.form_field_jq.bind("chosen:updated.chosen", function(evt) {
685 | _this.results_update_field(evt);
686 | });
687 | this.form_field_jq.bind("chosen:activate.chosen", function(evt) {
688 | _this.activate_field(evt);
689 | });
690 | this.form_field_jq.bind("chosen:open.chosen", function(evt) {
691 | _this.container_mousedown(evt);
692 | });
693 | this.form_field_jq.bind("chosen:close.chosen", function(evt) {
694 | _this.input_blur(evt);
695 | });
696 | this.search_field.bind('blur.chosen', function(evt) {
697 | _this.input_blur(evt);
698 | });
699 | this.search_field.bind('keyup.chosen', function(evt) {
700 | _this.keyup_checker(evt);
701 | });
702 | this.search_field.bind('keydown.chosen', function(evt) {
703 | _this.keydown_checker(evt);
704 | });
705 | this.search_field.bind('focus.chosen', function(evt) {
706 | _this.input_focus(evt);
707 | });
708 | this.search_field.bind('cut.chosen', function(evt) {
709 | _this.clipboard_event_checker(evt);
710 | });
711 | this.search_field.bind('paste.chosen', function(evt) {
712 | _this.clipboard_event_checker(evt);
713 | });
714 | if (this.is_multiple) {
715 | return this.search_choices.bind('click.chosen', function(evt) {
716 | _this.choices_click(evt);
717 | });
718 | } else {
719 | return this.container.bind('click.chosen', function(evt) {
720 | evt.preventDefault();
721 | });
722 | }
723 | };
724 |
725 | Chosen.prototype.destroy = function() {
726 | $(this.container[0].ownerDocument).unbind("click.chosen", this.click_test_action);
727 | if (this.search_field[0].tabIndex) {
728 | this.form_field_jq[0].tabIndex = this.search_field[0].tabIndex;
729 | }
730 | this.container.remove();
731 | this.form_field_jq.removeData('chosen');
732 | return this.form_field_jq.show();
733 | };
734 |
735 | Chosen.prototype.search_field_disabled = function() {
736 | this.is_disabled = this.form_field_jq[0].disabled;
737 | if (this.is_disabled) {
738 | this.container.addClass('chosen-disabled');
739 | this.search_field[0].disabled = true;
740 | if (!this.is_multiple) {
741 | this.selected_item.unbind("focus.chosen", this.activate_action);
742 | }
743 | return this.close_field();
744 | } else {
745 | this.container.removeClass('chosen-disabled');
746 | this.search_field[0].disabled = false;
747 | if (!this.is_multiple) {
748 | return this.selected_item.bind("focus.chosen", this.activate_action);
749 | }
750 | }
751 | };
752 |
753 | Chosen.prototype.container_mousedown = function(evt) {
754 | if (!this.is_disabled) {
755 | if (evt && evt.type === "mousedown" && !this.results_showing) {
756 | evt.preventDefault();
757 | }
758 | if (!((evt != null) && ($(evt.target)).hasClass("search-choice-close"))) {
759 | if (!this.active_field) {
760 | if (this.is_multiple && this.search_field.val()==this.default_text) {
761 | this.search_field.val("");
762 | }
763 | $(this.container[0].ownerDocument).bind('click.chosen', this.click_test_action);
764 | this.results_show();
765 | } else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chosen-single").length)) {
766 | evt.preventDefault();
767 | this.results_toggle();
768 | }
769 | return this.activate_field();
770 | }
771 | }
772 | };
773 |
774 | Chosen.prototype.container_mouseup = function(evt) {
775 | if (evt.target.nodeName === "ABBR" && !this.is_disabled) {
776 | return this.results_reset(evt);
777 | }
778 | };
779 |
780 | Chosen.prototype.search_results_mousewheel = function(evt) {
781 | var delta;
782 | if (evt.originalEvent) {
783 | delta = evt.originalEvent.deltaY || -evt.originalEvent.wheelDelta || evt.originalEvent.detail;
784 | }
785 | if (delta != null) {
786 | evt.preventDefault();
787 | if (evt.type === 'DOMMouseScroll') {
788 | delta = delta * 40;
789 | }
790 | return this.search_results.scrollTop(delta + this.search_results.scrollTop());
791 | }
792 | };
793 |
794 | Chosen.prototype.blur_test = function(evt) {
795 | if (!this.active_field && this.container.hasClass("chosen-container-active")) {
796 | return this.close_field();
797 | }
798 | };
799 |
800 | Chosen.prototype.close_field = function() {
801 | $(this.container[0].ownerDocument).unbind("click.chosen", this.click_test_action);
802 | this.active_field = false;
803 | this.results_hide();
804 | this.container.removeClass("chosen-container-active");
805 | this.clear_backstroke();
806 | this.show_search_field_default();
807 | return this.search_field_scale();
808 | };
809 |
810 | Chosen.prototype.activate_field = function() {
811 | this.container.addClass("chosen-container-active");
812 | this.active_field = true;
813 | this.search_field.val(this.search_field.val());
814 | return this.search_field.focus();
815 | };
816 |
817 | Chosen.prototype.test_active_click = function(evt) {
818 | var active_container;
819 | active_container = $(evt.target).closest('.chosen-container');
820 | if (active_container.length && this.container[0] === active_container[0]) {
821 | return this.active_field = true;
822 | } else {
823 | return this.close_field();
824 | }
825 | };
826 |
827 | Chosen.prototype.results_build = function() {
828 | this.parsing = true;
829 | this.selected_option_count = null;
830 | this.results_data = SelectParser.select_to_array(this.form_field);
831 | if (this.is_multiple) {
832 | this.search_choices.find("li.search-choice").remove();
833 | } else if (!this.is_multiple) {
834 | this.single_set_selected_text();
835 | if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
836 | this.search_field[0].readOnly = true;
837 | this.container.addClass("chosen-container-single-nosearch");
838 | } else {
839 | this.search_field[0].readOnly = false;
840 | this.container.removeClass("chosen-container-single-nosearch");
841 | }
842 | }
843 | this.update_results_content(this.results_option_build({
844 | first: true
845 | }));
846 | this.search_field_disabled();
847 | this.show_search_field_default();
848 | this.search_field_scale();
849 | return this.parsing = false;
850 | };
851 |
852 | Chosen.prototype.result_do_highlight = function(el) {
853 | var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
854 | if (el.length) {
855 | this.result_clear_highlight();
856 | this.result_highlight = el;
857 | this.result_highlight.addClass("highlighted");
858 | maxHeight = parseInt(this.search_results.css("maxHeight"), 10);
859 | visible_top = this.search_results.scrollTop();
860 | visible_bottom = maxHeight + visible_top;
861 | high_top = this.result_highlight.position().top + this.search_results.scrollTop();
862 | high_bottom = high_top + this.result_highlight.outerHeight();
863 | if (high_bottom >= visible_bottom) {
864 | return this.search_results.scrollTop((high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0);
865 | } else if (high_top < visible_top) {
866 | return this.search_results.scrollTop(high_top);
867 | }
868 | }
869 | };
870 |
871 | Chosen.prototype.result_clear_highlight = function() {
872 | if (this.result_highlight) {
873 | this.result_highlight.removeClass("highlighted");
874 | }
875 | return this.result_highlight = null;
876 | };
877 |
878 | Chosen.prototype.results_show = function() {
879 | if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
880 | this.form_field_jq.trigger("chosen:maxselected", {
881 | chosen: this
882 | });
883 | return false;
884 | }
885 | this.container.addClass("chosen-with-drop");
886 | this.results_showing = true;
887 | this.search_field.focus();
888 | this.search_field.val(this.search_field.val());
889 | this.winnow_results();
890 | return this.form_field_jq.trigger("chosen:showing_dropdown", {
891 | chosen: this
892 | });
893 | };
894 |
895 | Chosen.prototype.update_results_content = function(content) {
896 | return this.search_results.html(content);
897 | };
898 |
899 | Chosen.prototype.results_hide = function() {
900 | if (this.results_showing) {
901 | this.result_clear_highlight();
902 | this.container.removeClass("chosen-with-drop");
903 | this.form_field_jq.trigger("chosen:hiding_dropdown", {
904 | chosen: this
905 | });
906 | }
907 | return this.results_showing = false;
908 | };
909 |
910 | Chosen.prototype.set_tab_index = function(el) {
911 | var ti;
912 | if (this.form_field.tabIndex) {
913 | ti = this.form_field.tabIndex;
914 | this.form_field.tabIndex = -1;
915 | return this.search_field[0].tabIndex = ti;
916 | }
917 | };
918 |
919 | Chosen.prototype.set_label_behavior = function() {
920 | var _this = this;
921 | this.form_field_label = this.form_field_jq.parents("label");
922 | if (!this.form_field_label.length && this.form_field.id.length) {
923 | this.form_field_label = $("label[for='" + this.form_field.id + "']");
924 | }
925 | if (this.form_field_label.length > 0) {
926 | return this.form_field_label.bind('click.chosen', function(evt) {
927 | if (_this.is_multiple) {
928 | return _this.container_mousedown(evt);
929 | } else {
930 | return _this.activate_field();
931 | }
932 | });
933 | }
934 | };
935 |
936 | Chosen.prototype.show_search_field_default = function() {
937 | if (this.is_multiple && this.choices_count() < 1 && !this.active_field) {
938 | if(this.search_field.val()=='') this.search_field.val(this.default_text);
939 | return this.search_field.addClass("default");
940 | } else {
941 | if(this.search_field.val()==this.default_text) this.search_field.val("");
942 | return this.search_field.removeClass("default");
943 | }
944 | };
945 |
946 | Chosen.prototype.search_results_mouseup = function(evt) {
947 | var target;
948 | target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
949 | if (target.length) {
950 | this.result_highlight = target;
951 | this.result_select(evt);
952 | return this.search_field.focus();
953 | }
954 | //deselect
955 | target = $(evt.target).hasClass("result-selected") ? $(evt.target) : $(evt.target).parents(".result-selected").first();
956 | if( target.length ){
957 | this.result_highlight = target;
958 | var i=target[0].getAttribute("data-option-array-index");
959 | $('ul.chosen-choices a.search-choice-close[data-option-array-index='+i+']').parent('li').remove();
960 | evt.preventDefault();
961 | return this.result_deselect(i);
962 | }
963 | };
964 |
965 | Chosen.prototype.search_results_mouseover = function(evt) {
966 | var target;
967 | target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
968 | if (target) {
969 | return this.result_do_highlight(target);
970 | }
971 | };
972 |
973 | Chosen.prototype.search_results_mouseout = function(evt) {
974 | if ($(evt.target).hasClass("active-result" || $(evt.target).parents('.active-result').first())) {
975 | return this.result_clear_highlight();
976 | }
977 | };
978 |
979 | Chosen.prototype.choice_build = function(item) {
980 | var choice, close_link,
981 | _this = this;
982 | choice = $('', {
983 | "class": "search-choice"
984 | }).html("" + (this.choice_label(item)) + "");
985 | if (item.disabled) {
986 | choice.addClass('search-choice-disabled');
987 | } else {
988 | close_link = $('', {
989 | "class": 'search-choice-close',
990 | 'data-option-array-index': item.array_index
991 | });
992 | close_link.bind('click.chosen', function(evt) {
993 | return _this.choice_destroy_link_click(evt);
994 | });
995 | choice.append(close_link);
996 | }
997 | return this.search_container.before(choice);
998 | };
999 |
1000 | Chosen.prototype.choice_destroy_link_click = function(evt) {
1001 | evt.preventDefault();
1002 | evt.stopPropagation();
1003 | if (!this.is_disabled) {
1004 | return this.choice_destroy($(evt.target));
1005 | }
1006 | };
1007 |
1008 | Chosen.prototype.choice_destroy = function(link) {
1009 | if (this.result_deselect(link[0].getAttribute("data-option-array-index"))) {
1010 | this.show_search_field_default();
1011 | if (this.is_multiple && this.choices_count() > 0 && this.search_field.val().length < 1) {
1012 | this.results_hide();
1013 | }
1014 | link.parents('li').first().remove();
1015 | return this.search_field_scale();
1016 | }
1017 | };
1018 |
1019 | Chosen.prototype.results_reset = function() {
1020 | this.reset_single_select_options();
1021 | this.form_field.options[0].selected = true;
1022 | this.single_set_selected_text();
1023 | this.show_search_field_default();
1024 | this.results_reset_cleanup();
1025 | this.form_field_jq.trigger("change");
1026 | if (this.active_field) {
1027 | return this.results_hide();
1028 | }
1029 | };
1030 |
1031 | Chosen.prototype.results_reset_cleanup = function() {
1032 | this.current_selectedIndex = this.form_field.selectedIndex;
1033 | return this.selected_item.find("abbr").remove();
1034 | };
1035 |
1036 | Chosen.prototype.result_select = function(evt) {
1037 | var high, item;
1038 | if (this.result_highlight) {
1039 | high = this.result_highlight;
1040 | this.result_clear_highlight();
1041 | if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
1042 | this.form_field_jq.trigger("chosen:maxselected", {
1043 | chosen: this
1044 | });
1045 | return false;
1046 | }
1047 | if (this.is_multiple) {
1048 | high.removeClass("active-result");
1049 | } else {
1050 | this.reset_single_select_options();
1051 | }
1052 | high.addClass("result-selected");
1053 | item = this.results_data[high[0].getAttribute("data-option-array-index")];
1054 | item.selected = true;
1055 | this.form_field.options[item.options_index].selected = true;
1056 | this.selected_option_count = null;
1057 | if (this.is_multiple) {
1058 | this.choice_build(item);
1059 | } else {
1060 | this.single_set_selected_text(this.choice_label(item));
1061 | }
1062 | if (!this.is_multiple) {
1063 | this.results_hide();
1064 | this.search_field.val("");
1065 | }
1066 | if (this.is_multiple || this.form_field.selectedIndex !== this.current_selectedIndex) {
1067 | this.form_field_jq.trigger("change", {
1068 | 'selected': this.form_field.options[item.options_index].value
1069 | });
1070 | }
1071 | this.current_selectedIndex = this.form_field.selectedIndex;
1072 | evt.preventDefault();
1073 | return this.search_field_scale();
1074 | }
1075 | };
1076 |
1077 | Chosen.prototype.single_set_selected_text = function(text) {
1078 | if (text == null) {
1079 | text = this.default_text;
1080 | }
1081 | if (text === this.default_text) {
1082 | this.selected_item.addClass("chosen-default");
1083 | } else {
1084 | this.single_deselect_control_build();
1085 | this.selected_item.removeClass("chosen-default");
1086 | }
1087 | return this.selected_item.find("span").html(text);
1088 | };
1089 |
1090 | Chosen.prototype.result_deselect = function(pos) {
1091 | var result_data;
1092 | result_data = this.results_data[pos];
1093 | if (!this.form_field.options[result_data.options_index].disabled) {
1094 | result_data.selected = false;
1095 | this.form_field.options[result_data.options_index].selected = false;
1096 | this.selected_option_count = null;
1097 | this.result_clear_highlight();
1098 | if (this.results_showing) {
1099 | this.winnow_results();
1100 | }
1101 | this.form_field_jq.trigger("change", {
1102 | deselected: this.form_field.options[result_data.options_index].value
1103 | });
1104 | this.search_field_scale();
1105 | return true;
1106 | } else {
1107 | return false;
1108 | }
1109 | };
1110 |
1111 | Chosen.prototype.single_deselect_control_build = function() {
1112 | if (!this.allow_single_deselect) {
1113 | return;
1114 | }
1115 | if (!this.selected_item.find("abbr").length) {
1116 | this.selected_item.find("span").first().after("");
1117 | }
1118 | return this.selected_item.addClass("chosen-single-with-deselect");
1119 | };
1120 |
1121 | Chosen.prototype.get_search_text = function() {
1122 | return $('').text($.trim(this.search_field.val())).html();
1123 | };
1124 |
1125 | Chosen.prototype.winnow_results_set_highlight = function() {
1126 | var do_high, selected_results;
1127 | selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : [];
1128 | do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first();
1129 | if (do_high != null) {
1130 | return this.result_do_highlight(do_high);
1131 | }
1132 | };
1133 |
1134 | Chosen.prototype.no_results = function(terms) {
1135 | var no_results_html;
1136 | no_results_html = $('' + this.results_none_found + ' ""');
1137 | no_results_html.find("span").first().html(terms);
1138 | this.search_results.append(no_results_html);
1139 | return this.form_field_jq.trigger("chosen:no_results", {
1140 | chosen: this
1141 | });
1142 | };
1143 |
1144 | Chosen.prototype.no_results_clear = function() {
1145 | return this.search_results.find(".no-results").remove();
1146 | };
1147 |
1148 | Chosen.prototype.keydown_arrow = function() {
1149 | var next_sib;
1150 | if (this.results_showing && this.result_highlight) {
1151 | next_sib = this.result_highlight.nextAll("li.active-result").first();
1152 | if (next_sib) {
1153 | return this.result_do_highlight(next_sib);
1154 | }
1155 | } else {
1156 | return this.results_show();
1157 | }
1158 | };
1159 |
1160 | Chosen.prototype.keyup_arrow = function() {
1161 | var prev_sibs;
1162 | if (!this.results_showing && !this.is_multiple) {
1163 | return this.results_show();
1164 | } else if (this.result_highlight) {
1165 | prev_sibs = this.result_highlight.prevAll("li.active-result");
1166 | if (prev_sibs.length) {
1167 | return this.result_do_highlight(prev_sibs.first());
1168 | } else {
1169 | if (this.choices_count() > 0) {
1170 | this.results_hide();
1171 | }
1172 | return this.result_clear_highlight();
1173 | }
1174 | }
1175 | };
1176 |
1177 | Chosen.prototype.keydown_backstroke = function() {
1178 | var next_available_destroy;
1179 | if (this.pending_backstroke) {
1180 | this.choice_destroy(this.pending_backstroke.find("a").first());
1181 | return this.clear_backstroke();
1182 | } else {
1183 | next_available_destroy = this.search_container.siblings("li.search-choice").last();
1184 | if (next_available_destroy.length && !next_available_destroy.hasClass("search-choice-disabled")) {
1185 | this.pending_backstroke = next_available_destroy;
1186 | if (this.single_backstroke_delete) {
1187 | return this.keydown_backstroke();
1188 | } else {
1189 | return this.pending_backstroke.addClass("search-choice-focus");
1190 | }
1191 | }
1192 | }
1193 | };
1194 |
1195 | Chosen.prototype.clear_backstroke = function() {
1196 | if (this.pending_backstroke) {
1197 | this.pending_backstroke.removeClass("search-choice-focus");
1198 | }
1199 | return this.pending_backstroke = null;
1200 | };
1201 |
1202 | Chosen.prototype.keydown_checker = function(evt) {
1203 | var stroke, _ref1;
1204 | stroke = (_ref1 = evt.which) != null ? _ref1 : evt.keyCode;
1205 | this.search_field_scale();
1206 | if (stroke !== 8 && this.pending_backstroke) {
1207 | this.clear_backstroke();
1208 | }
1209 | switch (stroke) {
1210 | case 8:
1211 | this.backstroke_length = this.search_field.val().length;
1212 | break;
1213 | case 9:
1214 | if (this.results_showing && !this.is_multiple) {
1215 | this.result_select(evt);
1216 | }
1217 | this.mouse_on_container = false;
1218 | break;
1219 | case 13:
1220 | if (this.results_showing) {
1221 | evt.preventDefault();
1222 | }
1223 | break;
1224 | case 32:
1225 | if (this.disable_search) {
1226 | evt.preventDefault();
1227 | }
1228 | break;
1229 | case 38:
1230 | evt.preventDefault();
1231 | this.keyup_arrow();
1232 | break;
1233 | case 40:
1234 | evt.preventDefault();
1235 | this.keydown_arrow();
1236 | break;
1237 | }
1238 | };
1239 |
1240 | Chosen.prototype.search_field_scale = function() {
1241 | var div, f_width, h, style, style_block, styles, w, _i, _len;
1242 | if (this.is_multiple) {
1243 | h = 0;
1244 | w = 0;
1245 | style_block = "position:absolute; left: -1000px; top: -1000px; display:none;";
1246 | styles = ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing'];
1247 | for (_i = 0, _len = styles.length; _i < _len; _i++) {
1248 | style = styles[_i];
1249 | style_block += style + ":" + this.search_field.css(style) + ";";
1250 | }
1251 | div = $('', {
1252 | 'style': style_block
1253 | });
1254 | div.text(this.search_field.val());
1255 | $('body').append(div);
1256 | w = div.width() + 25;
1257 | div.remove();
1258 | f_width = this.container.outerWidth();
1259 | if (w > f_width - 10) {
1260 | w = f_width - 10;
1261 | }
1262 | return this.search_field.css({
1263 | 'width': w + 'px'
1264 | });
1265 | }
1266 | };
1267 |
1268 | return Chosen;
1269 |
1270 | })(AbstractChosen);
1271 |
1272 | }).call(this);
1273 |
--------------------------------------------------------------------------------