Merge branch 'maint/7.0'
[ninja.git] / application / media / js / ajax.commands.js
blob6fbd1625342b4e290f4446011a394b68b8d8971b
2 $("document").ready( function () {
4         $( "body" ).on( "click", "button.command-button", function () {
6                 var btn = $( this ),
7                         command = btn.attr( "data-command" ),
8                         href = _site_domain + _index_page + "/ajax/command/",
9                         toggle = false,
10                         dialog;
12                 if ( command.match( /ENABLE/ ) ) {
13                         toggle = command.replace( "ENABLE", "DISABLE" );
14                 } else if ( command.match( /DISABLE/ ) ) {
15                         toggle = command.replace( "DISABLE", "ENABLE" );
16                 }
18                 if ( command.match( /START/ ) ) {
19                         toggle = command.replace( "START", "STOP" );
20                 } else if ( command.match( /STOP/ ) ) {
21                         toggle = command.replace( "STOP", "START" );
22                 }
24                 btn.attr( "disabled", true );
26                 $.ajax({
27                         url : href,
28                         type: "POST",
29                         data: {
30                                 "method": "submit",
31                                 "command": command
32                         },
33                         success : function( data ) {
35                                 var msg = "<strong>" + data.brief + "</strong><br />" + data.description;
37                                 dialog = $.notify( msg, {
38                                         "sticky": true,
39                                         "remove": function () {
40                                                 btn.removeAttr( "disabled" );
41                                         },
42                                         "buttons": {
44                                                 "Submit command!": function () {
46                                                         dialog.remove();
48                                                         $.ajax({
49                                                                 url : href,
50                                                                 type: "POST",
51                                                                 data: {
52                                                                         "method": "commit",
53                                                                         "command": command
54                                                                 },
55                                                                 success : function( data ) {
57                                                                         var title = btn.html();
59                                                                         $.notify( "Command has been executed!" );
61                                                                         if ( typeof( data.state ) != "undefined" ) {
62                                                                                 if ( data.state === 0 ) {
63                                                                                         title = title.replace( /Enable/, "Disable" );
64                                                                                         title = title.replace( /Start/, "Stop" );
65                                                                                 } else {
66                                                                                         title = title.replace( /Stop/, "Start" );
67                                                                                         title = title.replace( /Disable/, "Enable" );
68                                                                                 }
70                                                                                 btn.html( title );
71                                                                                 btn.attr( "data-state", (data.state == 1) ? 0 : 1 );
73                                                                                 if ( toggle )
74                                                                                         btn.attr( "data-command", toggle );
75                                                                                 toggle = command;
77                                                                         }
79                                                                         btn.removeAttr( "disabled" );
81                                                                 }
82                                                         });
83                                                 }
85                                         }
86                                 } );
88                         }
89                 });
91         } );
93 } );