Created
January 20, 2014 20:22
-
-
Save David-Melo/8528316 to your computer and use it in GitHub Desktop.
CGE Main App Logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var my = my || {}; | |
$("img.gallery-thumb").error(function(a,b) { | |
$(this).hide(); | |
}); | |
head.ready(function(){ | |
my.vm = (function(){ | |
var | |
_csrf = $('body').data('csrf'); | |
socket = ko.observable(); | |
utils = { | |
socketCometReceived: function (message) { | |
console.log('[] New comet message received :: ', message); | |
if(message.model === 'user'){ | |
var userId = message.id; | |
console.log(userId); | |
} | |
} | |
}; | |
return { | |
_csrf: _csrf, | |
socket: socket, | |
utils: utils | |
} | |
})(my.vm); | |
// as soon as this file is loaded, connect automatically, | |
var SocketInit = $.ajax({ | |
url: '/session/init', | |
type: "GET", | |
dataType: 'jsonp', | |
success: function(response){ | |
// Socket IO | |
(function (io) { | |
var socket = io.connect(); | |
if (typeof console !== 'undefined') { | |
log('[] Connecting to Server...'); | |
log('[] '+response.message); | |
} | |
socket.on('connect', function socketConnected() { | |
socket.on('message', my.vm.utils.socketCometReceived); | |
socket.get('/user/subscribe'); | |
log('[] Socket is now connected.\n'); | |
}); | |
my.vm.socket(socket); | |
function log () { | |
if (typeof console !== 'undefined') { | |
console.log.apply(console, arguments); | |
} | |
} | |
})(window.io); | |
}, | |
error: function(a,b,c){ | |
console.log('failure'); | |
}, | |
complete: function(a,b,c){ | |
} | |
}); | |
/* | |
head.ready(function(){ | |
var app = angular.module("app", []).config(function($routeProvider){ | |
$routeProvider.when('/login',{ | |
templateUrl: 'template/login.html', | |
controller: 'LoginController' | |
}); | |
$routeProvider.otherwise({ | |
redirectTo: '/login' | |
}); | |
}); | |
app.controller('LoginController',function(){ | |
}); | |
angular.element(document).ready(function() { | |
angular.bootstrap(document, ['app']); | |
}); | |
});*/ | |
my.vm.proto = (function(){ | |
var | |
utils = { | |
} | |
return { | |
utils: utils | |
} | |
})(my.vm.proto); | |
my.vm.print = (function(){ | |
var | |
go = function(url){ | |
NProgress.start(); | |
if($('#print-frame')){ | |
$('#print-frame').remove(); | |
} | |
// Set attributes as a second parameter | |
$('<iframe />', { | |
id: 'print-frame', | |
name: 'print-frame', | |
src: url, | |
class: 'ko-hidden', | |
width: '1024', | |
height: '9000' | |
}).appendTo('body'); | |
setTimeout(function(){ | |
window.frames["print-frame"].focus(); | |
window.frames["print-frame"].print(); | |
NProgress.done(); | |
},4000); | |
}, | |
utils = { | |
} | |
return { | |
go : go, | |
utils: utils | |
} | |
})(my.vm.print); | |
my.vm.appointments = (function(){ | |
var | |
utils = { | |
makeDates: function(){ | |
var d1 = moment(); | |
var d2 = moment().add('days', 9); | |
var itr = moment(d1).twix(d2).iterateInner("days"); | |
var range=[]; | |
while(itr.hasNext()){ | |
range.push(itr.next().format()); | |
} | |
var days = []; | |
ko.utils.arrayForEach(range, function(item) { | |
var shortDay = moment(item).format('ddd'); | |
if(shortDay !== 'Sat' && shortDay !== 'Sun'){ | |
days.push({label:moment(item).format('dddd, MM/D/YY'),value:moment(item).format()}); | |
} | |
}); | |
return days; | |
} | |
}; | |
return { | |
utils: utils | |
} | |
})(my.vm.appointments); | |
my.vm.listings = (function(){ | |
var | |
utils = { | |
makeBasic: function(listing){ | |
var self = this; | |
ko.mapping.fromJS(listing, {}, self); | |
//listing.editedDate = Globalize.format(new Date(listing.field_131),'M/d/yyyy h:mm tt'); | |
self.cgid = ko.observable(listing.id); | |
self.sysid = ko.observable(listing.field_sysid); | |
self.mls = ko.observable(listing.field_157); | |
self.city = ko.observable(listing.field_922); | |
self.state = ko.observable(listing.field_924); | |
self.zip = ko.observable(listing.field_10); | |
self.county = ko.observable(listing.field_61); | |
self.editedDate = moment(listing.field_131).fromNow(); | |
self.source = ko.observable(listing.source); | |
self.price = ko.observable(Globalize.format(parseInt(listing.field_137),'c0')); | |
self.beds = ko.observable(listing.field_25); | |
self.baths = ko.observable(listing.field_92); | |
self.status = ko.observable(listing.field_246); | |
self.year = ko.observable(listing.field_314); | |
switch(listing.source){ | |
case 'RE1': | |
self.mode = 'RES'; | |
break; | |
case 'RE2': | |
self.mode = 'RES'; | |
break; | |
case 'RIN': | |
self.mode = 'COM'; | |
break; | |
case 'RLD': | |
self.mode = 'COM'; | |
break; | |
case 'CLD': | |
self.mode = 'COM'; | |
break; | |
case 'RNT': | |
self.mode = 'RES'; | |
break; | |
case 'COM': | |
self.mode = 'COM'; | |
break; | |
case 'BUS': | |
self.mode = 'COM'; | |
break; | |
} | |
}, | |
makeDetailed: function(listing){ | |
var sef = this; | |
if(!listing) { | |
} | |
} | |
} | |
return { | |
utils: utils | |
} | |
})(my.vm.listings); | |
my.vm.mailer = (function(){ | |
var | |
utils = { | |
makeMail:function(data){ | |
var self = this; | |
self.subject = ko.observable(data.subject); | |
self.contact = ko.observable(data.contact); | |
self.email = ko.observable(data.email); | |
self.name = ko.observable(data.name); | |
self.phone = ko.observable(data.phone); | |
self.message = ko.observable(data.message); | |
self.data = data; | |
return self; | |
}, | |
send: function(message,tpl,callback){ | |
//my.vm.lp.utils.isBusy(true); | |
NProgress.start(); | |
var data = ko.toJS(message); | |
var template = $("#"+tpl).html(); | |
var output = Mustache.render(template, data.data); | |
data.body = output.replace(/\s{2,}/g,' '); | |
data.body = data.body.replace(/'/g,'\u2018'); | |
data._csrf = my.vm._csrf; | |
my.vm.socket().post('/mail/send', data, function(res){ | |
if(res===200){ | |
callback(null); | |
}else{ | |
callback('There Was An Error'); | |
} | |
//my.vm.lp.utils.isBusy(false); | |
NProgress.done(); | |
}); | |
} | |
}; | |
return { | |
utils: utils | |
} | |
})(my.vm.mailer); | |
my.vm.search = (function(){ | |
var | |
app = $.sammy('#main-content',function() { | |
self = this; | |
function preCheck(callback) { | |
if(my.vm.search.ready()){ | |
callback(); | |
} else { | |
console.log('NoCriteria'); | |
return false; | |
} | |
}; | |
self.around(preCheck); | |
self.notFound = function(a,b){ | |
self.runRoute('get', '/#'); | |
}; | |
self.bind('run', function(){ | |
//my.vm.sizeWindow(); | |
}); | |
self.get('/#',function() { | |
console.log('null'); | |
}); | |
self.get('#/search',function() { | |
var self = this; | |
self.load('/template/find/search/results',function(content){ | |
}).then(function(content){ | |
$("#main-content").html(content); | |
}).then(function(){ | |
ko.applyBindingsToDescendants(my.vm,document.getElementById('main-content')); | |
$('#search-tabs a[href="#results"]').tab('show'); | |
$('.tooltips').tooltip(); | |
}); | |
}); | |
}), | |
options = { | |
counties: [ | |
{value: 'DADE', label: 'Miami-Dade'}, | |
{value: 'BROWARD', label: 'Broward'}, | |
{value: 'PALMBCH', label: 'Palm Beach'}, | |
{value: 'OTHER', label: 'Other'} | |
] | |
}, | |
utils = { | |
isBusy: ko.observable(0), | |
preload: {}, | |
pagination: { | |
perPage: ko.observable('10'), | |
curPage: ko.observable(1), | |
numPages: ko.observable(0), | |
offset: ko.observable(0), | |
pages: ko.observableArray([]), | |
getArray: function(a,b){ | |
var arr = []; | |
for (var i = a; i <= b; i++) { | |
arr.push(i); | |
} | |
return arr | |
}, | |
firstPage: function(){ | |
my.vm.search.utils.pagination.curPage(1); | |
my.vm.search.quick.go(); | |
}, | |
toPage: function(page){ | |
my.vm.search.utils.pagination.curPage(page); | |
my.vm.search.quick.go(); | |
}, | |
prevPage: function(){ | |
var fromPage = my.vm.search.utils.pagination.curPage(); | |
var toPage = fromPage-1; | |
if(toPage>0){ | |
my.vm.search.utils.pagination.curPage(toPage); | |
my.vm.search.quick.go(); | |
} | |
}, | |
nextPage: function(){ | |
var fromPage = my.vm.search.utils.pagination.curPage(); | |
var totPages = my.vm.search.utils.pagination.numPages(); | |
var toPage = fromPage+1; | |
if(toPage<totPages){ | |
my.vm.search.utils.pagination.curPage(toPage); | |
my.vm.search.quick.go(); | |
} | |
}, | |
lastPage: function(){ | |
var lastPage = my.vm.search.utils.pagination.numPages(); | |
my.vm.search.utils.pagination.curPage(lastPage); | |
my.vm.search.quick.go(); | |
}, | |
refresh: function(){ | |
my.vm.search.quick.go(); | |
} | |
}, | |
enumerate : function(number,string){ | |
var options = []; | |
for (var i=1;i<=number;i++) | |
{ | |
if(string){ | |
var label = (i>1)? string + 's' : string; | |
options.push({label:i+" "+label,value:i}); | |
} else { | |
options.push({label:i,value:i}); | |
} | |
} | |
return options; | |
}, | |
makeRaw: function(data){ | |
var obj = {}; | |
ko.utils.arrayForEach(data, function(item) { | |
item = ko.toJS(item); | |
if(obj[item.id]){ | |
obj[item.id][item.modifier.value] = item.castType(); | |
} else { | |
obj[item.id] = item.query.raw(); | |
} | |
}); | |
return obj; | |
}, | |
makePretty: function(data){ | |
var strings = []; | |
var label = (my.vm.search.quick.utils.target())? '<b>'+my.vm.search.quick.utils.target().label+'</b> ' : ''; | |
if(data.length) strings.push("Find all "+ label +"listings where: "); | |
ko.utils.arrayForEach(data, function(item) { | |
strings.push(item.query.pretty()); | |
}); | |
return strings.join(""); | |
}, | |
runQuery: function(){ | |
my.vm.search.utils.clear(); | |
my.vm.search.utils.isBusy(1); | |
var destination = my.vm.search.utils.destinationSet(); | |
if(destination){ | |
NProgress.start(); | |
var target = my.vm.search.quick.utils.target(); | |
var data = {} | |
data.query = my.vm.search.query().raw; | |
data.page = my.vm.search.utils.pagination.curPage(); | |
data.limit = my.vm.search.utils.pagination.perPage(); | |
data._csrf = my.vm._csrf; | |
my.vm.socket().get(target.url, data , function(res){ | |
my.vm.search.utils.listResults(res,function(){ | |
my.vm.search.app.run('#/search'); | |
}); | |
}); | |
} else { | |
my.vm.search.utils.isBusy(0); | |
NProgress.done(); | |
} | |
}, | |
listResults: function(data,done){ | |
// Reset Arrays | |
my.vm.search.data.results.count(0) | |
my.vm.search.data.results.items([]); | |
// Insert New Results | |
my.vm.search.data.results.count(data.count); | |
ko.utils.arrayForEach(data.results, function(item) { | |
my.vm.search.data.results.items.push(new my.vm.listings.utils.makeBasic(item)); | |
}); | |
NProgress.done(); | |
my.vm.search.utils.isBusy(0); | |
done(); | |
}, | |
openResult: function(item,b,c,s){ | |
if($(b.target).parents('.btn-group').hasClass("share-options")){ | |
console.log('prevnet'); | |
return false; | |
} | |
NProgress.start(); | |
var tabs = $('#search-tabs'); | |
var tab = $('#search-tabs a[href="#' + item.cgid() + '"]').length; | |
if(!tab){ | |
console.log('theresnotab'); | |
$.get('/find/' + my.vm.search.quick.utils.target().value + '/'+ item.cgid(), function(template) { | |
tabs.append( | |
$('<li><a id="tab' + item.cgid() + '" href="#' + item.cgid() + '" data-toggle="tab" class="result-tab-link">' + | |
'Listing #' + item.cgid() + | |
'<button data-bind="click: search.utils.closeResult" class="close close-btn" type="button" ' + | |
'title="Close Tab">×</button>' + | |
'</a></li>')); | |
$('#search-tabs-content').append( | |
$('<div class="tab-pane result-tab-container" id="' + item.cgid() + | |
'"><div id="result' + item.cgid() + '" class="result-tab-panel">' + template + '</div></div>')); | |
$('#search-tabs a[href="#' + item.cgid() + '"]').tab('show'); | |
$("body").animate({ scrollTop: 0 }, "fast"); | |
ko.applyBindings(my.vm,document.getElementById('result' + item.cgid())); | |
ko.applyBindings(my.vm,document.getElementById('tab' + item.cgid())); | |
NProgress.done(); | |
}); | |
} else { | |
console.log('theresatab'); | |
$('#search-tabs a[href="#' + item.cgid() + '"]').tab('show'); | |
NProgress.done(); | |
} | |
}, | |
closeResult: function(d,e){ | |
var item = String($(e.target.offsetParent).attr('href')); | |
var listing = (item).replace('#',''); | |
$('#search-tabs a[href="#results"]').tab('show'); | |
$('#tab'+listing).remove(); | |
$('#result'+listing).remove(); | |
$('#'+listing).remove(); | |
}, | |
clear: function(){ | |
$('.result-tab-link').remove(); | |
$('.result-tab-panel').remove(); | |
$('.result-tab-container').remove(); | |
my.vm.search.data.results.items([]); | |
} | |
}, | |
field = function(field){ | |
var self = this; | |
var modifiers = { | |
equals: {value: 'equals', label: 'Equals'}, | |
notEquals: {value: 'notEquals', label: 'Is Not'}, | |
contains: {value: 'contains', label: 'Contains'}, | |
startsWith: {value: 'startsWith', label: 'Starts With'}, | |
endsWith: {value: 'endsWith', label: 'Ends With'}, | |
greaterThan: {value: 'greaterThan', label: 'Greater Than'}, | |
lessThan: {value: 'lessThan', label: 'Less Than'} | |
}; | |
if(!field){ | |
field = { | |
id: null, | |
instance: '090', | |
field: '', | |
filters: ['equals'], | |
type: 'string' | |
} | |
}; | |
self.id = ko.observable(field.id); | |
self.instance = ko.observable(field.field.replace(/\s/g,"")+field.id); | |
self.field = ko.observable(field.field); | |
self.criteria = ko.observable(); | |
self.modifier = ko.observable((field.modifier)? modifiers[field.modifier] : modifiers['equals']); | |
self.type = ko.observable(field.type); | |
self.setFilter = function(filter){ | |
self.modifier(filter); | |
}; | |
self.setModifier = function(modifier){ | |
self.modifier(modifiers[modifier]); | |
}; | |
self.filters = function(){ | |
var filters = []; | |
if(!field.filters){ | |
return filters; | |
} | |
ko.utils.arrayForEach(field.filters, function(item) { | |
filters.push(modifiers[item]); | |
}); | |
return filters; | |
}; | |
self.options = function(){ | |
var options = []; | |
if(!field.options){ | |
return options; | |
} | |
ko.utils.arrayForEach(field.options, function(item) { | |
options.push(item); | |
}); | |
return options; | |
}; | |
self.castType = function(){ | |
switch(self.type()){ | |
case 'int': | |
return parseInt(self.criteria()); | |
break; | |
default: | |
return self.criteria(); | |
break; | |
} | |
}; | |
self.query = { | |
pretty: function(){ | |
var str = '<div class="search-query-item">'; | |
str += '<strong>' + self.field() + '</strong>' +' '; | |
str += self.modifier().label +': '; | |
str += '<em><strong class="text-info">' + self.criteria() + '</em></strong>.' +' '; | |
str += '</div>'; | |
return str; | |
}, | |
raw: function(){ | |
var obj; | |
var criteria; | |
switch(self.type()){ | |
case 'int': | |
criteria = parseInt(self.criteria()); | |
break; | |
default: | |
criteria = self.criteria(); | |
break; | |
} | |
//console.log(({}).toString.call(criteria).match(/\s([a-zA-Z]+)/)[1].toLowerCase()); | |
switch(self.modifier().value){ | |
case 'equals': | |
obj = criteria; | |
break; | |
case 'notEquals': | |
obj = { | |
'!': criteria | |
}; | |
break; | |
case 'contains': | |
obj = { | |
contains: criteria | |
}; | |
break; | |
case 'startsWith': | |
obj = { | |
startsWith: criteria | |
}; | |
break; | |
case 'endsWith': | |
obj = { | |
endsWith: criteria | |
}; | |
break; | |
case 'greaterThan': | |
obj = { | |
greaterThan: criteria | |
}; | |
break; | |
case 'lessThan': | |
obj = { | |
lessThan: criteria | |
}; | |
break; | |
} | |
return obj; | |
} | |
}; | |
}, | |
quick = { | |
loaded: ko.observable(0), | |
preloaded: ko.observable((window.quickSearch)?window.quickSearch:null), | |
criteria: { | |
string: ko.observable() | |
}, | |
utils: { | |
parseQuickStrings: function(input){ | |
if(input){ | |
var str = input; | |
str = str.replace(/[^a-zA-Z0-9]+/," "); | |
var arr = str.split(" "); | |
return arr; | |
} else { | |
return false; | |
} | |
} | |
}, | |
modes: { | |
current: ko.observable(), | |
type: ko.observable(), | |
destination: ko.observable().extend({required:{params:true,message:'Required'}}) | |
}, | |
go: function(){ | |
/*my.vm.search.utils.isBusy(1); | |
NProgress.start(); | |
var target = my.vm.search.quick.utils.target(); | |
// ---------Legacy Mode-------- // | |
var data = $('<input type="hidden" name="data" value="'+ encodeURI(JSON.stringify(my.vm.search.query().raw)) +'"/>'); | |
$('<form action="' + target.url + '/" method="POST"/>') | |
.append(data) | |
.append($('<input type="hidden" name="_csrf" value="'+ my.vm._csrf +'"/>')) | |
.appendTo($(document.body)) | |
.submit();*/ | |
// ---------Dynamic Mode-------- // | |
my.vm.search.utils.clear(); | |
my.vm.search.utils.isBusy(1); | |
var destination = my.vm.search.utils.destinationSet(); | |
if(destination){ | |
NProgress.start(); | |
var target = my.vm.search.quick.utils.target(); | |
var data = {} | |
data.meta = target; | |
data.query = my.vm.search.query().raw; | |
data.page = my.vm.search.utils.pagination.curPage(); | |
data.limit = my.vm.search.utils.pagination.perPage(); | |
data._csrf = my.vm._csrf; | |
my.vm.socket().get('/run-search', data , function(res){ | |
my.vm.search.utils.listResults(res,function(){ | |
my.vm.search.app.run('#/search'); | |
$("body").animate({ scrollTop: 0 }, "fast"); | |
$('#search-tabs a[href="#results"]').tab('show'); | |
}); | |
}); | |
} else { | |
my.vm.search.utils.isBusy(0); | |
NProgress.done(); | |
} | |
} | |
}, | |
criteria = ko.observableArray([]), | |
data = { | |
results: { | |
count: ko.observable(0), | |
items: ko.observableArray([]) | |
} | |
}; | |
return { | |
app: app, | |
utils: utils, | |
field: field, | |
options: options, | |
quick: quick, | |
criteria: criteria, | |
data: data | |
}; | |
})(my.vm.search); | |
my.vm.search.fields = { | |
cities: [ | |
{label: "Aventura", value: "Aventura"}, | |
{label: "Bal Harbour", value: "Bal Harbour"}, | |
{label: "Boca Raton", value: "Boca Raton"}, | |
{label: "Brickell", value: "Miami"}, | |
{label: "Brickell Key", value: "Brickell Key"}, | |
{label: "Coconut Grove", value: "Coconut Grove"}, | |
{label: "Cooper City", value: "Cooper City"}, | |
{label: "Coral Gables", value: "Coral Gables"}, | |
{label: "Cutler Bay", value: "Cutler Bay"}, | |
{label: "Dania Beach", value: "Dania Beach"}, | |
{label: "Davie", value: "Davie"}, | |
{label: "Doral", value: "Doral"}, | |
{label: "El Portal", value: "El Portal"}, | |
{label: "Fisher Island", value: "Fisher Island"}, | |
{label: "Fort Lauderdale", value: "Fort Lauderdale"}, | |
{label: "Gulfstream", value: "Gulfstream"}, | |
{label: "Golden Beach", value: "Golden Beach"}, | |
{label: "Hallandale", value: "Hallandale"}, | |
{label: "Hialeah", value: "Hialeah"}, | |
{label: "Hollywood", value: "Hollywood"}, | |
{label: "Homestead", value: "Homestead"}, | |
{label: "Indian Creek", value: "Indian Creek"}, | |
{label: "Kendall", value: "Kendall"}, | |
{label: "Key Biscayne", value: "Key Biscayne"}, | |
{label: "Miami", value: "Miami"}, | |
{label: "Miami Beach", value: "Miami Beach"}, | |
{label: "Miami Gardens", value: "Miami Gardens"}, | |
{label: "Miami Lakes", value: "Miami Lakes"}, | |
{label: "Miami Shores", value: "Miami Shores"}, | |
{label: "Miramar", value: "Miramar"}, | |
{label: "North Bay Village", value: "North Bay Village"}, | |
{label: "North Miami", value: "North Miami"}, | |
{label: "North Miami Beach", value: "North Miami Beach"}, | |
{label: "Palmetto Bay", value: "Palmetto Bay"}, | |
{label: "Pembroke Pines", value: "Pembroke Pines"}, | |
{label: "Pinecrest", value: "Pinecrest"}, | |
{label: "Plantation", value: "Plantation"}, | |
{label: "South Miami", value: "South Miami"}, | |
{label: "Sunny Isles Beach", value: "Sunny Isles Beach"}, | |
{label: "Surfside", value: "Surfside"}, | |
{label: "West Miami", value: "West Miami"}, | |
{label: "Weston", value: "Weston"} | |
], | |
modes: { | |
BOTH: [ | |
{ field: 'MLS#', id: 'field_157' }, | |
{ field: 'Address', id: 'field_881' }, | |
{ field: 'City', id: 'field_922' }, | |
{ field: 'County', id: 'field_61', options: my.vm.search.options.counties }, | |
{ field: 'Zip', id: 'field_10' }, | |
{ field: 'Property Type', id: 'field_' }, | |
{ field: 'Over $', id: 'field_137', modifier: 'greaterThan' }, | |
{ field: 'Under $', id: 'field_137', modifier: 'lessThan' }, | |
{ field: 'Total Area', id: 'field_261', modifier: 'greaterThan', filters: ['greaterThan','lessThan'] }, | |
{ field: 'Lot Size', id: 'field_232', modifier: 'greaterThan', filters: ['greaterThan','lessThan'] }, | |
{ field: 'Year Built', id: 'field_314', modifier: 'greaterThan', filters: ['greaterThan','lessThan'] }, | |
{ field: 'Date Entered', id: 'field_131' } | |
], | |
RES: { | |
BUY: [ | |
{ field: 'Rooms', id: 'field_25', options: my.vm.search.utils.enumerate(5,'Room'), modifier: 'equals' }, | |
{ field: 'Baths', id: 'field_92', options: my.vm.search.utils.enumerate(5,'Bath'), modifier: 'equals' }, | |
{ field: 'Pool', id: 'field_191' }, | |
{ field: 'Waterfront', id: 'field_295' }, | |
{ field: 'Terms Considered', id: 'field_273' }, | |
{ field: 'REO', id: 'field_1473' }, | |
{ field: 'Short Sale', id: 'field_1465' } | |
], | |
RNT: [ | |
{ field: 'Rooms', id: 'field_25', options: my.vm.search.utils.enumerate(5,'Room'), modifier: 'equals' }, | |
{ field: 'Baths', id: 'field_92', options: my.vm.search.utils.enumerate(5,'Bath'), modifier: 'equals' }, | |
{ field: 'Pool', id: 'field_191' }, | |
{ field: 'Waterfront', id: 'field_295' }, | |
{ field: 'Furnished', id: 'field_99' }, | |
{ field: 'Pets Allowed', id: 'field_181' } | |
] | |
}, | |
COM: { | |
BUY: [ | |
{ field: 'Terms Considered', id: 'field_273' }, | |
{ field: 'REO', id: 'field_1473' }, | |
{ field: 'Short Sale', id: 'field_1465' } | |
], | |
RNT: [] | |
} | |
}, | |
current: ko.observableArray([]), | |
active: ko.observableArray([]), | |
advanced: ko.observableArray([]) | |
}; | |
my.vm.search.fields.available = ko.computed(function(){ | |
var mode = my.vm.search.quick.modes.current(); | |
var type = my.vm.search.quick.modes.type(); | |
var sharedFields = my.vm.search.fields.modes.BOTH; | |
var modeFields = []; | |
if (mode==='RES') { | |
switch (type) { | |
case 'BUY': | |
modeFields = my.vm.search.fields.modes.RES.BUY; | |
break; | |
case 'RNT': | |
modeFields = my.vm.search.fields.modes.RES.RNT; | |
break; | |
} | |
} else | |
if (mode==='COM') { | |
switch (type) { | |
case 'BUY': | |
modeFields = my.vm.search.fields.modes.COM.BUY; | |
break; | |
case 'RNT': | |
modeFields = my.vm.search.fields.modes.COM.RNT; | |
break; | |
} | |
} | |
return sharedFields.concat(modeFields); | |
}); | |
my.vm.search.fields.new = { | |
toAdd: ko.observable(), | |
insert: function(a,b){ | |
var toAdd = my.vm.search.fields.new.toAdd(); | |
var field = ko.utils.arrayFirst(my.vm.search.fields.available(), function(item) { | |
return (item.id == toAdd); | |
}); | |
if(field){ | |
my.vm.search.fields.advanced.push(new my.vm.search.field(field)); | |
my.vm.search.fields.new.toAdd(null) | |
} | |
}, | |
available: ko.computed(function(){ | |
var arr = []; | |
var available = my.vm.search.fields.available(); | |
var active = my.vm.search.fields.active(); | |
var advanced = my.vm.search.fields.advanced(); | |
var instanced = active.concat(advanced); | |
ko.utils.arrayForEach(available, function(first) { | |
var exists = ko.utils.arrayFirst(instanced, function(second) { | |
return second.instance() == first.field.replace(/\s/g,"")+first.id; | |
}); | |
if(!exists){ | |
arr.push(first); | |
} | |
}); | |
return arr; | |
}) | |
}; | |
my.vm.search.utils.syncFields = ko.computed(function(){ | |
var criteria = my.vm.search.criteria(); | |
var advanced = my.vm.search.fields.advanced(); | |
var initFields = function(input,done){ | |
var toprocess = []; | |
// Look through criteria for new fields | |
ko.utils.arrayForEach(input, function(item) { | |
var field = ko.utils.arrayFirst(my.vm.search.fields.active(), function(instace) { | |
var id1 = instace.id(); | |
var id2 = item.id(); | |
var int1 = instace.instance(); | |
var int2 = item.instance(); | |
return (id1 === id2 && int1 === int2); | |
}); | |
if(!field){ | |
toprocess.push(item); | |
} | |
}); | |
done(toprocess); | |
}; | |
var syncFields = function(data,done){ | |
ko.utils.arrayForEach(data, function(item) { | |
my.vm.search.fields.active.push(item); | |
}); | |
done(); | |
}; | |
initFields(criteria,function(data){ | |
syncFields(data,function(){ | |
}); | |
}); | |
my.vm.search.fields.active(criteria.concat(advanced)); //SETS ACTIVE | |
}); | |
/* my.vm.search.utils.preload = ko.computed(function(){ | |
var criteria = my.vm.search.criteria(); | |
var preloaded = my.vm.search.quick.preloaded(); | |
var loaded = my.vm.search.quick.loaded(); | |
var initFields = function(done){ | |
for (var key in preloaded.query) { | |
var field = ko.utils.arrayFirst(my.vm.search.fields.available(), function(item) { | |
return (item.id == key); | |
}); | |
my.vm.search.fields.current.push(field); | |
} | |
done(); | |
}; | |
var preLoad = function(done){ | |
ko.utils.arrayForEach(criteria, function(item) { | |
for (var key in preloaded.query) { | |
if (item.id() == key) { | |
var field = preloaded.query[key]; | |
if(field != null && typeof field === 'object'){ | |
for (var modifier in preloaded.query[key]) { | |
item.setModifier(modifier); | |
item.criteria(field[modifier]); | |
} | |
} else if (field != null && typeof field !== 'object') { | |
item.setModifier('equals'); | |
item.criteria(field); | |
} | |
} | |
} | |
}); | |
}; | |
if(preloaded){ | |
initFields(function(){ | |
preLoad(); | |
}); | |
*//* // Loop through each search field attribute | |
for (var key in preloaded.query) { | |
// Get Instace, if Any | |
var instance = ko.utils.arrayFirst(criteria, function(item) { | |
return (item.id() == key); | |
}); | |
// If instace exists, update | |
if(instance){ | |
for (var modifier in preloaded.query[key]) { | |
instance.setModifier(modifier); | |
instance.criteria(preloaded.query[key][modifier]); | |
} | |
// If Not, then create new | |
} else { | |
//var field = my.vm.search.fields[key]; | |
//console.log(field); | |
// var newinstance = new my.vm.search.field(field); | |
//console.log(ko.toJS(newinstance)); | |
} | |
}*//* | |
// Preload Results | |
if(preloaded.results && !loaded){ | |
my.vm.search.utils.listResults(preloaded); | |
my.vm.search.quick.loaded(1); | |
} | |
} | |
});*/ | |
my.vm.search.destination = ko.computed(function(){ | |
var destination = false; | |
var modeSet = (my.vm.search.quick.modes.current()&&my.vm.search.quick.modes.type())? true:false; | |
if(modeSet){ | |
destination = my.vm.search.quick.modes.destination(); | |
} | |
return destination || false; | |
}); | |
my.vm.search.utils.pager = ko.computed(function(){ | |
var numResults = my.vm.search.data.results.count(); | |
var currPage = my.vm.search.utils.pagination.curPage(); | |
var perPage = my.vm.search.utils.pagination.perPage(); | |
var numPages = Math.ceil(numResults/perPage) || 0; | |
var offset = perPage*(currPage-1); | |
var rangeStart = ((currPage-4)>0)? currPage-4 : 1; | |
var rangeEnd = (rangeStart>4) ? (((currPage + 4)>numPages)? numPages : currPage + 4) : ((numPages<10) ? numPages : 10); | |
var pages = my.vm.search.utils.pagination.getArray(rangeStart,rangeEnd); | |
my.vm.search.utils.pagination.pages(pages); | |
my.vm.search.utils.pagination.numPages(numPages); | |
my.vm.search.utils.pagination.offset(offset); | |
}); | |
my.vm.search.utils.destinationSet= ko.computed(function(){ | |
return (my.vm.search.quick.modes.destination()) ? my.vm.search.quick.modes.destination() : false ; | |
}); | |
my.vm.search.utils.pagination.perPage.subscribe(function(){ | |
var ready = my.vm.search.ready(); | |
if(ready){ | |
my.vm.search.utils.pagination.curPage(1); | |
my.vm.search.quick.go(); | |
} | |
}); | |
my.vm.search.quick.types = ko.computed(function(){ | |
var choices = []; | |
var mode = my.vm.search.quick.modes.current(); | |
var type = my.vm.search.quick.modes.type(); | |
if (mode==='RES') { | |
switch (type) { | |
case 'BUY': | |
choices.push( | |
{value:'RE1',label:'Single Family Home'}, | |
{value:'RE2',label:'Condominiums'} | |
); | |
break; | |
case 'RNT': | |
choices.push( | |
{value:'RNT1',label:'Single Family Home'}, | |
{value:'RNT2',label:'Condominiums / Townhouse'} | |
); | |
break; | |
} | |
} else | |
if (mode==='COM') { | |
switch (type) { | |
case 'BUY': | |
choices.push( | |
{value:'COMB1',label:'Multifamily'}, | |
{value:'COMB2',label:'Office'}, | |
{value:'COMB3',label:'Industrial'}, | |
{value:'COMB4',label:'Retail'}, | |
{value:'COMB5',label:'Hotel & Motel'}, | |
{value:'COMB6',label:'Special Purpose'}, | |
{value:'COMB7',label:'Mixed Use'}, | |
{value:'RIN',label:'Residential Income'}, | |
{value:'RLD',label:'Residential Land'}, | |
{value:'BUS',label:'Business'}, | |
{value:'CLD1',label:'Land'}, | |
{value:'CLD2',label:'Agricultural'} | |
); | |
break; | |
case 'RNT': | |
choices.push( | |
{value:'COMR1',label:'Industrial'}, | |
{value:'COMR2',label:'Office'}, | |
{value:'COMR2',label:'Retail'} | |
); | |
break; | |
} | |
} | |
return choices; | |
}); | |
my.vm.search.quick.utils.target = ko.computed(function(){ | |
var source = (window.quickSearch) ? window.quickSearch.source : false; | |
var matched = ko.utils.arrayFilter(my.vm.search.quick.types(), function(item) { | |
return item.value === my.vm.search.quick.modes.destination(); | |
}); | |
return (!matched[0] && window.quickSearch) ? window.quickSearch.source : matched[0]; | |
}); | |
my.vm.search.query = ko.computed(function(){ | |
var criteria = my.vm.search.fields.active(); | |
var queryItems = ko.utils.arrayFilter(criteria, function(item) { | |
if(typeof item.criteria() !== 'undefined'){ | |
return (item.criteria()) ? true : false; | |
} | |
}); | |
var queryRaw = my.vm.search.utils.makeRaw(queryItems); | |
var queryPretty = my.vm.search.utils.makePretty(queryItems); | |
return { | |
items: queryItems, | |
raw: queryRaw, | |
pretty: queryPretty | |
}; | |
}); | |
my.vm.search.ready = ko.computed(function(){ | |
var searchObj = my.vm.search.query().raw; | |
var hasTarget = (my.vm.search.quick.utils.target())? true:false; | |
var hasQuery = ($.isEmptyObject(searchObj)) ? false : true; | |
var isBusy = my.vm.search.utils.isBusy(); | |
return (hasTarget && hasQuery && !isBusy) ? true : false; | |
}); | |
my.vm.search.utils.loading = ko.computed(function(){ | |
var ready = my.vm.search.ready(); | |
var busy = my.vm.search.utils.isBusy(); | |
return (my.vm.search.utils.isBusy()) ? 'Searching... <i class=\'icon-spin3 animate-spin\'></i>' : 'Run Search <i class=\'icon-search\'></i>'; | |
}); | |
my.vm.search.showButton = ko.computed(function(){ | |
var busy = my.vm.search.utils.isBusy(); | |
var ready = my.vm.search.ready(); | |
return (!busy && ready) ? true : false; | |
}); | |
$('#search-tabs').on('click', ' li a .close', function() { | |
var tabId = $(this).parents('li').children('a').attr('href'); | |
$(this).parents('li').remove('li'); | |
$(tabId).remove(); | |
$('#search-tabs a[href="#results"]').tab('show'); | |
}); | |
ko.bindingHandlers.inlineForm = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var defaultOptions = { | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
$(element).on('submit', function(e,d){ | |
// Hold Of Normal Form Behavior | |
e.preventDefault(); | |
e.stopPropagation(); | |
var settings = { | |
contact: options.contact, | |
subject: options.subject | |
}; | |
var formData = $(element).serializeArray(); | |
ko.utils.arrayForEach(formData, function(item) { | |
switch (item.name) { | |
case 'name': | |
settings.name = item.value; | |
break; | |
case 'email': | |
settings.email = item.value; | |
break; | |
case 'phone': | |
settings.phone = item.value; | |
break; | |
case 'message': | |
settings.message = item.value; | |
break; | |
default: | |
settings[item.name] = item.value; | |
break; | |
} | |
}); | |
var message = new my.vm.mailer.utils.makeMail(settings); | |
my.vm.mailer.utils.send(message,options.template,function(err){ | |
if(err) console.log(err); | |
$("body").animate({ scrollTop: 0 }, "fast"); | |
$('#thanks').modal('show'); | |
}); | |
}); | |
} | |
}; | |
ko.bindingHandlers.searchField = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.unwrap(options); | |
var defaultOptions = { | |
instance: unwrapped.field.replace(/\s/g,"")+options.id | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
var instace = new my.vm.search.field(options); | |
my.vm.search.criteria.push(instace); | |
ko.renderTemplate( | |
"search-field-tpl", | |
instace, | |
{ | |
afterRender: function(renderedElement) { | |
} | |
}, | |
element, | |
"replaceNode" | |
); | |
} | |
}; | |
ko.bindingHandlers.pagiNator = { | |
update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var defaultOptions = {}; | |
options = $.extend(true, {}, defaultOptions, options); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var pag = { | |
curPage: options.curPage(), | |
numPages: options.numPages(), | |
perPage: options.perPage(), | |
offset: options.offset(), | |
pages: options.pages() | |
} | |
ko.renderTemplate( | |
"paginator-tpl", | |
options, | |
{ | |
afterRender: function(renderedElement) { | |
} | |
}, | |
element, | |
"replaceNode" | |
); | |
} | |
}; | |
ko.bindingHandlers.gmap = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var defaultOptions = { | |
center: new google.maps.LatLng(25.7511143, -80.259729), | |
zoom: 14, | |
mapTypeId: google.maps.MapTypeId.ROADMAP, | |
scrollwheel: false | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
var map = new google.maps.Map(element,options); | |
var geocoder = new google.maps.Geocoder(); | |
var label = new google.maps.InfoWindow({ | |
content: options.address | |
}); | |
geocoder.geocode({ 'address': options.address }, function(results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
map.setCenter(results[0].geometry.location); | |
var marker = new google.maps.Marker({ | |
map: map, | |
position: results[0].geometry.location | |
}); | |
label.open(map,marker) | |
}; | |
}); | |
} | |
}; | |
my.vm.email = (function(){ | |
var | |
data = { | |
id: ko.observable(null), | |
address: ko.observable(null), | |
link: ko.observable(null), | |
sender: ko.observable(null), | |
recipients: ko.observable(null), | |
message: ko.observable(null) | |
}, | |
utils = { | |
open: function(data){ | |
my.vm.email.data.id(data.id); | |
my.vm.email.data.link(data.link); | |
my.vm.email.data.address(data.address); | |
$('#email-share-modal').modal('show'); | |
}, | |
send: function(){ | |
var data = { | |
subject: 'Somebody has shared a listing with you.', | |
email: my.vm.email.data.sender(), | |
contact: my.vm.email.data.recipients(), | |
message: my.vm.email.utils.message() | |
} | |
var message = new my.vm.mailer.utils.makeMail(data); | |
my.vm.mailer.utils.send(message,'share-by-email',function(err){ | |
if(err) console.log(err); | |
$('.email-listing')[0].reset(); | |
$('#email-share-modal').modal('hide'); | |
}); | |
} | |
}; | |
return { | |
data: data, | |
utils: utils | |
} | |
})(my.vm.email); | |
my.vm.email.utils.message = ko.computed(function(){ | |
var id = my.vm.email.data.id(); | |
var link = my.vm.email.data.link(); | |
var address = my.vm.email.data.address(); | |
var message = my.vm.email.data.message(); | |
var line1 = "<p>Hello, I was looking at this property on The Canero Group and wanted to share it with you.\n" | |
var line2 = "<a href='" + link + "' title='" + address + "' target='_blank'>Click here to see " + address + " on CaneroGroup.com</a>\n</p>" | |
var line3 = (message) ? '<p>' + message + '</p>' : ''; | |
return line1 + line2 + line3; | |
}); | |
ko.bindingHandlers.facebookShare = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var defaultOptions = { | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
$(element).sharrre({ | |
share: { | |
facebook: true | |
}, | |
template: '<a href="#"><i class="icon-facebook-rect"></i> Like on Facebook</a>', | |
enableHover: false, | |
enableTracking: false, | |
click: function(api, options){ | |
api.simulateClick(); | |
api.openPopup('facebook'); | |
}, | |
title: options.title, | |
text: 'I\'m looking at ' + options.title + ' on The Canero Group, Real Estate Investment Services', | |
url: options.url | |
}); | |
} | |
}; | |
ko.bindingHandlers.shareButton = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var defaultOptions = { | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
$(element).sharrre({ | |
share: { | |
facebook: true | |
}, | |
template: '<a href="#"><i class="icon-facebook-rect"></i> Like on Facebook</a>', | |
enableHover: false, | |
enableTracking: false, | |
click: function(api, options){ | |
api.simulateClick(); | |
api.openPopup('facebook'); | |
}, | |
title: options.title, | |
text: 'I\'m looking at ' + options.title + ' on The Canero Group, Real Estate Investment Services', | |
url: options.url | |
}); | |
} | |
}; | |
ko.bindingHandlers.twitterShare = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var defaultOptions = { | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
$(element).sharrre({ | |
share: { | |
twitter: true | |
}, | |
template: '<a href="#"><i class="icon-twitter-squared"></i> Share on Twitter</a>', | |
enableHover: false, | |
enableTracking: false, | |
click: function(api, options){ | |
api.simulateClick(); | |
api.openPopup('twitter'); | |
}, | |
title: options.title, | |
text: 'I\'m looking at ' + options.title + ' on The Canero Group, Real Estate Investment Services', | |
url: options.url | |
}); | |
} | |
}; | |
ko.bindingHandlers.gplusShare = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var defaultOptions = { | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
$(element).sharrre({ | |
share: { | |
facebook : true | |
}, | |
template: '<a href="#"><i class="icon-googleplus-rect"></i> Share on Google+</a>', | |
enableHover: false, | |
enableTracking: false, | |
click: function(api, options){ | |
api.simulateClick(); | |
api.openPopup('googlePlus'); | |
}, | |
title: options.title, | |
text: 'I\'m looking at ' + options.title + ' on The Canero Group, Real Estate Investment Services', | |
url: options.url | |
}); | |
} | |
}; | |
ko.bindingHandlers.linkedInShare = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var defaultOptions = { | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
$(element).sharrre({ | |
share: { | |
linkedin: true | |
}, | |
template: '<a href="#"><i class="icon-linkedin-rect"></i> Post on LinkedIn</a>', | |
enableHover: false, | |
enableTracking: false, | |
click: function(api, options){ | |
api.simulateClick(); | |
api.openPopup('linkedin'); | |
}, | |
title: options.title, | |
text: 'I\'m looking at ' + options.title + ' on The Canero Group, Real Estate Investment Services', | |
url: options.url | |
}); | |
} | |
}; | |
my.vm.mortgage = (function(){ | |
var | |
data= { | |
baseprice: ko.observable(0), | |
downpayment: ko.observable(20), | |
loanamount: ko.observable(0), | |
interest: ko.observable(4.5), | |
years: ko.observable(30), | |
payments: ko.observable(0) | |
}, | |
utils = { | |
calculate: function(a,b){ | |
my.vm.mortgage.data.baseprice($(b.target).data('price')); | |
$('#mortgage-calculator').modal('show'); | |
} | |
}; | |
return { | |
data: data, | |
utils: utils | |
} | |
})(my.vm.appointments); | |
my.vm.mortgage.calculate = ko.computed(function(){ | |
var baseprice = parseFloat(my.vm.mortgage.data.baseprice()); | |
var downpayment = parseFloat(my.vm.mortgage.data.downpayment()); | |
var interest = parseFloat(my.vm.mortgage.data.interest())/1200; | |
var term = parseFloat(my.vm.mortgage.data.years())*12; | |
var loanamount = baseprice - baseprice*(downpayment*.01); | |
var payments = loanamount * interest / (1 - (Math.pow(1/(1 + interest), term))); | |
my.vm.mortgage.data.loanamount(loanamount); | |
my.vm.mortgage.data.payments(payments); | |
return parseFloat(payments); | |
}); | |
ko.bindingHandlers.caculateMortgage = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var defaultOptions = { | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
my.vm.mortgage.data.baseprice(parseInt(options.price)); | |
ko.applyBindingsToDescendants(my.vm,document.getElementById('mortgage-calculator')); | |
} | |
}; | |
my.vm.images = (function(){ | |
var | |
data= { | |
baseprice: ko.observable(0), | |
downpayment: ko.observable(20), | |
loanamount: ko.observable(0), | |
interest: ko.observable(4.5), | |
years: ko.observable(30), | |
payments: ko.observable(0) | |
}, | |
utils = { | |
calculate: function(a,b){ | |
my.vm.mortgage.data.baseprice($(b.target).data('price')); | |
$('#mortgage-calculator').modal('show'); | |
} | |
}; | |
return { | |
data: data, | |
utils: utils | |
} | |
})(my.vm.images); | |
ko.bindingHandlers.imgSlider = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var defaultOptions = { | |
url: $(element).data('url'), | |
id: $(element).data('listing'), | |
count: $(element).data('count') | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
var id = $(element).attr('id'); | |
var newImage = function(info,done){ | |
var image = $("<img class='img-responsive max-width' />").attr('src', info.url) | |
.load(function(a) { | |
var style = ''; | |
var count = $('#gallery-carousel-'+options.id+' .carousel-inner .item').length; | |
if(!count){ | |
style = 'active'; | |
} | |
var item = $("<div class='item' />").addClass(style).append(image); | |
$('#gallery-carousel-'+options.id+' .carousel-inner').append(item); | |
}); | |
done(); | |
}; | |
function makeList(count,done){ | |
var arr = []; | |
for (var i = 1; i <= count; ++i) { | |
arr.push({id:i,url:options.url+'_'+i+'.jpg'}); | |
arr.push({id:i,url:options.url+'-'+i+'.jpg'}); | |
} | |
done(arr); | |
} | |
var images = makeList(options.count,function(items){ | |
async.eachSeries(items, newImage, function(err){ | |
console.log('asyndone'); | |
}); | |
}); | |
} | |
}; | |
ko.bindingHandlers.thumbSlider = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var input = valueAccessor(); | |
var options = { | |
url: input.url(), | |
id: input.listing(), | |
count: input.count() | |
}; | |
var pre = $("<img class='img-responsive max-width thumb-placeholder' />").attr('src', 'http://placehold.it/130x100&text=No+Image+Found') | |
.load(function(a) { | |
var item = $("<div class='item' />").addClass('active').append(pre); | |
console.log(item); | |
$('#thumb-carousel-'+options.id+' .carousel-inner').append(item); | |
}); | |
var id = $(element).attr('id'); | |
var newImage = function(info,done){ | |
var image = $("<img class='img-responsive max-width' />").attr('src', info.url) | |
.load(function(a) { | |
var style = ""; | |
var placeholder = $('#thumb-carousel-'+options.id+' .carousel-inner').find(".thumb-placeholder"); | |
if(placeholder){ | |
placeholder.parent('.item').remove(); | |
} | |
var count = $('#thumb-carousel-'+options.id+' .carousel-inner .item').length; | |
if(!count){ | |
style = 'active'; | |
} | |
var item = $("<div class='item' />").addClass(style).append(image); | |
$('#thumb-carousel-'+options.id+' .carousel-inner').append(item); | |
}); | |
done(); | |
}; | |
function makeList(count,done){ | |
var arr = []; | |
for (var i = 1; i <= 2; ++i) { | |
arr.push({id:i,url:options.url+'_'+i+'.jpg'}); | |
arr.push({id:i,url:options.url+'-'+i+'.jpg'}); | |
} | |
done(arr); | |
} | |
var images = makeList(options.count,function(items){ | |
async.eachSeries(items, newImage, function(err){ | |
console.log('asyndone'); | |
}); | |
}); | |
} | |
}; | |
ko.bindingHandlers.listThumb = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var image = $("<img class='img-responsive max-width thumbnail'/>").attr('src', 'http://placehold.it/130x100&text=Not+Available') | |
.load(function(a) { | |
$(element).html(image); | |
}); | |
console.log('listthumbbound'); | |
var input = valueAccessor(); | |
var options = { | |
url: input.url(), | |
id: input.listing(), | |
count: input.count() | |
}; | |
var id = $(element).attr('id'); | |
var rendered = false; | |
var newImage = function(info,done){ | |
console.log('newimage'); | |
console.log(rendered); | |
if(!rendered){ | |
console.log('letstry'); | |
console.log(info.url); | |
var image = $("<img class='img-responsive max-width thumbnail'/>").attr('src', info.url) | |
.load(function(a) { | |
rendered = true; | |
$(element).html(image); | |
}); | |
} | |
done(); | |
}; | |
function makeList(count,done){ | |
var arr = []; | |
for (var i = 1; i <= count; ++i) { | |
arr.push(); | |
arr.push({id:i,url:options.url+'-'+i+'.jpg'}); | |
} | |
done(arr); | |
} | |
var items = [ | |
{id:4,url:options.url+'-'+4+'.jpg'}, | |
{id:3,url:options.url+'-'+3+'.jpg'}, | |
{id:2,url:options.url+'-'+2+'.jpg'}, | |
{id:1,url:options.url+'-'+1+'.jpg'}, | |
{id:4,url:options.url+'_'+4+'.jpg'}, | |
{id:3,url:options.url+'_'+3+'.jpg'}, | |
{id:2,url:options.url+'_'+2+'.jpg'}, | |
{id:1,url:options.url+'_'+1+'.jpg'} | |
]; | |
async.eachSeries(items, newImage, function(err){ | |
console.log('asyndone'); | |
}); | |
} | |
}; | |
ko.bindingHandlers.imgThumbs = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var defaultOptions = { | |
url: $(element).data('url'), | |
id: $(element).data('listing'), | |
count: $(element).data('count') | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
var id = $(element).attr('id'); | |
var newImage = function(info,done){ | |
var image = $("<img class='img-responsive max-width' />").attr('src', info.url) | |
.load(function(a) { | |
var controls = $("<a class='thumbnail add-bottom' />").html(image); | |
var navItem = $("<div class='col-sm-2' />").append(controls); | |
$('#gallery-slider-'+options.id+' .thumbnail-controls .row').append(navItem); | |
}); | |
done(); | |
}; | |
function makeList(count,done){ | |
var arr = []; | |
for (var i = 1; i <= count; ++i) { | |
arr.push({id:i,url:options.url+'_'+i+'.jpg'}); | |
arr.push({id:i,url:options.url+'-'+i+'.jpg'}); | |
} | |
done(arr); | |
} | |
var images = makeList(options.count,function(items){ | |
async.eachSeries(items, newImage, function(err){ | |
console.log('asyndone'); | |
}); | |
}); | |
} | |
}; | |
ko.bindingHandlers.affix = { | |
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | |
var options = valueAccessor(); | |
var unwrapped = ko.utils.unwrapObservable(options); | |
var defaultOptions = { | |
width: $(element).width(), | |
min: 50, | |
windowH: $(window).outerHeight(), | |
windowW: $(window).outerWidth() | |
}; | |
options = $.extend(true, {}, defaultOptions, options); | |
if(options.windowH>options.min&&options.windowW>974){ | |
$(element).affix({ | |
offset: { | |
top: function(){ | |
$(element).width(options.width); | |
return options.top; | |
} | |
} | |
}); | |
} | |
} | |
}; | |
ko.applyBindingsWithValidation(my.vm, document.getElementById('main-content'),{ | |
registerExtenders: true, | |
messagesOnModified: true, | |
insertMessages: true, | |
errorMessageClass: 'cms-message-error', | |
decorateElement: true, | |
errorElementClass: 'cms-input-error', | |
parseInputAttributes: true, | |
messageTemplate: null } | |
); | |
// Activate Tooltips | |
$('.tooltips').tooltip(); | |
my.vm.alerts = (function(){ | |
var | |
create = function(alert){ | |
var self = this; | |
self.mode = alert.mode; | |
self.message = alert.message; | |
}, | |
utils = { | |
new: function(alert){ | |
var scope = angular.element($("#alerts")).scope(); | |
scope.$apply(function(){ | |
scope.alerts.push(new my.vm.alerts.create(alert)) | |
}); | |
} | |
} | |
return { | |
create: create, | |
utils: utils | |
} | |
})(my.vm.alerts); | |
my.vm.app = angular.module("MyApp", ['ngSails']); | |
my.vm.app.controller("Alerts", function ($scope, $sails, $timeout) { | |
$scope.alerts = []; | |
(function () { | |
$sails.get('/user/subscribe', function (data) { | |
if(200){ | |
//success | |
} else | |
if(403) { | |
//forbidden | |
} | |
}); | |
$sails.on("message", function (message) { | |
if (message.model === "user") { | |
if(message.data.alert){ | |
$scope.addAlert(message.data.alert); | |
} | |
} | |
}); | |
}()); | |
$scope.addAlert = function(alert) { | |
$scope.alerts.push(new my.vm.alerts.create(alert)); | |
if (alert.timeout) { | |
$timeout(function(){ | |
$scope.closeAlert($scope.alerts.indexOf(alert)); | |
}, alert.timeout); | |
} | |
}; | |
$scope.closeAlert = function(index) { | |
$scope.alerts.splice(index, 1); | |
}; | |
}); | |
angular.bootstrap(document.getElementById('app'), ['MyApp']); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment