<commit_before>/*
 *  (c) <NAME>. 3DaGoGo, Inc. (<EMAIL>)
 *
 *  Distributed under the GNU Affero General Public License http://www.gnu.org/licenses/agpl.html
 */

var SettingsPage = Backbone.View.extend({
	parent: null,
	initialize: function(params) {
		this.parent = params.parent;
	},
	show: function() {
		this.parent.$el.find('.settings-page').addClass('hide');
		this.$el.removeClass('hide');
	}
});

/***********************
* Printer - Connection
************************/

var PrinterConnectionView = SettingsPage.extend({
	el: '#printer-connection',
	template: _.template( $("#printer-connection-settings-page-template").html() ),
	settings: null,
	initialize: function(params)
	{
		this.listenTo(app.socketData, 'change:printer', this.printerStatusChanged );

		SettingsPage.prototype.initialize.call(this, params);
	},
	show: function() {
		//Call Super
		SettingsPage.prototype.show.apply(this);

		if (!this.settings) {
			this.getInfo();
		}
	},
	getInfo: function()
	{
		this.$('a.retry-ports i').addClass('animate-spin');
		$.getJSON(API_BASEURL + 'settings/printer', null, _.bind(function(data) {
			if (data.serial) {
				this.settings = data;
				this.render(); // This removes the animate-spin from the link
				this.delegateEvents({
					'change #settings-baudrate': 'saveConnectionSettings',
					'change #settings-serial-port': 'saveConnectionSettings',
					'click a.retry-ports': 'retryPortsClicked',
					'click .loading-button.test-connection button': 'testConnection'
				});
			} else {
				noty({text: "No serial settings found.", timeout: 3000});
			}
		}, this))
		.fail(function() {
			noty({text: "There was an error getting serial settings.", timeout: 3000});
			this.$('a.retry-ports i').removeClass('animate-spin');
		})
	},
	render: function() 
	{
		this.$('form').html(this.template({ 
			settings: this.settings
		}));

		this.printerStatusChanged(app.socketData, app.socketData.get('printer'));
	},
	retryPortsClicked: function(e)
	{
		e.preventDefault();
		this.getInfo();
	},
	saveConnectionSettings: function(e) {
		var connectionData = {};

		_.each(this.$('form').serializeArray(), function(e){
			connectionData[e.name] = e.value;
		});

		if (connectionData.baudrate && connectionData.port) {
			this.$('.loading-button.test-connection').addClass('loading');
			this.$('.connection-status').removeClass('failed connected').addClass('connecting');
	        $.ajax({
	            url: API_BASEURL + "connection",
	            type: "POST",
	            dataType: "json",
	            contentType: "application/json; charset=UTF-8",
	            data: JSON.stringify({
		            "command": "connect",
		            "port": connectionData.port,
		            "baudrate": parseInt(connectionData.baudrate),
		            "autoconnect": true,
		            "save": true
		        })
	        })
			.fail(function(){
				noty({text: "There was an error saving connection settings.", timeout: 3000});
			});
		}
	},
	printerStatusChanged: function(s, value)
	{
		this.$('.connection-status').removeClass('connecting failed connected').addClass(value.status);

		if (value.status != 'connecting') {
			console.log(value);
			this.$('.loading-button.test-connection').removeClass('loading');
		}
	},
	testConnection: function(e)
	{
		e.preventDefault();
		this.saveConnectionSettings();
	}
});

/***********************
* Printer - Profile
************************/

var PrinterProfileView = SettingsPage.extend({
	el: '#printer-profile',<commit_msg>remove log<commit_after>/*
 *  (c) <NAME>. 3DaGoGo, Inc. (<EMAIL>)
 *
 *  Distributed under the GNU Affero General Public License http://www.gnu.org/licenses/agpl.html
 */

var SettingsPage = Backbone.View.extend({
	parent: null,
	initialize: function(params) {
		this.parent = params.parent;
	},
	show: function() {
		this.parent.$el.find('.settings-page').addClass('hide');
		this.$el.removeClass('hide');
	}
});

/***********************
* Printer - Connection
************************/

var PrinterConnectionView = SettingsPage.extend({
	el: '#printer-connection',
	template: _.template( $("#printer-connection-settings-page-template").html() ),
	settings: null,
	initialize: function(params)
	{
		this.listenTo(app.socketData, 'change:printer', this.printerStatusChanged );

		SettingsPage.prototype.initialize.call(this, params);
	},
	show: function() {
		//Call Super
		SettingsPage.prototype.show.apply(this);

		if (!this.settings) {
			this.getInfo();
		}
	},
	getInfo: function()
	{
		this.$('a.retry-ports i').addClass('animate-spin');
		$.getJSON(API_BASEURL + 'settings/printer', null, _.bind(function(data) {
			if (data.serial) {
				this.settings = data;
				this.render(); // This removes the animate-spin from the link
				this.delegateEvents({
					'change #settings-baudrate': 'saveConnectionSettings',
					'change #settings-serial-port': 'saveConnectionSettings',
					'click a.retry-ports': 'retryPortsClicked',
					'click .loading-button.test-connection button': 'testConnection'
				});
			} else {
				noty({text: "No serial settings found.", timeout: 3000});
			}
		}, this))
		.fail(function() {
			noty({text: "There was an error getting serial settings.", timeout: 3000});
			this.$('a.retry-ports i').removeClass('animate-spin');
		})
	},
	render: function() 
	{
		this.$('form').html(this.template({ 
			settings: this.settings
		}));

		this.printerStatusChanged(app.socketData, app.socketData.get('printer'));
	},
	retryPortsClicked: function(e)
	{
		e.preventDefault();
		this.getInfo();
	},
	saveConnectionSettings: function(e) {
		var connectionData = {};

		_.each(this.$('form').serializeArray(), function(e){
			connectionData[e.name] = e.value;
		});

		if (connectionData.baudrate && connectionData.port) {
			this.$('.loading-button.test-connection').addClass('loading');
			this.$('.connection-status').removeClass('failed connected').addClass('connecting');
	        $.ajax({
	            url: API_BASEURL + "connection",
	            type: "POST",
	            dataType: "json",
	            contentType: "application/json; charset=UTF-8",
	            data: JSON.stringify({
		            "command": "connect",
		            "port": connectionData.port,
		            "baudrate": parseInt(connectionData.baudrate),
		            "autoconnect": true,
		            "save": true
		        })
	        })
			.fail(function(){
				noty({text: "There was an error saving connection settings.", timeout: 3000});
			});
		}
	},
	printerStatusChanged: function(s, value)
	{
		this.$('.connection-status').removeClass('connecting failed connected').addClass(value.status);

		if (value.status != 'connecting') {
			this.$('.loading-button.test-connection').removeClass('loading');
		}
	},
	testConnection: function(e)
	{
		e.preventDefault();
		this.saveConnectionSettings();
	}
});

/***********************
* Printer - Profile
************************/

var PrinterProfileView = SettingsPage.extend({
	el: '#printer-profile',
{'fraction_non_alphanumeric': 0.19511494252873562, 'fraction_numerical': 0.004022988505747126, 'mean_word_length': 3.631403858948769, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4883944', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2121, 'n_words': 383}