///
///
/* HTML5 Page: MadHatter/pageShape.html - SignalR Hub and Knockout extenders */
$(function () {
function hintViewModel(Title, owner) {
//var self = this;
this.Title = ko.observable(Title);
};
function itemViewModel(ContractNo, MarketNo, CoreCode, Review, Summary, Flag, Ratings, owner)
{
var self = this;
this.ContractNo = ContractNo;
this.CoreCode = ko.observable(CoreCode);
this.MarketNo = ko.observable(MarketNo);
this.Review = ko.observable(Review);
this.Summary = ko.observable(Summary).extend({ required: " Please Enter A Summary" });
this.Flag = ko.observable(Flag);
this.Ratings = ko.observable(Ratings);
this.MarketNo.subscribe( function (newValue) { owner.updateRecord(ko.toJS(self), null); });
this.CoreCode.subscribe( function (newValue) { owner.updateRecord(ko.toJS(self), null); });
this.Review.subscribe( function (newValue) { owner.updateRecord(ko.toJS(self), null); });
this.Summary.subscribe( function (newValue) { owner.updateRecord(ko.toJS(self), self.Summary); });
this.Flag.subscribe( function (newValue) { owner.updateRecord(ko.toJS(self), null); });
this.Ratings.subscribe( function (newValue) { owner.updateRecord(ko.toJS(self), null); });
this.removeContract = function () { owner.deleteSelected(this.ContractNo); }
};
function CareGridViewModel()
{
var self = this;
this.hub = $.connection.careFormHub;
this.HintOA = ko.observableArray([]);
this.ListingOA = ko.observableArray([]);
this.MarketNo = ko.observable();
this.CoreCode = ko.observable();
this.Review = ko.observable();
this.Summary = ko.observable();
this.Flag = ko.observable();
this.Ratings = ko.observable();
var HintOA = this.HintOA;
var ListingOA = this.ListingOA;
this.marketTypes = [ /*{ Title: "Oil", MarketNo: 1 }, { Title: "Gas", MarketNo: 2 }, { Title: "Liffe", MarketNo: 3 }*/ ];
var notify = true;
this.createContract = function ()
{
document.getElementById("save").focus();/*Free type "dd-MMM-ccyy" press "Add" knockout, moment or jQuery.datepicker() needs to lose focus*/
/* MarketNo or Title. Hide And Find Using Combo DEVELOPER */
var oRecord = {
//Title: this.Title(),
MarketNo: this.MarketNo(),
CoreCode: this.CoreCode(),
Review: this.Review(),
Summary: this.Summary(),
Flag: this.Flag(),
Ratings: this.Ratings(),
};
this.hub.server.add(oRecord).done( function() {
console.log('Contract Record Saved!');
}).fail(function(error) {
console.warn(error);
});
/*[schtml]*/
//this.Title('');
this.MarketNo('');
this.CoreCode('');
this.Review('');
this.Summary('');
this.Flag('A');
this.Ratings('');
/*[schtml]*/
}
this.deleteSelected = function (id) {
this.hub.server.delete(id);
}
this.updateRecord = function (oRecord, observe)
{
if (notify)
{
var isOK = (observe != null) ? !observe.hasError() : true;
if (isOK) {
//var kFilter = /[A-z]/g;
//if (!kFilter.test(oRecord.Amount)) /* /[-+]?[0-9]*\.?[0-9]+/ /[A-z]/g */
this.hub.server.update(oRecord);
}
}
}
this.hub.client.contractCreated = function( oItem ) {
ListingOA.push(new itemViewModel( oItem.ContractNo,
oItem.MarketNo,
oItem.CoreCode,
oItem.Review,
oItem.Summary,
oItem.Flag,
oItem.Ratings,
self));
};
this.hub.client.recordRemoved = function( id ) {
var record = ko.utils.arrayFilter(ListingOA(), function (value) { return value.ContractNo == id; })[0];
ListingOA.remove( record );
};
this.hub.client.recordUpdated = function (changedRecord)
{
var record = ko.utils.arrayFilter(ListingOA(), function (value) { return value.ContractNo == changedRecord.ContractNo; })[0];
if (record) {
//var keyId = $("#MarketNo").val();
//if (keyId != changedRecord.MarketNo)
//this.hub.client.recordRemoved(changedRecord.ContractNo);
//else{
notify = false;
record.ContractNo = changedRecord.ContractNo;
record.MarketNo(changedRecord.MarketNo);
record.CoreCode(changedRecord.CoreCode);
record.Review( changedRecord.Review);
record.Summary( changedRecord.Summary);
record.Flag( changedRecord.Flag);
record.Ratings( changedRecord.Ratings);
notify = true;
//}
}
};
//and detected the event in permissionChanged function
this.permissionChanged = function (obj, event) {
if (event.originalEvent) {
//user changed
var keyId = $("#MarketNo").val();
var mNo = document.getElementById("MarketNo");
if (keyId !== null && keyId > 0) {
$('#error').html(" ... Loading Please Wait - Scanning ... Records ... Index");
this.hub.server.fillCategoryCareForm(keyId);
}
}
else {
// program changed
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
this.hub.client.allHintsRetrieved = function (itemsMap) {
var records = $.map(itemsMap, function (oHints) {
return new hintViewModel(oHints.Title, self)
});
self.HintOA(records);
};
this.hub.client.allMarketTypesRetrieved = function (json) {
var records = ko.utils.arrayMap(json, function (marketType) {
return {
Title: marketType.Title,
MarketNo: marketType.MarketNo
};
});
self.marketTypes = records;
/* DEVELOPER|BUG_BEAR: */
var mNo = document.getElementById("MarketNo");
mNo.innerText = "0";
document.getElementById("MarketNo").focus(); /* -- Without This The knockout Category select is blank -- */
//
//document.getElementById("MarketText").focus();
};
this.hub.client.allItemsRetrieved = function (itemsMap) {
var records = $.map(itemsMap, function (oItem) {
return new itemViewModel( oItem.ContractNo,
oItem.MarketNo,
oItem.CoreCode,
oItem.Review,
oItem.Summary,
oItem.Flag,
oItem.Ratings,
self)
});
ListingOA(records);
};
/////////////////////////////////////////////////////////////////////////////////////////////////
this.hub.client.issueError = function (error) {
$("#error").text(error);
};
this.init = function () {
this.hub.server.getAllMarketTypes(); /* camelCode: personHub.getAll()|PersonHub.GetAll */
}
}
ko.extenders.required = function(target, overrideMessage) {
//add some sub-observables to our observable
target.hasError = ko.observable();
target.validationMessage = ko.observable();
/* define a validation function */
function validate( newValue ) {
//var current = target();
//var writeValue = "banana";
//target.notifySubscribers(valueToWrite);
//target.getSubscriptionsCount();
var bFail = (newValue && newValue.length > 0)? false: true;
target.hasError(bFail);
target.validationMessage( !bFail ? "" : overrideMessage || "This field is required");
}
validate(target()); //initial validation
target.subscribe(validate); //validate whenever the value changes
return target; // the original observable
};
ko.bindingHandlers.dateString = {
init : function(element, valueAccessor) {
//attach an event handler to our dom element to handle user input
element.onchange = function(){
var value = valueAccessor(); //get our observable
//set our observable to the parsed date from the input
value( moment(element.value, 'DD-MMM-YYYY').toDate() );
};
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var value = valueAccessor();
var valueUnwrapped = ko.utils.unwrapObservable(value);
if (valueUnwrapped) {
element.value = moment(valueUnwrapped).format('DD-MMM-YYYY');
}
}
};
/* Does this work? "12.125..0" */
$('.numbersOnly').keyup(function () {
/* BUG_BEAR: Enter Number "123.456" Try to change Number "-123.456". Although "123.456" is back on the screen "0" goes into database */
//event
// if (event.which == 13) {
// event.preventDefault();
// }
if (this.value != this.value.replace(/[^0-9\.]/g, '')) {
this.value = this.value.replace(/[^0-9\.]/g, '');
}
});
if (!Modernizr.inputtypes.datetime) {
$("input[type='datetime']").datepicker({ dateFormat: "dd-M-yy" });
/* [DisplayFormat(ApplyFormatInEditMode=true, DataFormatString = "{0:dd-MMM-yyyy}")] */
/* The Opera Browser Has A Built In datetimepicker, Issue on Edit - Razor ? [jquery.unobtrusive*;jquery.validate*] BUG_BEAR|WHAT_THIS|CLEAN_UP|DEVELOPER|MANUAL */
}
var viewModel = new CareGridViewModel();
ko.applyBindings(viewModel);
$.connection.hub.start(function () {
viewModel.init();
});
});